/* ***************************************************************** * * * SB-Prolog * * Copyright SUNY at Stony Brook, 1986 * * * ****************************************************************** */ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ /* begin asmpass11.P ***************************************/ /* ********************************************************************** $asmpass11_export([$asm_dopass1/6]). $asmpass11_use($blist,[_,_,$member1/2]). $asmpass11_use($name,[$name/2,_]). $asmpass11_use($meta,[_,_,$length/2]). $asmpass11_use($listutil1,[_,_,_,_,_,_,$member2/2,$closetail/1]). ********************************************************************** */ $asm_pass1([], _, _, N, N). $asm_pass1([label(X)| Rest], Lsym, Csym, Lc, NewLc) :- $member1((X, Lc), Lsym), $asm_pass1(Rest, Lsym, Csym, Lc, NewLc). $asm_pass1([Special| Rest], Lsym, Csym, Lc, NLc) :- $asmpass1_do(Special, Csym), $asmpass1_doinst(Special, Nbytes), NewLc is Lc + Nbytes, $asm_pass1(Rest, Lsym, Csym, NewLc, NLc). $asm_pass1([Inst| Rest], Lsym, Csym, Lc, NLc):- $asmpass1_doinst(Inst, Nbytes), NewLc is Lc + Nbytes, $asm_pass1(Rest, Lsym, Csym, NewLc, NLc). $asmpass1_do(getcon(Con,_), Csym) :- $member1((Con,0,_), Csym). $asmpass1_do(getstr((Str,Arity), _), Csym) :- $member1((Str,Arity,_), Csym). $asmpass1_do(unicon(Con), Csym) :- $member1((Con,0,_), Csym). $asmpass1_do(putcon(Con,_), Csym) :- $member1((Con,0,_), Csym). $asmpass1_do(putstr((Str,Arity), _), Csym) :- $member1((Str,Arity,_), Csym). $asmpass1_do(bldcon(Con), Csym) :- $member1((Con,0,_), Csym). $asmpass1_do(putstrv((Str,Arity), _), Csym) :- $member1((Str,Arity,_), Csym). $asmpass1_do(getstrv((Str,Arity), _), Csym) :- $member1((Str,Arity,_), Csym). $asmpass1_do(call((Pname, Arity), _), Csym) :- (Arity >= 0, $member1((Pname, Arity, _), Csym)) ; (Arity < 0). $asmpass1_do(calld((Pname, Arity), _), Csym) :- (Arity >= 0, $member1((Pname, Arity, _), Csym)) ; (Arity < 0). $asmpass1_do(execute((Pname, Arity)), Csym):- $member1((Pname, Arity, _), Csym). $asmpass1_doinst(label(X),0). $asmpass1_doinst(getpvar(V,R),4). $asmpass1_doinst(getpval(V,R),4). $asmpass1_doinst(gettval(R,R1),4). $asmpass1_doinst(getcon(I,R),6). $asmpass1_doinst(getnumcon(I,R),6). $asmpass1_doinst(getfloatcon(I,R),6). $asmpass1_doinst(getnil(R),2). $asmpass1_doinst(getstr(I,R),6). $asmpass1_doinst(getlist(R),2). $asmpass1_doinst(getlist_tvar_tvar(R0,R1,R2),4). $asmpass1_doinst(getcomma(R),2). $asmpass1_doinst(getcomma_tvar_tvar(R0,R1,R2),4). $asmpass1_doinst(unipvar(V),2). $asmpass1_doinst(unipval(V),2). $asmpass1_doinst(unitvar(R),2). $asmpass1_doinst(unitval(R),2). $asmpass1_doinst(unicon(I),6). $asmpass1_doinst(uninumcon(N),6). $asmpass1_doinst(unifloatcon(N),6). $asmpass1_doinst(uninil,2). $asmpass1_doinst(putpvar(V,R),4). $asmpass1_doinst(putpval(V,R),4). $asmpass1_doinst(puttvar(R,R1),4). $asmpass1_doinst(putcon(I,R),6). $asmpass1_doinst(putnumcon(N,R),6). $asmpass1_doinst(putfloatcon(I,R),6). $asmpass1_doinst(putnil(R),2). $asmpass1_doinst(putstr(I,R),6). $asmpass1_doinst(putstrv(I,R),6). $asmpass1_doinst(getstrv(I,V),6). $asmpass1_doinst(putlist(R),2). $asmpass1_doinst(bldpvar(V),2). $asmpass1_doinst(bldpval(V),2). $asmpass1_doinst(bldtvar(R),2). $asmpass1_doinst(bldtval(R),2). $asmpass1_doinst(bldcon(I),6). $asmpass1_doinst(bldnumcon(N),6). $asmpass1_doinst(bldfloatcon(N),6). $asmpass1_doinst(bldnil,2). $asmpass1_doinst(trymeelse(L,A),6). $asmpass1_doinst(retrymeelse(L,A),6). $asmpass1_doinst(trustmeelsefail(A),2). $asmpass1_doinst(try(L,A),6). $asmpass1_doinst(retry(L,A),6). $asmpass1_doinst(trust(L,A),6). $asmpass1_doinst(getpbreg(V),2). $asmpass1_doinst(gettbreg(R),2). $asmpass1_doinst(putpbreg(V),2). $asmpass1_doinst(puttbreg(R),2). $asmpass1_doinst(switchonterm(R,L,L1),10). $asmpass1_doinst(switchoncon(L),6). $asmpass1_doinst(switchonstr(L),6). $asmpass1_doinst(switchonbound(R, L1, L2), 10). $asmpass1_doinst(addreg(R,R1),4). $asmpass1_doinst(subreg(R,R1),4). $asmpass1_doinst(mulreg(R,R1),4). $asmpass1_doinst(divreg(R,R1),4). $asmpass1_doinst(and(R,R1),4). $asmpass1_doinst(or(R,R1),4). $asmpass1_doinst(logshiftl(R,R1),4). $asmpass1_doinst(logshiftr(R,R1),4). $asmpass1_doinst(negate(R),2). $asmpass1_doinst(movreg(R,R1),4). $asmpass1_doinst(putdval(V,R),4). $asmpass1_doinst(putuval(V,R),4). $asmpass1_doinst(call(I,B),6). $asmpass1_doinst(calld(L,B),6). $asmpass1_doinst(allocate,2). $asmpass1_doinst(deallocate,2). $asmpass1_doinst(proceed,2). $asmpass1_doinst(execute(I),6). $asmpass1_doinst(callv(B),2). $asmpass1_doinst(executev,2). $asmpass1_doinst(jump(L),6). $asmpass1_doinst(jumpz(L,R),6). $asmpass1_doinst(jumpnz(L,R),6). $asmpass1_doinst(jumplt(L,R),6). $asmpass1_doinst(jumple(L,R),6). $asmpass1_doinst(jumpgt(L,R),6). $asmpass1_doinst(jumpge(L,R),6). $asmpass1_doinst(fail,2). $asmpass1_doinst(noop,2). $asmpass1_doinst(halt,2). $asmpass1_doinst(builtin(W),2). $asmpass1_doinst(Junk,2) :- write('unknown opcode detected in pass1'),tab(3), write(Junk),nl. /* Fill in the values of any symbols which have not been defined with the value -2. */ $asmpass1_setundef([], N, N) :- !. $asmpass1_setundef([ (Pred, Arity, Val) | Rest ], Nout, Nin) :- $name(Pred, Chars), $length(Chars, L), Nmed is Nin + L + 6, (Val = -2; true), $asmpass1_setundef(Rest, Nout, Nmed). /* Fill in the values of any predicates which are referenced and defined within this module. Append the names of any predicates which are defined but not referenced by this module. Ignore predicates with arity < 0, these are strictly internal jumps that should not go via the symbol table. */ $asmpass1_fillin([], _). $asmpass1_fillin([ (Name, Arity, LcValue) | Rest ], Table) :- $member1((Name, Arity, LcValue), Table), $asmpass1_fillin(Rest, Table). /* Uniq creates a list (Ulist) containing only the first label encountered for each predicate-name arity pair and excludes all labels whose predicate-name arity pair have an arity of -1. Labels with an arity of -1 are used as the targets of jump instructions only. */ $asm_uniq([],_). $asm_uniq([ ((Pname, Arity,_),_) | Tail], Ulist) :- (Arity = -1 ; /* internal branch */ $member2((Pname, Arity, _), Ulist)), /* already present in ST */ $asm_uniq(Tail, Ulist). $asm_uniq([ ((Pname,Arity,_),Lc) | Tail], Ulist) :- $member1((Pname, Arity, Lc), Ulist), $asm_uniq(Tail, Ulist). $asm_dopass1(Program, Index, ConstantSymtab, LabelSymtab, Ntext, Npsc) :- $asm_pass1(Program,LabelSymtab,ConstantSymtab,0,Ntext), $asm_index_pass1(Index, ConstantSymtab), $asm_uniq( LabelSymtab, EntrypointTable ), $closetail( LabelSymtab ), $closetail( EntrypointTable ), $asmpass1_fillin( EntrypointTable, ConstantSymtab ), $asmpass1_setundef( ConstantSymtab, Npsc, 0), !. $asm_index_pass1([],_). $asm_index_pass1([pred(_,_)|Rest], Csym) :- $asm_index_pass1(Rest, Csym). $asm_index_pass1([arglabel(T,Val,Label)|Rest],Csym) :- ((T = c, $member1((Val, 0, _), Csym)); (T = s, Val = (Str, Ar), $member1((Str, Ar, _), Csym)); true), $asm_index_pass1(Rest, Csym). /* end asmpass11.P *********************************************/