1 /* Pawn compiler - code generation (unoptimized "assembler" code)
3 * Copyright (c) ITB CompuPhase, 1997-2006
5 * This software is provided "as-is", without any express or implied warranty.
6 * In no event will the authors be held liable for any damages arising from
7 * the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software in
15 * a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
26 #include <stdlib.h> /* for _MAX_PATH */
29 #include <alloc/fortify.h>
33 static int fcurseg; /* the file number (fcurrent) for the active segment */
36 /* When a subroutine returns to address 0, the AMX must halt. In earlier
37 * releases, the RET and RETN opcodes checked for the special case 0 address.
38 * Today, the compiler simply generates a HALT instruction at address 0. So
39 * a subroutine can savely return to 0, and then encounter a HALT.
41 SC_FUNC void writeleader(symbol *root)
43 int lbl_nostate,lbl_table;
46 constvalue *fsa, *state, *stlist;
48 char lbl_default[sNAMEMAX+1];
53 stgwrite(";program exit point\n");
54 stgwrite("\thalt 0\n\n");
55 code_idx+=opcodes(1)+opargs(1); /* calculate code length */
57 /* check whether there are any functions that have states */
58 for (sym=root->next; sym!=NULL; sym=sym->next)
59 if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL)
62 return; /* no function has states, nothing to do next */
64 /* generate an error function that is called for an undefined state */
65 stgwrite("\n;exit point for functions called from the wrong state\n");
66 lbl_nostate=getlabel();
67 setlabel(lbl_nostate);
69 outval(AMX_ERR_INVSTATE,TRUE);
70 code_idx+=opcodes(1)+opargs(1); /* calculate code length */
72 /* write the "state-selectors" table with all automatons (update the
73 * automatons structure too, as we are now assigning the address to
74 * each automaton state-selector variable)
76 assert(glb_declared==0);
78 for (fsa=sc_automaton_tab.next; fsa!=NULL; fsa=fsa->next) {
80 stgwrite("0\t; automaton ");
81 if (strlen(fsa->name)==0)
82 stgwrite("(anonymous)");
86 fsa->value=glb_declared*sizeof(cell);
90 /* write stubs and jump tables for all state functions */
92 for (sym=root->next; sym!=NULL; sym=sym->next) {
93 if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL) {
94 stlist=sym->states->next;
95 assert(stlist!=NULL); /* there should be at least one state item */
97 assert(listid==-1 || listid>0);
98 if (listid==-1 && stlist->next!=NULL) {
99 /* first index is the "fallback", take the next one (if available) */
101 listid=stlist->index;
104 /* first index is the fallback, there is no second... */
105 strcpy(stlist->name,"0"); /* insert dummy label number */
106 /* this is an error, but we postpone adding the error message until the
107 * function definition
111 /* generate label numbers for all statelist ids */
112 for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
113 assert(strlen(stlist->name)==0);
114 strcpy(stlist->name,itoh(getlabel()));
116 if (strcmp(sym->name,uENTRYFUNC)==0)
117 continue; /* do not generate stubs for this special function */
118 sym->addr=code_idx; /* fix the function address now */
119 /* get automaton id for this function */
121 fsa_id=state_getfsa(listid);
122 assert(fsa_id>=0); /* automaton 0 exists */
123 fsa=automaton_findid(fsa_id);
124 /* count the number of states actually used; at the sane time, check
125 * whether there is a default state function
128 strcpy(lbl_default,itoh(lbl_nostate));
129 for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
130 if (stlist->index==-1) {
131 assert(strlen(stlist->name)<sizeof lbl_default);
132 strcpy(lbl_default,stlist->name);
134 statecount+=state_count(stlist->index);
137 /* generate a stub entry for the functions */
138 stgwrite("\tload.pri ");
139 outval(fsa->value,FALSE);
143 code_idx+=opcodes(1)+opargs(1); /* calculate code length */
144 lbl_table=getlabel();
146 /* generate the jump table */
148 ffcase(statecount,lbl_default,TRUE);
149 for (state=sc_state_tab.next; state!=NULL; state=state->next) {
150 if (state->index==fsa_id) {
151 /* find the label for this list id */
152 for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
153 if (stlist->index!=-1 && state_inlist(stlist->index,(int)state->value)) {
154 ffcase(state->value,stlist->name,FALSE);
158 if (stlist==NULL && strtol(lbl_default,NULL,16)==lbl_nostate)
159 error(230,state->name,sym->name); /* unimplemented state, no fallback */
160 } /* if (state belongs to automaton of function) */
163 } /* if (is function, used & having states) */
168 * Not much left of this once important function.
170 * Global references: pc_stksize (referred to only)
171 * sc_dataalign (referred to only)
173 * glb_declared (altered)
175 SC_FUNC void writetrailer(void)
177 assert(sc_dataalign % opcodes(1) == 0); /* alignment must be a multiple of
179 assert(sc_dataalign!=0);
181 /* pad code to align data segment */
182 if ((code_idx % sc_dataalign)!=0) {
184 while ((code_idx % sc_dataalign)!=0)
188 /* pad data segment to align the stack and the heap */
189 assert(litidx==0); /* literal queue should have been emptied */
190 assert(sc_dataalign % sizeof(cell) == 0);
191 if (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
194 while (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
200 stgwrite("\nSTKSIZE "); /* write stack size (align stack top) */
201 outval(pc_stksize - (pc_stksize % sc_dataalign), TRUE);
205 * Start (or restart) the CODE segment.
207 * In fact, the code and data segment specifiers are purely informational;
208 * the "DUMP" instruction itself already specifies that the following values
209 * should go to the data segment. All other instructions go to the code
212 * Global references: curseg
215 SC_FUNC void begcseg(void)
217 if (sc_status!=statSKIP && (curseg!=sIN_CSEG || fcurrent!=fcurseg)) {
220 outval(fcurrent,FALSE);
222 outval(code_idx,TRUE);
229 * Start (or restart) the DATA segment.
231 * Global references: curseg
233 SC_FUNC void begdseg(void)
235 if (sc_status!=statSKIP && (curseg!=sIN_DSEG || fcurrent!=fcurseg)) {
238 outval(fcurrent,FALSE);
240 outval((glb_declared-litidx)*sizeof(cell),TRUE);
246 SC_FUNC void setline(int chkbounds)
249 stgwrite("\t; line ");
252 if ((sc_debug & sSYMBOLIC)!=0 || chkbounds && (sc_debug & sCHKBOUNDS)!=0) {
253 /* generate a "break" (start statement) opcode rather than a "line" opcode
254 * because earlier versions of Small/Pawn have an incompatible version of the
257 stgwrite("\tbreak\t; ");
258 outval(code_idx,TRUE);
259 code_idx+=opcodes(1);
263 SC_FUNC void setfiledirect(char *name)
265 if (sc_status==statFIRST && sc_listing) {
267 pc_writeasm(outf,"#file ");
268 pc_writeasm(outf,name);
269 pc_writeasm(outf,"\n");
273 SC_FUNC void setlinedirect(int line)
275 if (sc_status==statFIRST && sc_listing) {
277 sprintf(string,"#line %d\n",line);
278 pc_writeasm(outf,string);
284 * Post a code label (specified as a number), on a new line.
286 SC_FUNC void setlabel(int number)
290 stgwrite((char *)itoh(number));
291 /* To assist verification of the assembled code, put the address of the
292 * label as a comment. However, labels that occur inside an expression
293 * may move (through optimization or through re-ordering). So write the
294 * address only if it is known to accurate.
298 outval(code_idx,FALSE);
303 /* Write a token that signifies the start or end of an expression or special
304 * statement. This allows several simple optimizations by the peephole
307 SC_FUNC void markexpr(optmark type,const char *name,cell offset)
311 stgwrite("\t;$exp\n");
314 stgwrite("\t;$par\n");
318 stgwrite("\t;$lcl ");
328 /* startfunc - declare a CODE entry point (function start)
330 * Global references: funcstatus (referred to only)
332 SC_FUNC void startfunc(char *fname)
336 char symname[2*sNAMEMAX+16];
337 funcdisplayname(symname,fname);
342 code_idx+=opcodes(1);
347 * Declare a CODE ending point (function end)
349 SC_FUNC void endfunc(void)
351 stgwrite("\n"); /* skip a line */
356 * Aligns the frame (and the stack) of the current function to a multiple
357 * of the specified byte count. Two caveats: the alignment ("numbytes") should
358 * be a power of 2, and this alignment must be done right after the frame
359 * is set up (before the first variable is declared)
361 SC_FUNC void alignframe(int numbytes)
364 /* "numbytes" should be a power of 2 for this code to work */
366 for (i=0; i<sizeof numbytes*8; i++)
367 if (numbytes & (1 << i))
372 stgwrite("\tlctrl 4\n"); /* get STK in PRI */
373 stgwrite("\tconst.alt "); /* get ~(numbytes-1) in ALT */
374 outval(~(numbytes-1),TRUE);
375 stgwrite("\tand\n"); /* PRI = STK "and" ~(numbytes-1) */
376 stgwrite("\tsctrl 4\n"); /* set the new value of STK ... */
377 stgwrite("\tsctrl 5\n"); /* ... and FRM */
378 code_idx+=opcodes(5)+opargs(4);
383 * Generate code to get the value of a symbol into "primary".
385 SC_FUNC void rvalue(value *lval)
390 if (lval->ident==iARRAYCELL) {
391 /* indirect fetch, address already in PRI */
392 stgwrite("\tload.i\n");
393 code_idx+=opcodes(1);
394 } else if (lval->ident==iARRAYCHAR) {
395 /* indirect fetch of a character from a pack, address already in PRI */
396 stgwrite("\tlodb.i ");
397 outval(sCHARBITS/8,TRUE); /* read one or two bytes */
398 code_idx+=opcodes(1)+opargs(1);
399 } else if (lval->ident==iREFERENCE) {
400 /* indirect fetch, but address not yet in PRI */
402 assert(sym->vclass==sLOCAL);/* global references don't exist in Pawn */
403 if (sym->vclass==sLOCAL)
404 stgwrite("\tlref.s.pri ");
406 stgwrite("\tlref.pri ");
407 outval(sym->addr,TRUE);
408 markusage(sym,uREAD);
409 code_idx+=opcodes(1)+opargs(1);
411 /* direct or stack relative fetch */
413 if (sym->vclass==sLOCAL)
414 stgwrite("\tload.s.pri ");
416 stgwrite("\tload.pri ");
417 outval(sym->addr,TRUE);
418 markusage(sym,uREAD);
419 code_idx+=opcodes(1)+opargs(1);
423 /* Get the address of a symbol into the primary or alternate register (used
424 * for arrays, and for passing arguments by reference).
426 SC_FUNC void address(symbol *sym,regid reg)
429 assert(reg==sPRI || reg==sALT);
430 /* the symbol can be a local array, a global array, or an array
431 * that is passed by reference.
433 if (sym->ident==iREFARRAY || sym->ident==iREFERENCE) {
434 /* reference to a variable or to an array; currently this is
435 * always a local variable */
438 stgwrite("\tload.s.pri ");
441 stgwrite("\tload.s.alt ");
445 /* a local array or local variable */
448 if (sym->vclass==sLOCAL)
449 stgwrite("\taddr.pri ");
451 stgwrite("\tconst.pri ");
454 if (sym->vclass==sLOCAL)
455 stgwrite("\taddr.alt ");
457 stgwrite("\tconst.alt ");
461 outval(sym->addr,TRUE);
462 markusage(sym,uREAD);
463 code_idx+=opcodes(1)+opargs(1);
468 * Saves the contents of "primary" into a memory cell, either directly
469 * or indirectly (at the address given in the alternate register).
471 SC_FUNC void store(value *lval)
476 if (lval->ident==iARRAYCELL) {
477 /* store at address in ALT */
478 stgwrite("\tstor.i\n");
479 code_idx+=opcodes(1);
480 } else if (lval->ident==iARRAYCHAR) {
481 /* store at address in ALT */
482 stgwrite("\tstrb.i ");
483 outval(sCHARBITS/8,TRUE); /* write one or two bytes */
484 code_idx+=opcodes(1)+opargs(1);
485 } else if (lval->ident==iREFERENCE) {
487 if (sym->vclass==sLOCAL)
488 stgwrite("\tsref.s.pri ");
490 stgwrite("\tsref.pri ");
491 outval(sym->addr,TRUE);
492 code_idx+=opcodes(1)+opargs(1);
495 markusage(sym,uWRITTEN);
496 if (sym->vclass==sLOCAL)
497 stgwrite("\tstor.s.pri ");
499 stgwrite("\tstor.pri ");
500 outval(sym->addr,TRUE);
501 code_idx+=opcodes(1)+opargs(1);
505 /* Get a cell from a fixed address in memory */
506 SC_FUNC void loadreg(cell address,regid reg)
508 assert(reg==sPRI || reg==sALT);
510 stgwrite("\tload.pri ");
512 stgwrite("\tload.alt ");
513 outval(address,TRUE);
514 code_idx+=opcodes(1)+opargs(1);
517 /* Store a cell into a fixed address in memory */
518 SC_FUNC void storereg(cell address,regid reg)
520 assert(reg==sPRI || reg==sALT);
522 stgwrite("\tstor.pri ");
524 stgwrite("\tstor.alt ");
525 outval(address,TRUE);
526 code_idx+=opcodes(1)+opargs(1);
529 /* source must in PRI, destination address in ALT. The "size"
530 * parameter is in bytes, not cells.
532 SC_FUNC void memcopy(cell size)
537 code_idx+=opcodes(1)+opargs(1);
540 /* Address of the source must already have been loaded in PRI
541 * "size" is the size in bytes (not cells).
543 SC_FUNC void copyarray(symbol *sym,cell size)
546 /* the symbol can be a local array, a global array, or an array
547 * that is passed by reference.
549 if (sym->ident==iREFARRAY) {
550 /* reference to an array; currently this is always a local variable */
551 assert(sym->vclass==sLOCAL); /* symbol must be stack relative */
552 stgwrite("\tload.s.alt ");
554 /* a local or global array */
555 if (sym->vclass==sLOCAL)
556 stgwrite("\taddr.alt ");
558 stgwrite("\tconst.alt ");
560 outval(sym->addr,TRUE);
561 markusage(sym,uWRITTEN);
563 code_idx+=opcodes(1)+opargs(1);
567 SC_FUNC void fillarray(symbol *sym,cell size,cell value)
569 ldconst(value,sPRI); /* load value in PRI */
572 /* the symbol can be a local array, a global array, or an array
573 * that is passed by reference.
575 if (sym->ident==iREFARRAY) {
576 /* reference to an array; currently this is always a local variable */
577 assert(sym->vclass==sLOCAL); /* symbol must be stack relative */
578 stgwrite("\tload.s.alt ");
580 /* a local or global array */
581 if (sym->vclass==sLOCAL)
582 stgwrite("\taddr.alt ");
584 stgwrite("\tconst.alt ");
586 outval(sym->addr,TRUE);
587 markusage(sym,uWRITTEN);
593 code_idx+=opcodes(2)+opargs(2);
596 SC_FUNC void stradjust(regid reg)
599 stgwrite("\tstradjust.pri\n");
600 code_idx+=opcodes(1);
603 /* Instruction to get an immediate value into the primary or the alternate
606 SC_FUNC void ldconst(cell val,regid reg)
608 assert(reg==sPRI || reg==sALT);
612 stgwrite("\tzero.pri\n");
613 code_idx+=opcodes(1);
615 stgwrite("\tconst.pri ");
617 code_idx+=opcodes(1)+opargs(1);
622 stgwrite("\tzero.alt\n");
623 code_idx+=opcodes(1);
625 stgwrite("\tconst.alt ");
627 code_idx+=opcodes(1)+opargs(1);
633 /* Copy value in alternate register to the primary register */
634 SC_FUNC void moveto1(void)
636 stgwrite("\tmove.pri\n");
637 code_idx+=opcodes(1)+opargs(0);
640 /* Push primary or the alternate register onto the stack
642 SC_FUNC void pushreg(regid reg)
644 assert(reg==sPRI || reg==sALT);
647 stgwrite("\tpush.pri\n");
650 stgwrite("\tpush.alt\n");
653 code_idx+=opcodes(1);
657 * Push a constant value onto the stack
659 SC_FUNC void pushval(cell val)
661 stgwrite("\tpush.c ");
663 code_idx+=opcodes(1)+opargs(1);
666 /* Pop stack into the primary or the alternate register
668 SC_FUNC void popreg(regid reg)
670 assert(reg==sPRI || reg==sALT);
673 stgwrite("\tpop.pri\n");
676 stgwrite("\tpop.alt\n");
679 code_idx+=opcodes(1);
685 SC_FUNC void genarray(int dims, int _autozero)
688 stgwrite("\tgenarray.z ");
690 stgwrite("\tgenarray ");
693 code_idx+=opcodes(1)+opargs(1);
697 * swap the top-of-stack with the value in primary register
699 SC_FUNC void swap1(void)
701 stgwrite("\tswap.pri\n");
702 code_idx+=opcodes(1);
706 * The "switch" statement generates a "case" table using the "CASE" opcode.
707 * The case table contains a list of records, each record holds a comparison
708 * value and a label to branch to on a match. The very first record is an
709 * exception: it holds the size of the table (excluding the first record) and
710 * the label to branch to when none of the values in the case table match.
711 * The case table is sorted on the comparison value. This allows more advanced
712 * abstract machines to sift the case table with a binary search.
714 SC_FUNC void ffswitch(int label)
716 stgwrite("\tswitch ");
717 outval(label,TRUE); /* the label is the address of the case table */
718 code_idx+=opcodes(1)+opargs(1);
721 SC_FUNC void ffcase(cell value,char *labelname,int newtable)
724 stgwrite("\tcasetbl\n");
725 code_idx+=opcodes(1);
732 code_idx+=opcodes(0)+opargs(2);
736 * Call specified function
738 SC_FUNC void ffcall(symbol *sym,const char *label,int numargs)
740 char symname[2*sNAMEMAX+16];
743 assert(sym->ident==iFUNCTN);
745 funcdisplayname(symname,sym->name);
746 if ((sym->usage & uNATIVE)!=0) {
747 /* reserve a SYSREQ id if called for the first time */
749 if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0)
750 sym->addr=ntv_funcid++;
751 stgwrite("\tsysreq.c ");
752 outval(sym->addr,FALSE);
757 stgwrite("\n"); /* write on a separate line, to mark a sequence point for the peephole optimizer */
758 stgwrite("\tstack ");
759 outval((numargs+1)*sizeof(cell), TRUE);
760 code_idx+=opcodes(2)+opargs(2);
762 /* normal function */
771 && (label!=NULL || !isalpha(sym->name[0]) && sym->name[0]!='_' && sym->name[0]!=sc_ctrlchar))
777 code_idx+=opcodes(1)+opargs(1);
781 /* Return from function
783 * Global references: funcstatus (referred to only)
785 SC_FUNC void ffret(int remparams)
788 stgwrite("\tretn\n");
791 code_idx+=opcodes(1);
794 SC_FUNC void ffabort(int reason)
798 code_idx+=opcodes(1)+opargs(1);
801 SC_FUNC void ffbounds(cell size)
803 if ((sc_debug & sCHKBOUNDS)!=0) {
804 stgwrite("\tbounds ");
806 code_idx+=opcodes(1)+opargs(1);
811 * Jump to local label number (the number is converted to a name)
813 SC_FUNC void jumplabel(int number)
817 code_idx+=opcodes(1)+opargs(1);
821 * Define storage (global and static variables)
823 SC_FUNC void defstorage(void)
829 * Inclrement/decrement stack pointer. Note that this routine does
830 * nothing if the delta is zero.
832 SC_FUNC void modstk(int delta)
835 stgwrite("\tstack ");
837 code_idx+=opcodes(1)+opargs(1);
841 /* set the stack to a hard offset from the frame */
842 SC_FUNC void setstk(cell value)
844 stgwrite("\tlctrl 5\n"); /* get FRM in PRI */
845 assert(value<=0); /* STK should always become <= FRM */
847 stgwrite("\tadd.c ");
848 outval(value, TRUE); /* add (negative) offset */
849 code_idx+=opcodes(1)+opargs(1);
850 // ??? write zeros in the space between STK and the value in PRI (the new stk)
851 // get value of STK in ALT
853 // need new FILL opcode that takes a variable size
855 stgwrite("\tsctrl 4\n"); /* store in STK */
856 code_idx+=opcodes(2)+opargs(2);
859 SC_FUNC void modheap(int delta)
864 code_idx+=opcodes(1)+opargs(1);
868 SC_FUNC void modheap_i()
870 stgwrite("\ttracker.pop.setheap\n");
871 code_idx+=opcodes(1);
874 SC_FUNC void setheap_save(cell value)
877 stgwrite("\ttracker.push.c ");
879 code_idx+=opcodes(1)+opargs(1);
882 SC_FUNC void setheap_pri(void)
884 stgwrite("\theap "); /* ALT = HEA++ */
885 outval(sizeof(cell), TRUE);
886 stgwrite("\tstor.i\n"); /* store PRI (default value) at address ALT */
887 stgwrite("\tmove.pri\n"); /* move ALT to PRI: PRI contains the address */
888 code_idx+=opcodes(3)+opargs(1);
891 SC_FUNC void setheap(cell value)
893 stgwrite("\tconst.pri "); /* load default value in PRI */
895 code_idx+=opcodes(1)+opargs(1);
900 * Convert a cell number to a "byte" address; i.e. double or quadruple
901 * the primary register.
903 SC_FUNC void cell2addr(void)
905 #if PAWN_CELL_SIZE==16
906 stgwrite("\tshl.c.pri 1\n");
907 #elif PAWN_CELL_SIZE==32
908 stgwrite("\tshl.c.pri 2\n");
909 #elif PAWN_CELL_SIZE==64
910 stgwrite("\tshl.c.pri 3\n");
912 #error Unsupported cell size
914 code_idx+=opcodes(1)+opargs(1);
918 * Double or quadruple the alternate register.
920 SC_FUNC void cell2addr_alt(void)
922 #if PAWN_CELL_SIZE==16
923 stgwrite("\tshl.c.alt 1\n");
924 #elif PAWN_CELL_SIZE==32
925 stgwrite("\tshl.c.alt 2\n");
926 #elif PAWN_CELL_SIZE==64
927 stgwrite("\tshl.c.alt 3\n");
929 #error Unsupported cell size
931 code_idx+=opcodes(1)+opargs(1);
935 * Convert "distance of addresses" to "number of cells" in between.
936 * Or convert a number of packed characters to the number of cells (with
939 SC_FUNC void addr2cell(void)
941 #if PAWN_CELL_SIZE==16
942 stgwrite("\tshr.c.pri 1\n");
943 #elif PAWN_CELL_SIZE==32
944 stgwrite("\tshr.c.pri 2\n");
945 #elif PAWN_CELL_SIZE==64
946 stgwrite("\tshr.c.pri 3\n");
948 #error Unsupported cell size
950 code_idx+=opcodes(1)+opargs(1);
953 /* Convert from character index to byte address. This routine does
954 * nothing if a character has the size of a byte.
956 SC_FUNC void char2addr(void)
959 stgwrite("\tshl.c.pri 1\n");
960 code_idx+=opcodes(1)+opargs(1);
964 /* Align PRI (which should hold a character index) to an address.
965 * The first character in a "pack" occupies the highest bits of
966 * the cell. This is at the lower memory address on Big Endian
967 * computers and on the higher address on Little Endian computers.
968 * The ALIGN.pri/alt instructions must solve this machine dependence;
969 * that is, on Big Endian computers, ALIGN.pri/alt shuold do nothing
970 * and on Little Endian computers they should toggle the address.
972 * NOTE: For Source Pawn, this is fliped. It will do nothing on Little-Endian.
974 SC_FUNC void charalign(void)
976 #if 0 /* TEMPORARILY DISABLED BECAUSE WE DON'T USE BIG ENDIAN */
977 stgwrite("\talign.pri ");
978 outval(sCHARBITS/8,TRUE);
979 code_idx+=opcodes(1)+opargs(1);
984 * Add a constant to the primary register.
986 SC_FUNC void addconst(cell value)
989 stgwrite("\tadd.c ");
991 code_idx+=opcodes(1)+opargs(1);
996 * signed multiply of primary and secundairy registers (result in primary)
998 SC_FUNC void os_mult(void)
1000 stgwrite("\tsmul\n");
1001 code_idx+=opcodes(1);
1005 * signed divide of alternate register by primary register (quotient in
1006 * primary; remainder in alternate)
1008 SC_FUNC void os_div(void)
1010 stgwrite("\tsdiv.alt\n");
1011 code_idx+=opcodes(1);
1015 * modulus of (alternate % primary), result in primary (signed)
1017 SC_FUNC void os_mod(void)
1019 stgwrite("\tsdiv.alt\n");
1020 stgwrite("\tmove.pri\n"); /* move ALT to PRI */
1021 code_idx+=opcodes(2);
1025 * Add primary and alternate registers (result in primary).
1027 SC_FUNC void ob_add(void)
1029 stgwrite("\tadd\n");
1030 code_idx+=opcodes(1);
1034 * subtract primary register from alternate register (result in primary)
1036 SC_FUNC void ob_sub(void)
1038 stgwrite("\tsub.alt\n");
1039 code_idx+=opcodes(1);
1043 * arithmic shift left alternate register the number of bits
1044 * given in the primary register (result in primary).
1045 * There is no need for a "logical shift left" routine, since
1046 * logical shift left is identical to arithmic shift left.
1048 SC_FUNC void ob_sal(void)
1050 stgwrite("\txchg\n");
1051 stgwrite("\tshl\n");
1052 code_idx+=opcodes(2);
1056 * arithmic shift right alternate register the number of bits
1057 * given in the primary register (result in primary).
1059 SC_FUNC void os_sar(void)
1061 stgwrite("\txchg\n");
1062 stgwrite("\tsshr\n");
1063 code_idx+=opcodes(2);
1067 * logical (unsigned) shift right of the alternate register by the
1068 * number of bits given in the primary register (result in primary).
1070 SC_FUNC void ou_sar(void)
1072 stgwrite("\txchg\n");
1073 stgwrite("\tshr\n");
1074 code_idx+=opcodes(2);
1078 * inclusive "or" of primary and alternate registers (result in primary)
1080 SC_FUNC void ob_or(void)
1083 code_idx+=opcodes(1);
1087 * "exclusive or" of primary and alternate registers (result in primary)
1089 SC_FUNC void ob_xor(void)
1091 stgwrite("\txor\n");
1092 code_idx+=opcodes(1);
1096 * "and" of primary and secundairy registers (result in primary)
1098 SC_FUNC void ob_and(void)
1100 stgwrite("\tand\n");
1101 code_idx+=opcodes(1);
1105 * test ALT==PRI; result in primary register (1 or 0).
1107 SC_FUNC void ob_eq(void)
1110 code_idx+=opcodes(1);
1116 SC_FUNC void ob_ne(void)
1118 stgwrite("\tneq\n");
1119 code_idx+=opcodes(1);
1122 /* The abstract machine defines the relational instructions so that PRI is
1123 * on the left side and ALT on the right side of the operator. For example,
1124 * SLESS sets PRI to either 1 or 0 depending on whether the expression
1125 * "PRI < ALT" is true.
1127 * The compiler generates comparisons with ALT on the left side of the
1128 * relational operator and PRI on the right side. The XCHG instruction
1129 * prefixing the relational operators resets this. We leave it to the
1130 * peephole optimizer to choose more compact instructions where possible.
1133 /* Relational operator prefix for chained relational expressions. The
1134 * "suffix" code restores the stack.
1135 * For chained relational operators, the goal is to keep the comparison
1136 * result "so far" in PRI and the value of the most recent operand in
1137 * ALT, ready for a next comparison.
1138 * The "prefix" instruction pushed the comparison result (PRI) onto the
1139 * stack and moves the value of ALT into PRI. If there is a next comparison,
1140 * PRI can now serve as the "left" operand of the relational operator.
1142 SC_FUNC void relop_prefix(void)
1144 stgwrite("\tpush.pri\n");
1145 stgwrite("\tmove.pri\n");
1146 code_idx+=opcodes(2);
1149 SC_FUNC void relop_suffix(void)
1151 stgwrite("\tswap.alt\n");
1152 stgwrite("\tand\n");
1153 stgwrite("\tpop.alt\n");
1154 code_idx+=opcodes(3);
1158 * test ALT<PRI (signed)
1160 SC_FUNC void os_lt(void)
1162 stgwrite("\txchg\n");
1163 stgwrite("\tsless\n");
1164 code_idx+=opcodes(2);
1168 * test ALT<=PRI (signed)
1170 SC_FUNC void os_le(void)
1172 stgwrite("\txchg\n");
1173 stgwrite("\tsleq\n");
1174 code_idx+=opcodes(2);
1178 * test ALT>PRI (signed)
1180 SC_FUNC void os_gt(void)
1182 stgwrite("\txchg\n");
1183 stgwrite("\tsgrtr\n");
1184 code_idx+=opcodes(2);
1188 * test ALT>=PRI (signed)
1190 SC_FUNC void os_ge(void)
1192 stgwrite("\txchg\n");
1193 stgwrite("\tsgeq\n");
1194 code_idx+=opcodes(2);
1198 * logical negation of primary register
1200 SC_FUNC void lneg(void)
1202 stgwrite("\tnot\n");
1203 code_idx+=opcodes(1);
1207 * two's complement primary register
1209 SC_FUNC void neg(void)
1211 stgwrite("\tneg\n");
1212 code_idx+=opcodes(1);
1216 * one's complement of primary register
1218 SC_FUNC void invert(void)
1220 stgwrite("\tinvert\n");
1221 code_idx+=opcodes(1);
1227 SC_FUNC void nooperation(void)
1229 stgwrite("\tnop\n");
1230 code_idx+=opcodes(1);
1236 SC_FUNC void inc(value *lval)
1241 if (lval->ident==iARRAYCELL) {
1242 /* indirect increment, address already in PRI */
1243 stgwrite("\tinc.i\n");
1244 code_idx+=opcodes(1);
1245 } else if (lval->ident==iARRAYCHAR) {
1246 /* indirect increment of single character, address already in PRI */
1247 stgwrite("\tpush.pri\n");
1248 stgwrite("\tpush.alt\n");
1249 stgwrite("\tmove.alt\n"); /* copy address */
1250 stgwrite("\tlodb.i "); /* read from PRI into PRI */
1251 outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1252 stgwrite("\tinc.pri\n");
1253 stgwrite("\tstrb.i "); /* write PRI to ALT */
1254 outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1255 stgwrite("\tpop.alt\n");
1256 stgwrite("\tpop.pri\n");
1257 code_idx+=opcodes(8)+opargs(2);
1258 } else if (lval->ident==iREFERENCE) {
1260 stgwrite("\tpush.pri\n");
1261 /* load dereferenced value */
1262 assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1263 if (sym->vclass==sLOCAL)
1264 stgwrite("\tlref.s.pri ");
1266 stgwrite("\tlref.pri ");
1267 outval(sym->addr,TRUE);
1269 stgwrite("\tinc.pri\n");
1270 /* store dereferenced value */
1271 if (sym->vclass==sLOCAL)
1272 stgwrite("\tsref.s.pri ");
1274 stgwrite("\tsref.pri ");
1275 outval(sym->addr,TRUE);
1276 stgwrite("\tpop.pri\n");
1277 code_idx+=opcodes(5)+opargs(2);
1279 /* local or global variable */
1281 if (sym->vclass==sLOCAL)
1282 stgwrite("\tinc.s ");
1285 outval(sym->addr,TRUE);
1286 code_idx+=opcodes(1)+opargs(1);
1292 * in case of an integer pointer, the symbol must be incremented by 2.
1294 SC_FUNC void dec(value *lval)
1299 if (lval->ident==iARRAYCELL) {
1300 /* indirect decrement, address already in PRI */
1301 stgwrite("\tdec.i\n");
1302 code_idx+=opcodes(1);
1303 } else if (lval->ident==iARRAYCHAR) {
1304 /* indirect decrement of single character, address already in PRI */
1305 stgwrite("\tpush.pri\n");
1306 stgwrite("\tpush.alt\n");
1307 stgwrite("\tmove.alt\n"); /* copy address */
1308 stgwrite("\tlodb.i "); /* read from PRI into PRI */
1309 outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1310 stgwrite("\tdec.pri\n");
1311 stgwrite("\tstrb.i "); /* write PRI to ALT */
1312 outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1313 stgwrite("\tpop.alt\n");
1314 stgwrite("\tpop.pri\n");
1315 code_idx+=opcodes(8)+opargs(2);
1316 } else if (lval->ident==iREFERENCE) {
1318 stgwrite("\tpush.pri\n");
1319 /* load dereferenced value */
1320 assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1321 if (sym->vclass==sLOCAL)
1322 stgwrite("\tlref.s.pri ");
1324 stgwrite("\tlref.pri ");
1325 outval(sym->addr,TRUE);
1327 stgwrite("\tdec.pri\n");
1328 /* store dereferenced value */
1329 if (sym->vclass==sLOCAL)
1330 stgwrite("\tsref.s.pri ");
1332 stgwrite("\tsref.pri ");
1333 outval(sym->addr,TRUE);
1334 stgwrite("\tpop.pri\n");
1335 code_idx+=opcodes(5)+opargs(2);
1337 /* local or global variable */
1339 if (sym->vclass==sLOCAL)
1340 stgwrite("\tdec.s ");
1343 outval(sym->addr,TRUE);
1344 code_idx+=opcodes(1)+opargs(1);
1349 * Jumps to "label" if PRI != 0
1351 SC_FUNC void jmp_ne0(int number)
1354 outval(number,TRUE);
1355 code_idx+=opcodes(1)+opargs(1);
1359 * Jumps to "label" if PRI == 0
1361 SC_FUNC void jmp_eq0(int number)
1363 stgwrite("\tjzer ");
1364 outval(number,TRUE);
1365 code_idx+=opcodes(1)+opargs(1);
1368 /* write a value in hexadecimal; optionally adds a newline */
1369 SC_FUNC void outval(cell val,int newline)
1371 stgwrite(itoh(val));