Update CS:GO team score gamedata.
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];
741 char aliasname[sNAMEMAX+1];
745 assert(sym->ident==iFUNCTN);
747 funcdisplayname(symname,sym->name);
748 if ((sym->usage & uNATIVE)!=0) {
749 /* reserve a SYSREQ id if called for the first time */
751 stgwrite("\tsysreq.c ");
752 if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0)
753 sym->addr=ntv_funcid++;
754 /* Look for an alias */
755 if (lookup_alias(aliasname, sym->name)) {
756 symbol *asym = findglb(aliasname, sGLOBAL);
757 if (asym && asym->ident==iFUNCTN && ((sym->usage & uNATIVE) != 0)) {
759 if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0) {
760 sym->addr=ntv_funcid++;
761 markusage(sym, uREAD);
765 outval(sym->addr,FALSE);
770 stgwrite("\n"); /* write on a separate line, to mark a sequence point for the peephole optimizer */
771 stgwrite("\tstack ");
772 outval((numargs+1)*sizeof(cell), TRUE);
773 code_idx+=opcodes(2)+opargs(2);
775 /* normal function */
784 && (label!=NULL || (!isalpha(sym->name[0]) && sym->name[0]!='_' && sym->name[0]!=sc_ctrlchar)))
790 code_idx+=opcodes(1)+opargs(1);
794 /* Return from function
796 * Global references: funcstatus (referred to only)
798 SC_FUNC void ffret(int remparams)
801 stgwrite("\tretn\n");
804 code_idx+=opcodes(1);
807 SC_FUNC void ffabort(int reason)
811 code_idx+=opcodes(1)+opargs(1);
814 SC_FUNC void ffbounds(cell size)
816 if ((sc_debug & sCHKBOUNDS)!=0) {
817 stgwrite("\tbounds ");
819 code_idx+=opcodes(1)+opargs(1);
824 * Jump to local label number (the number is converted to a name)
826 SC_FUNC void jumplabel(int number)
830 code_idx+=opcodes(1)+opargs(1);
834 * Define storage (global and static variables)
836 SC_FUNC void defstorage(void)
842 * Inclrement/decrement stack pointer. Note that this routine does
843 * nothing if the delta is zero.
845 SC_FUNC void modstk(int delta)
848 stgwrite("\tstack ");
850 code_idx+=opcodes(1)+opargs(1);
854 /* set the stack to a hard offset from the frame */
855 SC_FUNC void setstk(cell value)
857 stgwrite("\tstackadjust ");
858 assert(value<=0); /* STK should always become <= FRM */
859 outval(value, TRUE); /* add (negative) offset */
860 code_idx+=opcodes(1)+opargs(1);
863 SC_FUNC void modheap(int delta)
868 code_idx+=opcodes(1)+opargs(1);
872 SC_FUNC void modheap_i()
874 stgwrite("\ttracker.pop.setheap\n");
875 code_idx+=opcodes(1);
878 SC_FUNC void setheap_save(cell value)
881 stgwrite("\ttracker.push.c ");
883 code_idx+=opcodes(1)+opargs(1);
886 SC_FUNC void setheap_pri(void)
888 stgwrite("\theap "); /* ALT = HEA++ */
889 outval(sizeof(cell), TRUE);
890 stgwrite("\tstor.i\n"); /* store PRI (default value) at address ALT */
891 stgwrite("\tmove.pri\n"); /* move ALT to PRI: PRI contains the address */
892 code_idx+=opcodes(3)+opargs(1);
895 SC_FUNC void setheap(cell value)
897 stgwrite("\tconst.pri "); /* load default value in PRI */
899 code_idx+=opcodes(1)+opargs(1);
904 * Convert a cell number to a "byte" address; i.e. double or quadruple
905 * the primary register.
907 SC_FUNC void cell2addr(void)
909 #if PAWN_CELL_SIZE==16
910 stgwrite("\tshl.c.pri 1\n");
911 #elif PAWN_CELL_SIZE==32
912 stgwrite("\tshl.c.pri 2\n");
913 #elif PAWN_CELL_SIZE==64
914 stgwrite("\tshl.c.pri 3\n");
916 #error Unsupported cell size
918 code_idx+=opcodes(1)+opargs(1);
922 * Double or quadruple the alternate register.
924 SC_FUNC void cell2addr_alt(void)
926 #if PAWN_CELL_SIZE==16
927 stgwrite("\tshl.c.alt 1\n");
928 #elif PAWN_CELL_SIZE==32
929 stgwrite("\tshl.c.alt 2\n");
930 #elif PAWN_CELL_SIZE==64
931 stgwrite("\tshl.c.alt 3\n");
933 #error Unsupported cell size
935 code_idx+=opcodes(1)+opargs(1);
939 * Convert "distance of addresses" to "number of cells" in between.
940 * Or convert a number of packed characters to the number of cells (with
943 SC_FUNC void addr2cell(void)
945 #if PAWN_CELL_SIZE==16
946 stgwrite("\tshr.c.pri 1\n");
947 #elif PAWN_CELL_SIZE==32
948 stgwrite("\tshr.c.pri 2\n");
949 #elif PAWN_CELL_SIZE==64
950 stgwrite("\tshr.c.pri 3\n");
952 #error Unsupported cell size
954 code_idx+=opcodes(1)+opargs(1);
957 /* Convert from character index to byte address. This routine does
958 * nothing if a character has the size of a byte.
960 SC_FUNC void char2addr(void)
963 stgwrite("\tshl.c.pri 1\n");
964 code_idx+=opcodes(1)+opargs(1);
968 /* Align PRI (which should hold a character index) to an address.
969 * The first character in a "pack" occupies the highest bits of
970 * the cell. This is at the lower memory address on Big Endian
971 * computers and on the higher address on Little Endian computers.
972 * The ALIGN.pri/alt instructions must solve this machine dependence;
973 * that is, on Big Endian computers, ALIGN.pri/alt shuold do nothing
974 * and on Little Endian computers they should toggle the address.
976 * NOTE: For Source Pawn, this is fliped. It will do nothing on Little-Endian.
978 SC_FUNC void charalign(void)
980 #if 0 /* TEMPORARILY DISABLED BECAUSE WE DON'T USE BIG ENDIAN */
981 stgwrite("\talign.pri ");
982 outval(sCHARBITS/8,TRUE);
983 code_idx+=opcodes(1)+opargs(1);
988 * Add a constant to the primary register.
990 SC_FUNC void addconst(cell value)
993 stgwrite("\tadd.c ");
995 code_idx+=opcodes(1)+opargs(1);
1000 * signed multiply of primary and secundairy registers (result in primary)
1002 SC_FUNC void os_mult(void)
1004 stgwrite("\tsmul\n");
1005 code_idx+=opcodes(1);
1009 * signed divide of alternate register by primary register (quotient in
1010 * primary; remainder in alternate)
1012 SC_FUNC void os_div(void)
1014 stgwrite("\tsdiv.alt\n");
1015 code_idx+=opcodes(1);
1019 * modulus of (alternate % primary), result in primary (signed)
1021 SC_FUNC void os_mod(void)
1023 stgwrite("\tsdiv.alt\n");
1024 stgwrite("\tmove.pri\n"); /* move ALT to PRI */
1025 code_idx+=opcodes(2);
1029 * Add primary and alternate registers (result in primary).
1031 SC_FUNC void ob_add(void)
1033 stgwrite("\tadd\n");
1034 code_idx+=opcodes(1);
1038 * subtract primary register from alternate register (result in primary)
1040 SC_FUNC void ob_sub(void)
1042 stgwrite("\tsub.alt\n");
1043 code_idx+=opcodes(1);
1047 * arithmic shift left alternate register the number of bits
1048 * given in the primary register (result in primary).
1049 * There is no need for a "logical shift left" routine, since
1050 * logical shift left is identical to arithmic shift left.
1052 SC_FUNC void ob_sal(void)
1054 stgwrite("\txchg\n");
1055 stgwrite("\tshl\n");
1056 code_idx+=opcodes(2);
1060 * arithmic shift right alternate register the number of bits
1061 * given in the primary register (result in primary).
1063 SC_FUNC void os_sar(void)
1065 stgwrite("\txchg\n");
1066 stgwrite("\tsshr\n");
1067 code_idx+=opcodes(2);
1071 * logical (unsigned) shift right of the alternate register by the
1072 * number of bits given in the primary register (result in primary).
1074 SC_FUNC void ou_sar(void)
1076 stgwrite("\txchg\n");
1077 stgwrite("\tshr\n");
1078 code_idx+=opcodes(2);
1082 * inclusive "or" of primary and alternate registers (result in primary)
1084 SC_FUNC void ob_or(void)
1087 code_idx+=opcodes(1);
1091 * "exclusive or" of primary and alternate registers (result in primary)
1093 SC_FUNC void ob_xor(void)
1095 stgwrite("\txor\n");
1096 code_idx+=opcodes(1);
1100 * "and" of primary and secundairy registers (result in primary)
1102 SC_FUNC void ob_and(void)
1104 stgwrite("\tand\n");
1105 code_idx+=opcodes(1);
1109 * test ALT==PRI; result in primary register (1 or 0).
1111 SC_FUNC void ob_eq(void)
1114 code_idx+=opcodes(1);
1120 SC_FUNC void ob_ne(void)
1122 stgwrite("\tneq\n");
1123 code_idx+=opcodes(1);
1126 /* The abstract machine defines the relational instructions so that PRI is
1127 * on the left side and ALT on the right side of the operator. For example,
1128 * SLESS sets PRI to either 1 or 0 depending on whether the expression
1129 * "PRI < ALT" is true.
1131 * The compiler generates comparisons with ALT on the left side of the
1132 * relational operator and PRI on the right side. The XCHG instruction
1133 * prefixing the relational operators resets this. We leave it to the
1134 * peephole optimizer to choose more compact instructions where possible.
1137 /* Relational operator prefix for chained relational expressions. The
1138 * "suffix" code restores the stack.
1139 * For chained relational operators, the goal is to keep the comparison
1140 * result "so far" in PRI and the value of the most recent operand in
1141 * ALT, ready for a next comparison.
1142 * The "prefix" instruction pushed the comparison result (PRI) onto the
1143 * stack and moves the value of ALT into PRI. If there is a next comparison,
1144 * PRI can now serve as the "left" operand of the relational operator.
1146 SC_FUNC void relop_prefix(void)
1148 stgwrite("\tpush.pri\n");
1149 stgwrite("\tmove.pri\n");
1150 code_idx+=opcodes(2);
1153 SC_FUNC void relop_suffix(void)
1155 stgwrite("\tswap.alt\n");
1156 stgwrite("\tand\n");
1157 stgwrite("\tpop.alt\n");
1158 code_idx+=opcodes(3);
1162 * test ALT<PRI (signed)
1164 SC_FUNC void os_lt(void)
1166 stgwrite("\txchg\n");
1167 stgwrite("\tsless\n");
1168 code_idx+=opcodes(2);
1172 * test ALT<=PRI (signed)
1174 SC_FUNC void os_le(void)
1176 stgwrite("\txchg\n");
1177 stgwrite("\tsleq\n");
1178 code_idx+=opcodes(2);
1182 * test ALT>PRI (signed)
1184 SC_FUNC void os_gt(void)
1186 stgwrite("\txchg\n");
1187 stgwrite("\tsgrtr\n");
1188 code_idx+=opcodes(2);
1192 * test ALT>=PRI (signed)
1194 SC_FUNC void os_ge(void)
1196 stgwrite("\txchg\n");
1197 stgwrite("\tsgeq\n");
1198 code_idx+=opcodes(2);
1202 * logical negation of primary register
1204 SC_FUNC void lneg(void)
1206 stgwrite("\tnot\n");
1207 code_idx+=opcodes(1);
1211 * two's complement primary register
1213 SC_FUNC void neg(void)
1215 stgwrite("\tneg\n");
1216 code_idx+=opcodes(1);
1220 * one's complement of primary register
1222 SC_FUNC void invert(void)
1224 stgwrite("\tinvert\n");
1225 code_idx+=opcodes(1);
1231 SC_FUNC void nooperation(void)
1233 stgwrite("\tnop\n");
1234 code_idx+=opcodes(1);
1240 SC_FUNC void inc(value *lval)
1245 if (lval->ident==iARRAYCELL) {
1246 /* indirect increment, address already in PRI */
1247 stgwrite("\tinc.i\n");
1248 code_idx+=opcodes(1);
1249 } else if (lval->ident==iARRAYCHAR) {
1250 /* indirect increment of single character, address already in PRI */
1251 stgwrite("\tpush.pri\n");
1252 stgwrite("\tpush.alt\n");
1253 stgwrite("\tmove.alt\n"); /* copy address */
1254 stgwrite("\tlodb.i "); /* read from PRI into PRI */
1255 outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1256 stgwrite("\tinc.pri\n");
1257 stgwrite("\tstrb.i "); /* write PRI to ALT */
1258 outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1259 stgwrite("\tpop.alt\n");
1260 stgwrite("\tpop.pri\n");
1261 code_idx+=opcodes(8)+opargs(2);
1262 } else if (lval->ident==iREFERENCE) {
1264 stgwrite("\tpush.pri\n");
1265 /* load dereferenced value */
1266 assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1267 if (sym->vclass==sLOCAL)
1268 stgwrite("\tlref.s.pri ");
1270 stgwrite("\tlref.pri ");
1271 outval(sym->addr,TRUE);
1273 stgwrite("\tinc.pri\n");
1274 /* store dereferenced value */
1275 if (sym->vclass==sLOCAL)
1276 stgwrite("\tsref.s.pri ");
1278 stgwrite("\tsref.pri ");
1279 outval(sym->addr,TRUE);
1280 stgwrite("\tpop.pri\n");
1281 code_idx+=opcodes(5)+opargs(2);
1283 /* local or global variable */
1285 if (sym->vclass==sLOCAL)
1286 stgwrite("\tinc.s ");
1289 outval(sym->addr,TRUE);
1290 code_idx+=opcodes(1)+opargs(1);
1296 * in case of an integer pointer, the symbol must be incremented by 2.
1298 SC_FUNC void dec(value *lval)
1303 if (lval->ident==iARRAYCELL) {
1304 /* indirect decrement, address already in PRI */
1305 stgwrite("\tdec.i\n");
1306 code_idx+=opcodes(1);
1307 } else if (lval->ident==iARRAYCHAR) {
1308 /* indirect decrement of single character, address already in PRI */
1309 stgwrite("\tpush.pri\n");
1310 stgwrite("\tpush.alt\n");
1311 stgwrite("\tmove.alt\n"); /* copy address */
1312 stgwrite("\tlodb.i "); /* read from PRI into PRI */
1313 outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1314 stgwrite("\tdec.pri\n");
1315 stgwrite("\tstrb.i "); /* write PRI to ALT */
1316 outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1317 stgwrite("\tpop.alt\n");
1318 stgwrite("\tpop.pri\n");
1319 code_idx+=opcodes(8)+opargs(2);
1320 } else if (lval->ident==iREFERENCE) {
1322 stgwrite("\tpush.pri\n");
1323 /* load dereferenced value */
1324 assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1325 if (sym->vclass==sLOCAL)
1326 stgwrite("\tlref.s.pri ");
1328 stgwrite("\tlref.pri ");
1329 outval(sym->addr,TRUE);
1331 stgwrite("\tdec.pri\n");
1332 /* store dereferenced value */
1333 if (sym->vclass==sLOCAL)
1334 stgwrite("\tsref.s.pri ");
1336 stgwrite("\tsref.pri ");
1337 outval(sym->addr,TRUE);
1338 stgwrite("\tpop.pri\n");
1339 code_idx+=opcodes(5)+opargs(2);
1341 /* local or global variable */
1343 if (sym->vclass==sLOCAL)
1344 stgwrite("\tdec.s ");
1347 outval(sym->addr,TRUE);
1348 code_idx+=opcodes(1)+opargs(1);
1353 * Jumps to "label" if PRI != 0
1355 SC_FUNC void jmp_ne0(int number)
1358 outval(number,TRUE);
1359 code_idx+=opcodes(1)+opargs(1);
1363 * Jumps to "label" if PRI == 0
1365 SC_FUNC void jmp_eq0(int number)
1367 stgwrite("\tjzer ");
1368 outval(number,TRUE);
1369 code_idx+=opcodes(1)+opargs(1);
1372 /* write a value in hexadecimal; optionally adds a newline */
1373 SC_FUNC void outval(cell val,int newline)
1375 stgwrite(itoh(val));