1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcepawn/compiler/sc4.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,1378 @@
1.4 +/* Pawn compiler - code generation (unoptimized "assembler" code)
1.5 + *
1.6 + * Copyright (c) ITB CompuPhase, 1997-2006
1.7 + *
1.8 + * This software is provided "as-is", without any express or implied warranty.
1.9 + * In no event will the authors be held liable for any damages arising from
1.10 + * the use of this software.
1.11 + *
1.12 + * Permission is granted to anyone to use this software for any purpose,
1.13 + * including commercial applications, and to alter it and redistribute it
1.14 + * freely, subject to the following restrictions:
1.15 + *
1.16 + * 1. The origin of this software must not be misrepresented; you must not
1.17 + * claim that you wrote the original software. If you use this software in
1.18 + * a product, an acknowledgment in the product documentation would be
1.19 + * appreciated but is not required.
1.20 + * 2. Altered source versions must be plainly marked as such, and must not be
1.21 + * misrepresented as being the original software.
1.22 + * 3. This notice may not be removed or altered from any source distribution.
1.23 + *
1.24 + * Version: $Id$
1.25 + */
1.26 +#include <assert.h>
1.27 +#include <ctype.h>
1.28 +#include <stdio.h>
1.29 +#include <stdlib.h> /* for _MAX_PATH */
1.30 +#include <string.h>
1.31 +#if defined FORTIFY
1.32 + #include <alloc/fortify.h>
1.33 +#endif
1.34 +#include "sc.h"
1.35 +
1.36 +static int fcurseg; /* the file number (fcurrent) for the active segment */
1.37 +
1.38 +
1.39 +/* When a subroutine returns to address 0, the AMX must halt. In earlier
1.40 + * releases, the RET and RETN opcodes checked for the special case 0 address.
1.41 + * Today, the compiler simply generates a HALT instruction at address 0. So
1.42 + * a subroutine can savely return to 0, and then encounter a HALT.
1.43 + */
1.44 +SC_FUNC void writeleader(symbol *root)
1.45 +{
1.46 + int lbl_nostate,lbl_table;
1.47 + int statecount;
1.48 + symbol *sym;
1.49 + constvalue *fsa, *state, *stlist;
1.50 + int fsa_id,listid;
1.51 + char lbl_default[sNAMEMAX+1];
1.52 +
1.53 + assert(code_idx==0);
1.54 +
1.55 + begcseg();
1.56 + stgwrite(";program exit point\n");
1.57 + stgwrite("\thalt 0\n\n");
1.58 + code_idx+=opcodes(1)+opargs(1); /* calculate code length */
1.59 +
1.60 + /* check whether there are any functions that have states */
1.61 + for (sym=root->next; sym!=NULL; sym=sym->next)
1.62 + if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL)
1.63 + break;
1.64 + if (sym==NULL)
1.65 + return; /* no function has states, nothing to do next */
1.66 +
1.67 + /* generate an error function that is called for an undefined state */
1.68 + stgwrite("\n;exit point for functions called from the wrong state\n");
1.69 + lbl_nostate=getlabel();
1.70 + setlabel(lbl_nostate);
1.71 + stgwrite("\thalt ");
1.72 + outval(AMX_ERR_INVSTATE,TRUE);
1.73 + code_idx+=opcodes(1)+opargs(1); /* calculate code length */
1.74 +
1.75 + /* write the "state-selectors" table with all automatons (update the
1.76 + * automatons structure too, as we are now assigning the address to
1.77 + * each automaton state-selector variable)
1.78 + */
1.79 + assert(glb_declared==0);
1.80 + begdseg();
1.81 + for (fsa=sc_automaton_tab.next; fsa!=NULL; fsa=fsa->next) {
1.82 + defstorage();
1.83 + stgwrite("0\t; automaton ");
1.84 + if (strlen(fsa->name)==0)
1.85 + stgwrite("(anonymous)");
1.86 + else
1.87 + stgwrite(fsa->name);
1.88 + stgwrite("\n");
1.89 + fsa->value=glb_declared*sizeof(cell);
1.90 + glb_declared++;
1.91 + } /* for */
1.92 +
1.93 + /* write stubs and jump tables for all state functions */
1.94 + begcseg();
1.95 + for (sym=root->next; sym!=NULL; sym=sym->next) {
1.96 + if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL) {
1.97 + stlist=sym->states->next;
1.98 + assert(stlist!=NULL); /* there should be at least one state item */
1.99 + listid=stlist->index;
1.100 + assert(listid==-1 || listid>0);
1.101 + if (listid==-1 && stlist->next!=NULL) {
1.102 + /* first index is the "fallback", take the next one (if available) */
1.103 + stlist=stlist->next;
1.104 + listid=stlist->index;
1.105 + } /* if */
1.106 + if (listid==-1) {
1.107 + /* first index is the fallback, there is no second... */
1.108 + strcpy(stlist->name,"0"); /* insert dummy label number */
1.109 + /* this is an error, but we postpone adding the error message until the
1.110 + * function definition
1.111 + */
1.112 + continue;
1.113 + } /* if */
1.114 + /* generate label numbers for all statelist ids */
1.115 + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
1.116 + assert(strlen(stlist->name)==0);
1.117 + strcpy(stlist->name,itoh(getlabel()));
1.118 + } /* for */
1.119 + if (strcmp(sym->name,uENTRYFUNC)==0)
1.120 + continue; /* do not generate stubs for this special function */
1.121 + sym->addr=code_idx; /* fix the function address now */
1.122 + /* get automaton id for this function */
1.123 + assert(listid>0);
1.124 + fsa_id=state_getfsa(listid);
1.125 + assert(fsa_id>=0); /* automaton 0 exists */
1.126 + fsa=automaton_findid(fsa_id);
1.127 + /* count the number of states actually used; at the sane time, check
1.128 + * whether there is a default state function
1.129 + */
1.130 + statecount=0;
1.131 + strcpy(lbl_default,itoh(lbl_nostate));
1.132 + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
1.133 + if (stlist->index==-1) {
1.134 + assert(strlen(stlist->name)<sizeof lbl_default);
1.135 + strcpy(lbl_default,stlist->name);
1.136 + } else {
1.137 + statecount+=state_count(stlist->index);
1.138 + } /* if */
1.139 + } /* for */
1.140 + /* generate a stub entry for the functions */
1.141 + stgwrite("\tload.pri ");
1.142 + outval(fsa->value,FALSE);
1.143 + stgwrite("\t; ");
1.144 + stgwrite(sym->name);
1.145 + stgwrite("\n");
1.146 + code_idx+=opcodes(1)+opargs(1); /* calculate code length */
1.147 + lbl_table=getlabel();
1.148 + ffswitch(lbl_table);
1.149 + /* generate the jump table */
1.150 + setlabel(lbl_table);
1.151 + ffcase(statecount,lbl_default,TRUE);
1.152 + for (state=sc_state_tab.next; state!=NULL; state=state->next) {
1.153 + if (state->index==fsa_id) {
1.154 + /* find the label for this list id */
1.155 + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
1.156 + if (stlist->index!=-1 && state_inlist(stlist->index,(int)state->value)) {
1.157 + ffcase(state->value,stlist->name,FALSE);
1.158 + break;
1.159 + } /* if */
1.160 + } /* for */
1.161 + if (stlist==NULL && strtol(lbl_default,NULL,16)==lbl_nostate)
1.162 + error(230,state->name,sym->name); /* unimplemented state, no fallback */
1.163 + } /* if (state belongs to automaton of function) */
1.164 + } /* for (state) */
1.165 + stgwrite("\n");
1.166 + } /* if (is function, used & having states) */
1.167 + } /* for (sym) */
1.168 +}
1.169 +
1.170 +/* writetrailer
1.171 + * Not much left of this once important function.
1.172 + *
1.173 + * Global references: pc_stksize (referred to only)
1.174 + * sc_dataalign (referred to only)
1.175 + * code_idx (altered)
1.176 + * glb_declared (altered)
1.177 + */
1.178 +SC_FUNC void writetrailer(void)
1.179 +{
1.180 + assert(sc_dataalign % opcodes(1) == 0); /* alignment must be a multiple of
1.181 + * the opcode size */
1.182 + assert(sc_dataalign!=0);
1.183 +
1.184 + /* pad code to align data segment */
1.185 + if ((code_idx % sc_dataalign)!=0) {
1.186 + begcseg();
1.187 + while ((code_idx % sc_dataalign)!=0)
1.188 + nooperation();
1.189 + } /* if */
1.190 +
1.191 + /* pad data segment to align the stack and the heap */
1.192 + assert(litidx==0); /* literal queue should have been emptied */
1.193 + assert(sc_dataalign % sizeof(cell) == 0);
1.194 + if (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
1.195 + begdseg();
1.196 + defstorage();
1.197 + while (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
1.198 + stgwrite("0 ");
1.199 + glb_declared++;
1.200 + } /* while */
1.201 + } /* if */
1.202 +
1.203 + stgwrite("\nSTKSIZE "); /* write stack size (align stack top) */
1.204 + outval(pc_stksize - (pc_stksize % sc_dataalign), TRUE);
1.205 +}
1.206 +
1.207 +/*
1.208 + * Start (or restart) the CODE segment.
1.209 + *
1.210 + * In fact, the code and data segment specifiers are purely informational;
1.211 + * the "DUMP" instruction itself already specifies that the following values
1.212 + * should go to the data segment. All other instructions go to the code
1.213 + * segment.
1.214 + *
1.215 + * Global references: curseg
1.216 + * fcurrent
1.217 + */
1.218 +SC_FUNC void begcseg(void)
1.219 +{
1.220 + if (sc_status!=statSKIP && (curseg!=sIN_CSEG || fcurrent!=fcurseg)) {
1.221 + stgwrite("\n");
1.222 + stgwrite("CODE ");
1.223 + outval(fcurrent,FALSE);
1.224 + stgwrite("\t; ");
1.225 + outval(code_idx,TRUE);
1.226 + curseg=sIN_CSEG;
1.227 + fcurseg=fcurrent;
1.228 + } /* endif */
1.229 +}
1.230 +
1.231 +/*
1.232 + * Start (or restart) the DATA segment.
1.233 + *
1.234 + * Global references: curseg
1.235 + */
1.236 +SC_FUNC void begdseg(void)
1.237 +{
1.238 + if (sc_status!=statSKIP && (curseg!=sIN_DSEG || fcurrent!=fcurseg)) {
1.239 + stgwrite("\n");
1.240 + stgwrite("DATA ");
1.241 + outval(fcurrent,FALSE);
1.242 + stgwrite("\t; ");
1.243 + outval((glb_declared-litidx)*sizeof(cell),TRUE);
1.244 + curseg=sIN_DSEG;
1.245 + fcurseg=fcurrent;
1.246 + } /* if */
1.247 +}
1.248 +
1.249 +SC_FUNC void setline(int chkbounds)
1.250 +{
1.251 + if (sc_asmfile) {
1.252 + stgwrite("\t; line ");
1.253 + outval(fline,TRUE);
1.254 + } /* if */
1.255 + if ((sc_debug & sSYMBOLIC)!=0 || chkbounds && (sc_debug & sCHKBOUNDS)!=0) {
1.256 + /* generate a "break" (start statement) opcode rather than a "line" opcode
1.257 + * because earlier versions of Small/Pawn have an incompatible version of the
1.258 + * line opcode
1.259 + */
1.260 + stgwrite("\tbreak\t; ");
1.261 + outval(code_idx,TRUE);
1.262 + code_idx+=opcodes(1);
1.263 + } /* if */
1.264 +}
1.265 +
1.266 +SC_FUNC void setfiledirect(char *name)
1.267 +{
1.268 + if (sc_status==statFIRST && sc_listing) {
1.269 + assert(name!=NULL);
1.270 + pc_writeasm(outf,"#file ");
1.271 + pc_writeasm(outf,name);
1.272 + pc_writeasm(outf,"\n");
1.273 + } /* if */
1.274 +}
1.275 +
1.276 +SC_FUNC void setlinedirect(int line)
1.277 +{
1.278 + if (sc_status==statFIRST && sc_listing) {
1.279 + char string[40];
1.280 + sprintf(string,"#line %d\n",line);
1.281 + pc_writeasm(outf,string);
1.282 + } /* if */
1.283 +}
1.284 +
1.285 +/* setlabel
1.286 + *
1.287 + * Post a code label (specified as a number), on a new line.
1.288 + */
1.289 +SC_FUNC void setlabel(int number)
1.290 +{
1.291 + assert(number>=0);
1.292 + stgwrite("l.");
1.293 + stgwrite((char *)itoh(number));
1.294 + /* To assist verification of the assembled code, put the address of the
1.295 + * label as a comment. However, labels that occur inside an expression
1.296 + * may move (through optimization or through re-ordering). So write the
1.297 + * address only if it is known to accurate.
1.298 + */
1.299 + if (!staging) {
1.300 + stgwrite("\t\t; ");
1.301 + outval(code_idx,FALSE);
1.302 + } /* if */
1.303 + stgwrite("\n");
1.304 +}
1.305 +
1.306 +/* Write a token that signifies the start or end of an expression or special
1.307 + * statement. This allows several simple optimizations by the peephole
1.308 + * optimizer.
1.309 + */
1.310 +SC_FUNC void markexpr(optmark type,const char *name,cell offset)
1.311 +{
1.312 + switch (type) {
1.313 + case sEXPR:
1.314 + stgwrite("\t;$exp\n");
1.315 + break;
1.316 + case sPARM:
1.317 + stgwrite("\t;$par\n");
1.318 + break;
1.319 + case sLDECL:
1.320 + assert(name!=NULL);
1.321 + stgwrite("\t;$lcl ");
1.322 + stgwrite(name);
1.323 + stgwrite(" ");
1.324 + outval(offset,TRUE);
1.325 + break;
1.326 + default:
1.327 + assert(0);
1.328 + } /* switch */
1.329 +}
1.330 +
1.331 +/* startfunc - declare a CODE entry point (function start)
1.332 + *
1.333 + * Global references: funcstatus (referred to only)
1.334 + */
1.335 +SC_FUNC void startfunc(char *fname)
1.336 +{
1.337 + stgwrite("\tproc");
1.338 + if (sc_asmfile) {
1.339 + char symname[2*sNAMEMAX+16];
1.340 + funcdisplayname(symname,fname);
1.341 + stgwrite("\t; ");
1.342 + stgwrite(symname);
1.343 + } /* if */
1.344 + stgwrite("\n");
1.345 + code_idx+=opcodes(1);
1.346 +}
1.347 +
1.348 +/* endfunc
1.349 + *
1.350 + * Declare a CODE ending point (function end)
1.351 + */
1.352 +SC_FUNC void endfunc(void)
1.353 +{
1.354 + stgwrite("\n"); /* skip a line */
1.355 +}
1.356 +
1.357 +/* alignframe
1.358 + *
1.359 + * Aligns the frame (and the stack) of the current function to a multiple
1.360 + * of the specified byte count. Two caveats: the alignment ("numbytes") should
1.361 + * be a power of 2, and this alignment must be done right after the frame
1.362 + * is set up (before the first variable is declared)
1.363 + */
1.364 +SC_FUNC void alignframe(int numbytes)
1.365 +{
1.366 + #if !defined NDEBUG
1.367 + /* "numbytes" should be a power of 2 for this code to work */
1.368 + int i,count=0;
1.369 + for (i=0; i<sizeof numbytes*8; i++)
1.370 + if (numbytes & (1 << i))
1.371 + count++;
1.372 + assert(count==1);
1.373 + #endif
1.374 +
1.375 + stgwrite("\tlctrl 4\n"); /* get STK in PRI */
1.376 + stgwrite("\tconst.alt "); /* get ~(numbytes-1) in ALT */
1.377 + outval(~(numbytes-1),TRUE);
1.378 + stgwrite("\tand\n"); /* PRI = STK "and" ~(numbytes-1) */
1.379 + stgwrite("\tsctrl 4\n"); /* set the new value of STK ... */
1.380 + stgwrite("\tsctrl 5\n"); /* ... and FRM */
1.381 + code_idx+=opcodes(5)+opargs(4);
1.382 +}
1.383 +
1.384 +/* rvalue
1.385 + *
1.386 + * Generate code to get the value of a symbol into "primary".
1.387 + */
1.388 +SC_FUNC void rvalue(value *lval)
1.389 +{
1.390 + symbol *sym;
1.391 +
1.392 + sym=lval->sym;
1.393 + if (lval->ident==iARRAYCELL) {
1.394 + /* indirect fetch, address already in PRI */
1.395 + stgwrite("\tload.i\n");
1.396 + code_idx+=opcodes(1);
1.397 + } else if (lval->ident==iARRAYCHAR) {
1.398 + /* indirect fetch of a character from a pack, address already in PRI */
1.399 + stgwrite("\tlodb.i ");
1.400 + outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1.401 + code_idx+=opcodes(1)+opargs(1);
1.402 + } else if (lval->ident==iREFERENCE) {
1.403 + /* indirect fetch, but address not yet in PRI */
1.404 + assert(sym!=NULL);
1.405 + assert(sym->vclass==sLOCAL);/* global references don't exist in Pawn */
1.406 + if (sym->vclass==sLOCAL)
1.407 + stgwrite("\tlref.s.pri ");
1.408 + else
1.409 + stgwrite("\tlref.pri ");
1.410 + outval(sym->addr,TRUE);
1.411 + markusage(sym,uREAD);
1.412 + code_idx+=opcodes(1)+opargs(1);
1.413 + } else {
1.414 + /* direct or stack relative fetch */
1.415 + assert(sym!=NULL);
1.416 + if (sym->vclass==sLOCAL)
1.417 + stgwrite("\tload.s.pri ");
1.418 + else
1.419 + stgwrite("\tload.pri ");
1.420 + outval(sym->addr,TRUE);
1.421 + markusage(sym,uREAD);
1.422 + code_idx+=opcodes(1)+opargs(1);
1.423 + } /* if */
1.424 +}
1.425 +
1.426 +/* Get the address of a symbol into the primary or alternate register (used
1.427 + * for arrays, and for passing arguments by reference).
1.428 + */
1.429 +SC_FUNC void address(symbol *sym,regid reg)
1.430 +{
1.431 + assert(sym!=NULL);
1.432 + assert(reg==sPRI || reg==sALT);
1.433 + /* the symbol can be a local array, a global array, or an array
1.434 + * that is passed by reference.
1.435 + */
1.436 + if (sym->ident==iREFARRAY || sym->ident==iREFERENCE) {
1.437 + /* reference to a variable or to an array; currently this is
1.438 + * always a local variable */
1.439 + switch (reg) {
1.440 + case sPRI:
1.441 + stgwrite("\tload.s.pri ");
1.442 + break;
1.443 + case sALT:
1.444 + stgwrite("\tload.s.alt ");
1.445 + break;
1.446 + } /* switch */
1.447 + } else {
1.448 + /* a local array or local variable */
1.449 + switch (reg) {
1.450 + case sPRI:
1.451 + if (sym->vclass==sLOCAL)
1.452 + stgwrite("\taddr.pri ");
1.453 + else
1.454 + stgwrite("\tconst.pri ");
1.455 + break;
1.456 + case sALT:
1.457 + if (sym->vclass==sLOCAL)
1.458 + stgwrite("\taddr.alt ");
1.459 + else
1.460 + stgwrite("\tconst.alt ");
1.461 + break;
1.462 + } /* switch */
1.463 + } /* if */
1.464 + outval(sym->addr,TRUE);
1.465 + markusage(sym,uREAD);
1.466 + code_idx+=opcodes(1)+opargs(1);
1.467 +}
1.468 +
1.469 +/* store
1.470 + *
1.471 + * Saves the contents of "primary" into a memory cell, either directly
1.472 + * or indirectly (at the address given in the alternate register).
1.473 + */
1.474 +SC_FUNC void store(value *lval)
1.475 +{
1.476 + symbol *sym;
1.477 +
1.478 + sym=lval->sym;
1.479 + if (lval->ident==iARRAYCELL) {
1.480 + /* store at address in ALT */
1.481 + stgwrite("\tstor.i\n");
1.482 + code_idx+=opcodes(1);
1.483 + } else if (lval->ident==iARRAYCHAR) {
1.484 + /* store at address in ALT */
1.485 + stgwrite("\tstrb.i ");
1.486 + outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1.487 + code_idx+=opcodes(1)+opargs(1);
1.488 + } else if (lval->ident==iREFERENCE) {
1.489 + assert(sym!=NULL);
1.490 + if (sym->vclass==sLOCAL)
1.491 + stgwrite("\tsref.s.pri ");
1.492 + else
1.493 + stgwrite("\tsref.pri ");
1.494 + outval(sym->addr,TRUE);
1.495 + code_idx+=opcodes(1)+opargs(1);
1.496 + } else {
1.497 + assert(sym!=NULL);
1.498 + markusage(sym,uWRITTEN);
1.499 + if (sym->vclass==sLOCAL)
1.500 + stgwrite("\tstor.s.pri ");
1.501 + else
1.502 + stgwrite("\tstor.pri ");
1.503 + outval(sym->addr,TRUE);
1.504 + code_idx+=opcodes(1)+opargs(1);
1.505 + } /* if */
1.506 +}
1.507 +
1.508 +/* Get a cell from a fixed address in memory */
1.509 +SC_FUNC void loadreg(cell address,regid reg)
1.510 +{
1.511 + assert(reg==sPRI || reg==sALT);
1.512 + if (reg==sPRI)
1.513 + stgwrite("\tload.pri ");
1.514 + else
1.515 + stgwrite("\tload.alt ");
1.516 + outval(address,TRUE);
1.517 + code_idx+=opcodes(1)+opargs(1);
1.518 +}
1.519 +
1.520 +/* Store a cell into a fixed address in memory */
1.521 +SC_FUNC void storereg(cell address,regid reg)
1.522 +{
1.523 + assert(reg==sPRI || reg==sALT);
1.524 + if (reg==sPRI)
1.525 + stgwrite("\tstor.pri ");
1.526 + else
1.527 + stgwrite("\tstor.alt ");
1.528 + outval(address,TRUE);
1.529 + code_idx+=opcodes(1)+opargs(1);
1.530 +}
1.531 +
1.532 +/* source must in PRI, destination address in ALT. The "size"
1.533 + * parameter is in bytes, not cells.
1.534 + */
1.535 +SC_FUNC void memcopy(cell size)
1.536 +{
1.537 + stgwrite("\tmovs ");
1.538 + outval(size,TRUE);
1.539 +
1.540 + code_idx+=opcodes(1)+opargs(1);
1.541 +}
1.542 +
1.543 +/* Address of the source must already have been loaded in PRI
1.544 + * "size" is the size in bytes (not cells).
1.545 + */
1.546 +SC_FUNC void copyarray(symbol *sym,cell size)
1.547 +{
1.548 + assert(sym!=NULL);
1.549 + /* the symbol can be a local array, a global array, or an array
1.550 + * that is passed by reference.
1.551 + */
1.552 + if (sym->ident==iREFARRAY) {
1.553 + /* reference to an array; currently this is always a local variable */
1.554 + assert(sym->vclass==sLOCAL); /* symbol must be stack relative */
1.555 + stgwrite("\tload.s.alt ");
1.556 + } else {
1.557 + /* a local or global array */
1.558 + if (sym->vclass==sLOCAL)
1.559 + stgwrite("\taddr.alt ");
1.560 + else
1.561 + stgwrite("\tconst.alt ");
1.562 + } /* if */
1.563 + outval(sym->addr,TRUE);
1.564 + markusage(sym,uWRITTEN);
1.565 +
1.566 + code_idx+=opcodes(1)+opargs(1);
1.567 + memcopy(size);
1.568 +}
1.569 +
1.570 +SC_FUNC void fillarray(symbol *sym,cell size,cell value)
1.571 +{
1.572 + ldconst(value,sPRI); /* load value in PRI */
1.573 +
1.574 + assert(sym!=NULL);
1.575 + /* the symbol can be a local array, a global array, or an array
1.576 + * that is passed by reference.
1.577 + */
1.578 + if (sym->ident==iREFARRAY) {
1.579 + /* reference to an array; currently this is always a local variable */
1.580 + assert(sym->vclass==sLOCAL); /* symbol must be stack relative */
1.581 + stgwrite("\tload.s.alt ");
1.582 + } else {
1.583 + /* a local or global array */
1.584 + if (sym->vclass==sLOCAL)
1.585 + stgwrite("\taddr.alt ");
1.586 + else
1.587 + stgwrite("\tconst.alt ");
1.588 + } /* if */
1.589 + outval(sym->addr,TRUE);
1.590 + markusage(sym,uWRITTEN);
1.591 +
1.592 + assert(size>0);
1.593 + stgwrite("\tfill ");
1.594 + outval(size,TRUE);
1.595 +
1.596 + code_idx+=opcodes(2)+opargs(2);
1.597 +}
1.598 +
1.599 +SC_FUNC void stradjust(regid reg)
1.600 +{
1.601 + assert(reg==sPRI);
1.602 + stgwrite("\tstradjust.pri\n");
1.603 + code_idx+=opcodes(1);
1.604 +}
1.605 +
1.606 +/* Instruction to get an immediate value into the primary or the alternate
1.607 + * register
1.608 + */
1.609 +SC_FUNC void ldconst(cell val,regid reg)
1.610 +{
1.611 + assert(reg==sPRI || reg==sALT);
1.612 + switch (reg) {
1.613 + case sPRI:
1.614 + if (val==0) {
1.615 + stgwrite("\tzero.pri\n");
1.616 + code_idx+=opcodes(1);
1.617 + } else {
1.618 + stgwrite("\tconst.pri ");
1.619 + outval(val, TRUE);
1.620 + code_idx+=opcodes(1)+opargs(1);
1.621 + } /* if */
1.622 + break;
1.623 + case sALT:
1.624 + if (val==0) {
1.625 + stgwrite("\tzero.alt\n");
1.626 + code_idx+=opcodes(1);
1.627 + } else {
1.628 + stgwrite("\tconst.alt ");
1.629 + outval(val, TRUE);
1.630 + code_idx+=opcodes(1)+opargs(1);
1.631 + } /* if */
1.632 + break;
1.633 + } /* switch */
1.634 +}
1.635 +
1.636 +/* Copy value in alternate register to the primary register */
1.637 +SC_FUNC void moveto1(void)
1.638 +{
1.639 + stgwrite("\tmove.pri\n");
1.640 + code_idx+=opcodes(1)+opargs(0);
1.641 +}
1.642 +
1.643 +/* Push primary or the alternate register onto the stack
1.644 + */
1.645 +SC_FUNC void pushreg(regid reg)
1.646 +{
1.647 + assert(reg==sPRI || reg==sALT);
1.648 + switch (reg) {
1.649 + case sPRI:
1.650 + stgwrite("\tpush.pri\n");
1.651 + break;
1.652 + case sALT:
1.653 + stgwrite("\tpush.alt\n");
1.654 + break;
1.655 + } /* switch */
1.656 + code_idx+=opcodes(1);
1.657 +}
1.658 +
1.659 +/*
1.660 + * Push a constant value onto the stack
1.661 + */
1.662 +SC_FUNC void pushval(cell val)
1.663 +{
1.664 + stgwrite("\tpush.c ");
1.665 + outval(val, TRUE);
1.666 + code_idx+=opcodes(1)+opargs(1);
1.667 +}
1.668 +
1.669 +/* Pop stack into the primary or the alternate register
1.670 + */
1.671 +SC_FUNC void popreg(regid reg)
1.672 +{
1.673 + assert(reg==sPRI || reg==sALT);
1.674 + switch (reg) {
1.675 + case sPRI:
1.676 + stgwrite("\tpop.pri\n");
1.677 + break;
1.678 + case sALT:
1.679 + stgwrite("\tpop.alt\n");
1.680 + break;
1.681 + } /* switch */
1.682 + code_idx+=opcodes(1);
1.683 +}
1.684 +
1.685 +/*
1.686 + * Generate an array
1.687 + */
1.688 +SC_FUNC void genarray(int dims, int _autozero)
1.689 +{
1.690 + if (_autozero) {
1.691 + stgwrite("\tgenarray.z ");
1.692 + } else {
1.693 + stgwrite("\tgenarray ");
1.694 + }
1.695 + outval(dims, TRUE);
1.696 + code_idx+=opcodes(1)+opargs(1);
1.697 +}
1.698 +
1.699 +/*
1.700 + * swap the top-of-stack with the value in primary register
1.701 + */
1.702 +SC_FUNC void swap1(void)
1.703 +{
1.704 + stgwrite("\tswap.pri\n");
1.705 + code_idx+=opcodes(1);
1.706 +}
1.707 +
1.708 +/* Switch statements
1.709 + * The "switch" statement generates a "case" table using the "CASE" opcode.
1.710 + * The case table contains a list of records, each record holds a comparison
1.711 + * value and a label to branch to on a match. The very first record is an
1.712 + * exception: it holds the size of the table (excluding the first record) and
1.713 + * the label to branch to when none of the values in the case table match.
1.714 + * The case table is sorted on the comparison value. This allows more advanced
1.715 + * abstract machines to sift the case table with a binary search.
1.716 + */
1.717 +SC_FUNC void ffswitch(int label)
1.718 +{
1.719 + stgwrite("\tswitch ");
1.720 + outval(label,TRUE); /* the label is the address of the case table */
1.721 + code_idx+=opcodes(1)+opargs(1);
1.722 +}
1.723 +
1.724 +SC_FUNC void ffcase(cell value,char *labelname,int newtable)
1.725 +{
1.726 + if (newtable) {
1.727 + stgwrite("\tcasetbl\n");
1.728 + code_idx+=opcodes(1);
1.729 + } /* if */
1.730 + stgwrite("\tcase ");
1.731 + outval(value,FALSE);
1.732 + stgwrite(" ");
1.733 + stgwrite(labelname);
1.734 + stgwrite("\n");
1.735 + code_idx+=opcodes(0)+opargs(2);
1.736 +}
1.737 +
1.738 +/*
1.739 + * Call specified function
1.740 + */
1.741 +SC_FUNC void ffcall(symbol *sym,const char *label,int numargs)
1.742 +{
1.743 + char symname[2*sNAMEMAX+16];
1.744 + char aliasname[sNAMEMAX+1];
1.745 + int wasAlias = 0;
1.746 +
1.747 + assert(sym!=NULL);
1.748 + assert(sym->ident==iFUNCTN);
1.749 + if (sc_asmfile)
1.750 + funcdisplayname(symname,sym->name);
1.751 + if ((sym->usage & uNATIVE)!=0) {
1.752 + /* reserve a SYSREQ id if called for the first time */
1.753 + assert(label==NULL);
1.754 + stgwrite("\tsysreq.c ");
1.755 + if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0)
1.756 + sym->addr=ntv_funcid++;
1.757 + /* Look for an alias */
1.758 + if (lookup_alias(aliasname, sym->name)) {
1.759 + symbol *asym = findglb(aliasname, sGLOBAL);
1.760 + if (asym && asym->ident==iFUNCTN && ((sym->usage & uNATIVE) != 0)) {
1.761 + sym = asym;
1.762 + if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0) {
1.763 + sym->addr=ntv_funcid++;
1.764 + markusage(sym, uREAD);
1.765 + }
1.766 + }
1.767 + }
1.768 + outval(sym->addr,FALSE);
1.769 + if (sc_asmfile) {
1.770 + stgwrite("\t; ");
1.771 + stgwrite(symname);
1.772 + } /* if */
1.773 + stgwrite("\n"); /* write on a separate line, to mark a sequence point for the peephole optimizer */
1.774 + stgwrite("\tstack ");
1.775 + outval((numargs+1)*sizeof(cell), TRUE);
1.776 + code_idx+=opcodes(2)+opargs(2);
1.777 + } else {
1.778 + /* normal function */
1.779 + stgwrite("\tcall ");
1.780 + if (label!=NULL) {
1.781 + stgwrite("l.");
1.782 + stgwrite(label);
1.783 + } else {
1.784 + stgwrite(sym->name);
1.785 + } /* if */
1.786 + if (sc_asmfile
1.787 + && (label!=NULL || !isalpha(sym->name[0]) && sym->name[0]!='_' && sym->name[0]!=sc_ctrlchar))
1.788 + {
1.789 + stgwrite("\t; ");
1.790 + stgwrite(symname);
1.791 + } /* if */
1.792 + stgwrite("\n");
1.793 + code_idx+=opcodes(1)+opargs(1);
1.794 + } /* if */
1.795 +}
1.796 +
1.797 +/* Return from function
1.798 + *
1.799 + * Global references: funcstatus (referred to only)
1.800 + */
1.801 +SC_FUNC void ffret(int remparams)
1.802 +{
1.803 + if (remparams)
1.804 + stgwrite("\tretn\n");
1.805 + else
1.806 + stgwrite("\tret\n");
1.807 + code_idx+=opcodes(1);
1.808 +}
1.809 +
1.810 +SC_FUNC void ffabort(int reason)
1.811 +{
1.812 + stgwrite("\thalt ");
1.813 + outval(reason,TRUE);
1.814 + code_idx+=opcodes(1)+opargs(1);
1.815 +}
1.816 +
1.817 +SC_FUNC void ffbounds(cell size)
1.818 +{
1.819 + if ((sc_debug & sCHKBOUNDS)!=0) {
1.820 + stgwrite("\tbounds ");
1.821 + outval(size,TRUE);
1.822 + code_idx+=opcodes(1)+opargs(1);
1.823 + } /* if */
1.824 +}
1.825 +
1.826 +/*
1.827 + * Jump to local label number (the number is converted to a name)
1.828 + */
1.829 +SC_FUNC void jumplabel(int number)
1.830 +{
1.831 + stgwrite("\tjump ");
1.832 + outval(number,TRUE);
1.833 + code_idx+=opcodes(1)+opargs(1);
1.834 +}
1.835 +
1.836 +/*
1.837 + * Define storage (global and static variables)
1.838 + */
1.839 +SC_FUNC void defstorage(void)
1.840 +{
1.841 + stgwrite("dump ");
1.842 +}
1.843 +
1.844 +/*
1.845 + * Inclrement/decrement stack pointer. Note that this routine does
1.846 + * nothing if the delta is zero.
1.847 + */
1.848 +SC_FUNC void modstk(int delta)
1.849 +{
1.850 + if (delta) {
1.851 + stgwrite("\tstack ");
1.852 + outval(delta, TRUE);
1.853 + code_idx+=opcodes(1)+opargs(1);
1.854 + } /* if */
1.855 +}
1.856 +
1.857 +/* set the stack to a hard offset from the frame */
1.858 +SC_FUNC void setstk(cell value)
1.859 +{
1.860 + stgwrite("\tstackadjust ");
1.861 + assert(value<=0); /* STK should always become <= FRM */
1.862 + outval(value, TRUE); /* add (negative) offset */
1.863 + code_idx+=opcodes(1)+opargs(1);
1.864 +}
1.865 +
1.866 +SC_FUNC void modheap(int delta)
1.867 +{
1.868 + if (delta) {
1.869 + stgwrite("\theap ");
1.870 + outval(delta, TRUE);
1.871 + code_idx+=opcodes(1)+opargs(1);
1.872 + } /* if */
1.873 +}
1.874 +
1.875 +SC_FUNC void modheap_i()
1.876 +{
1.877 + stgwrite("\ttracker.pop.setheap\n");
1.878 + code_idx+=opcodes(1);
1.879 +}
1.880 +
1.881 +SC_FUNC void setheap_save(cell value)
1.882 +{
1.883 + assert(value);
1.884 + stgwrite("\ttracker.push.c ");
1.885 + outval(value, TRUE);
1.886 + code_idx+=opcodes(1)+opargs(1);
1.887 +}
1.888 +
1.889 +SC_FUNC void setheap_pri(void)
1.890 +{
1.891 + stgwrite("\theap "); /* ALT = HEA++ */
1.892 + outval(sizeof(cell), TRUE);
1.893 + stgwrite("\tstor.i\n"); /* store PRI (default value) at address ALT */
1.894 + stgwrite("\tmove.pri\n"); /* move ALT to PRI: PRI contains the address */
1.895 + code_idx+=opcodes(3)+opargs(1);
1.896 +}
1.897 +
1.898 +SC_FUNC void setheap(cell value)
1.899 +{
1.900 + stgwrite("\tconst.pri "); /* load default value in PRI */
1.901 + outval(value, TRUE);
1.902 + code_idx+=opcodes(1)+opargs(1);
1.903 + setheap_pri();
1.904 +}
1.905 +
1.906 +/*
1.907 + * Convert a cell number to a "byte" address; i.e. double or quadruple
1.908 + * the primary register.
1.909 + */
1.910 +SC_FUNC void cell2addr(void)
1.911 +{
1.912 + #if PAWN_CELL_SIZE==16
1.913 + stgwrite("\tshl.c.pri 1\n");
1.914 + #elif PAWN_CELL_SIZE==32
1.915 + stgwrite("\tshl.c.pri 2\n");
1.916 + #elif PAWN_CELL_SIZE==64
1.917 + stgwrite("\tshl.c.pri 3\n");
1.918 + #else
1.919 + #error Unsupported cell size
1.920 + #endif
1.921 + code_idx+=opcodes(1)+opargs(1);
1.922 +}
1.923 +
1.924 +/*
1.925 + * Double or quadruple the alternate register.
1.926 + */
1.927 +SC_FUNC void cell2addr_alt(void)
1.928 +{
1.929 + #if PAWN_CELL_SIZE==16
1.930 + stgwrite("\tshl.c.alt 1\n");
1.931 + #elif PAWN_CELL_SIZE==32
1.932 + stgwrite("\tshl.c.alt 2\n");
1.933 + #elif PAWN_CELL_SIZE==64
1.934 + stgwrite("\tshl.c.alt 3\n");
1.935 + #else
1.936 + #error Unsupported cell size
1.937 + #endif
1.938 + code_idx+=opcodes(1)+opargs(1);
1.939 +}
1.940 +
1.941 +/*
1.942 + * Convert "distance of addresses" to "number of cells" in between.
1.943 + * Or convert a number of packed characters to the number of cells (with
1.944 + * truncation).
1.945 + */
1.946 +SC_FUNC void addr2cell(void)
1.947 +{
1.948 + #if PAWN_CELL_SIZE==16
1.949 + stgwrite("\tshr.c.pri 1\n");
1.950 + #elif PAWN_CELL_SIZE==32
1.951 + stgwrite("\tshr.c.pri 2\n");
1.952 + #elif PAWN_CELL_SIZE==64
1.953 + stgwrite("\tshr.c.pri 3\n");
1.954 + #else
1.955 + #error Unsupported cell size
1.956 + #endif
1.957 + code_idx+=opcodes(1)+opargs(1);
1.958 +}
1.959 +
1.960 +/* Convert from character index to byte address. This routine does
1.961 + * nothing if a character has the size of a byte.
1.962 + */
1.963 +SC_FUNC void char2addr(void)
1.964 +{
1.965 + #if sCHARBITS==16
1.966 + stgwrite("\tshl.c.pri 1\n");
1.967 + code_idx+=opcodes(1)+opargs(1);
1.968 + #endif
1.969 +}
1.970 +
1.971 +/* Align PRI (which should hold a character index) to an address.
1.972 + * The first character in a "pack" occupies the highest bits of
1.973 + * the cell. This is at the lower memory address on Big Endian
1.974 + * computers and on the higher address on Little Endian computers.
1.975 + * The ALIGN.pri/alt instructions must solve this machine dependence;
1.976 + * that is, on Big Endian computers, ALIGN.pri/alt shuold do nothing
1.977 + * and on Little Endian computers they should toggle the address.
1.978 + *
1.979 + * NOTE: For Source Pawn, this is fliped. It will do nothing on Little-Endian.
1.980 + */
1.981 +SC_FUNC void charalign(void)
1.982 +{
1.983 +#if 0 /* TEMPORARILY DISABLED BECAUSE WE DON'T USE BIG ENDIAN */
1.984 + stgwrite("\talign.pri ");
1.985 + outval(sCHARBITS/8,TRUE);
1.986 + code_idx+=opcodes(1)+opargs(1);
1.987 +#endif
1.988 +}
1.989 +
1.990 +/*
1.991 + * Add a constant to the primary register.
1.992 + */
1.993 +SC_FUNC void addconst(cell value)
1.994 +{
1.995 + if (value!=0) {
1.996 + stgwrite("\tadd.c ");
1.997 + outval(value,TRUE);
1.998 + code_idx+=opcodes(1)+opargs(1);
1.999 + } /* if */
1.1000 +}
1.1001 +
1.1002 +/*
1.1003 + * signed multiply of primary and secundairy registers (result in primary)
1.1004 + */
1.1005 +SC_FUNC void os_mult(void)
1.1006 +{
1.1007 + stgwrite("\tsmul\n");
1.1008 + code_idx+=opcodes(1);
1.1009 +}
1.1010 +
1.1011 +/*
1.1012 + * signed divide of alternate register by primary register (quotient in
1.1013 + * primary; remainder in alternate)
1.1014 + */
1.1015 +SC_FUNC void os_div(void)
1.1016 +{
1.1017 + stgwrite("\tsdiv.alt\n");
1.1018 + code_idx+=opcodes(1);
1.1019 +}
1.1020 +
1.1021 +/*
1.1022 + * modulus of (alternate % primary), result in primary (signed)
1.1023 + */
1.1024 +SC_FUNC void os_mod(void)
1.1025 +{
1.1026 + stgwrite("\tsdiv.alt\n");
1.1027 + stgwrite("\tmove.pri\n"); /* move ALT to PRI */
1.1028 + code_idx+=opcodes(2);
1.1029 +}
1.1030 +
1.1031 +/*
1.1032 + * Add primary and alternate registers (result in primary).
1.1033 + */
1.1034 +SC_FUNC void ob_add(void)
1.1035 +{
1.1036 + stgwrite("\tadd\n");
1.1037 + code_idx+=opcodes(1);
1.1038 +}
1.1039 +
1.1040 +/*
1.1041 + * subtract primary register from alternate register (result in primary)
1.1042 + */
1.1043 +SC_FUNC void ob_sub(void)
1.1044 +{
1.1045 + stgwrite("\tsub.alt\n");
1.1046 + code_idx+=opcodes(1);
1.1047 +}
1.1048 +
1.1049 +/*
1.1050 + * arithmic shift left alternate register the number of bits
1.1051 + * given in the primary register (result in primary).
1.1052 + * There is no need for a "logical shift left" routine, since
1.1053 + * logical shift left is identical to arithmic shift left.
1.1054 + */
1.1055 +SC_FUNC void ob_sal(void)
1.1056 +{
1.1057 + stgwrite("\txchg\n");
1.1058 + stgwrite("\tshl\n");
1.1059 + code_idx+=opcodes(2);
1.1060 +}
1.1061 +
1.1062 +/*
1.1063 + * arithmic shift right alternate register the number of bits
1.1064 + * given in the primary register (result in primary).
1.1065 + */
1.1066 +SC_FUNC void os_sar(void)
1.1067 +{
1.1068 + stgwrite("\txchg\n");
1.1069 + stgwrite("\tsshr\n");
1.1070 + code_idx+=opcodes(2);
1.1071 +}
1.1072 +
1.1073 +/*
1.1074 + * logical (unsigned) shift right of the alternate register by the
1.1075 + * number of bits given in the primary register (result in primary).
1.1076 + */
1.1077 +SC_FUNC void ou_sar(void)
1.1078 +{
1.1079 + stgwrite("\txchg\n");
1.1080 + stgwrite("\tshr\n");
1.1081 + code_idx+=opcodes(2);
1.1082 +}
1.1083 +
1.1084 +/*
1.1085 + * inclusive "or" of primary and alternate registers (result in primary)
1.1086 + */
1.1087 +SC_FUNC void ob_or(void)
1.1088 +{
1.1089 + stgwrite("\tor\n");
1.1090 + code_idx+=opcodes(1);
1.1091 +}
1.1092 +
1.1093 +/*
1.1094 + * "exclusive or" of primary and alternate registers (result in primary)
1.1095 + */
1.1096 +SC_FUNC void ob_xor(void)
1.1097 +{
1.1098 + stgwrite("\txor\n");
1.1099 + code_idx+=opcodes(1);
1.1100 +}
1.1101 +
1.1102 +/*
1.1103 + * "and" of primary and secundairy registers (result in primary)
1.1104 + */
1.1105 +SC_FUNC void ob_and(void)
1.1106 +{
1.1107 + stgwrite("\tand\n");
1.1108 + code_idx+=opcodes(1);
1.1109 +}
1.1110 +
1.1111 +/*
1.1112 + * test ALT==PRI; result in primary register (1 or 0).
1.1113 + */
1.1114 +SC_FUNC void ob_eq(void)
1.1115 +{
1.1116 + stgwrite("\teq\n");
1.1117 + code_idx+=opcodes(1);
1.1118 +}
1.1119 +
1.1120 +/*
1.1121 + * test ALT!=PRI
1.1122 + */
1.1123 +SC_FUNC void ob_ne(void)
1.1124 +{
1.1125 + stgwrite("\tneq\n");
1.1126 + code_idx+=opcodes(1);
1.1127 +}
1.1128 +
1.1129 +/* The abstract machine defines the relational instructions so that PRI is
1.1130 + * on the left side and ALT on the right side of the operator. For example,
1.1131 + * SLESS sets PRI to either 1 or 0 depending on whether the expression
1.1132 + * "PRI < ALT" is true.
1.1133 + *
1.1134 + * The compiler generates comparisons with ALT on the left side of the
1.1135 + * relational operator and PRI on the right side. The XCHG instruction
1.1136 + * prefixing the relational operators resets this. We leave it to the
1.1137 + * peephole optimizer to choose more compact instructions where possible.
1.1138 + */
1.1139 +
1.1140 +/* Relational operator prefix for chained relational expressions. The
1.1141 + * "suffix" code restores the stack.
1.1142 + * For chained relational operators, the goal is to keep the comparison
1.1143 + * result "so far" in PRI and the value of the most recent operand in
1.1144 + * ALT, ready for a next comparison.
1.1145 + * The "prefix" instruction pushed the comparison result (PRI) onto the
1.1146 + * stack and moves the value of ALT into PRI. If there is a next comparison,
1.1147 + * PRI can now serve as the "left" operand of the relational operator.
1.1148 + */
1.1149 +SC_FUNC void relop_prefix(void)
1.1150 +{
1.1151 + stgwrite("\tpush.pri\n");
1.1152 + stgwrite("\tmove.pri\n");
1.1153 + code_idx+=opcodes(2);
1.1154 +}
1.1155 +
1.1156 +SC_FUNC void relop_suffix(void)
1.1157 +{
1.1158 + stgwrite("\tswap.alt\n");
1.1159 + stgwrite("\tand\n");
1.1160 + stgwrite("\tpop.alt\n");
1.1161 + code_idx+=opcodes(3);
1.1162 +}
1.1163 +
1.1164 +/*
1.1165 + * test ALT<PRI (signed)
1.1166 + */
1.1167 +SC_FUNC void os_lt(void)
1.1168 +{
1.1169 + stgwrite("\txchg\n");
1.1170 + stgwrite("\tsless\n");
1.1171 + code_idx+=opcodes(2);
1.1172 +}
1.1173 +
1.1174 +/*
1.1175 + * test ALT<=PRI (signed)
1.1176 + */
1.1177 +SC_FUNC void os_le(void)
1.1178 +{
1.1179 + stgwrite("\txchg\n");
1.1180 + stgwrite("\tsleq\n");
1.1181 + code_idx+=opcodes(2);
1.1182 +}
1.1183 +
1.1184 +/*
1.1185 + * test ALT>PRI (signed)
1.1186 + */
1.1187 +SC_FUNC void os_gt(void)
1.1188 +{
1.1189 + stgwrite("\txchg\n");
1.1190 + stgwrite("\tsgrtr\n");
1.1191 + code_idx+=opcodes(2);
1.1192 +}
1.1193 +
1.1194 +/*
1.1195 + * test ALT>=PRI (signed)
1.1196 + */
1.1197 +SC_FUNC void os_ge(void)
1.1198 +{
1.1199 + stgwrite("\txchg\n");
1.1200 + stgwrite("\tsgeq\n");
1.1201 + code_idx+=opcodes(2);
1.1202 +}
1.1203 +
1.1204 +/*
1.1205 + * logical negation of primary register
1.1206 + */
1.1207 +SC_FUNC void lneg(void)
1.1208 +{
1.1209 + stgwrite("\tnot\n");
1.1210 + code_idx+=opcodes(1);
1.1211 +}
1.1212 +
1.1213 +/*
1.1214 + * two's complement primary register
1.1215 + */
1.1216 +SC_FUNC void neg(void)
1.1217 +{
1.1218 + stgwrite("\tneg\n");
1.1219 + code_idx+=opcodes(1);
1.1220 +}
1.1221 +
1.1222 +/*
1.1223 + * one's complement of primary register
1.1224 + */
1.1225 +SC_FUNC void invert(void)
1.1226 +{
1.1227 + stgwrite("\tinvert\n");
1.1228 + code_idx+=opcodes(1);
1.1229 +}
1.1230 +
1.1231 +/*
1.1232 + * nop
1.1233 + */
1.1234 +SC_FUNC void nooperation(void)
1.1235 +{
1.1236 + stgwrite("\tnop\n");
1.1237 + code_idx+=opcodes(1);
1.1238 +}
1.1239 +
1.1240 +
1.1241 +/* increment symbol
1.1242 + */
1.1243 +SC_FUNC void inc(value *lval)
1.1244 +{
1.1245 + symbol *sym;
1.1246 +
1.1247 + sym=lval->sym;
1.1248 + if (lval->ident==iARRAYCELL) {
1.1249 + /* indirect increment, address already in PRI */
1.1250 + stgwrite("\tinc.i\n");
1.1251 + code_idx+=opcodes(1);
1.1252 + } else if (lval->ident==iARRAYCHAR) {
1.1253 + /* indirect increment of single character, address already in PRI */
1.1254 + stgwrite("\tpush.pri\n");
1.1255 + stgwrite("\tpush.alt\n");
1.1256 + stgwrite("\tmove.alt\n"); /* copy address */
1.1257 + stgwrite("\tlodb.i "); /* read from PRI into PRI */
1.1258 + outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1.1259 + stgwrite("\tinc.pri\n");
1.1260 + stgwrite("\tstrb.i "); /* write PRI to ALT */
1.1261 + outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1.1262 + stgwrite("\tpop.alt\n");
1.1263 + stgwrite("\tpop.pri\n");
1.1264 + code_idx+=opcodes(8)+opargs(2);
1.1265 + } else if (lval->ident==iREFERENCE) {
1.1266 + assert(sym!=NULL);
1.1267 + stgwrite("\tpush.pri\n");
1.1268 + /* load dereferenced value */
1.1269 + assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1.1270 + if (sym->vclass==sLOCAL)
1.1271 + stgwrite("\tlref.s.pri ");
1.1272 + else
1.1273 + stgwrite("\tlref.pri ");
1.1274 + outval(sym->addr,TRUE);
1.1275 + /* increment */
1.1276 + stgwrite("\tinc.pri\n");
1.1277 + /* store dereferenced value */
1.1278 + if (sym->vclass==sLOCAL)
1.1279 + stgwrite("\tsref.s.pri ");
1.1280 + else
1.1281 + stgwrite("\tsref.pri ");
1.1282 + outval(sym->addr,TRUE);
1.1283 + stgwrite("\tpop.pri\n");
1.1284 + code_idx+=opcodes(5)+opargs(2);
1.1285 + } else {
1.1286 + /* local or global variable */
1.1287 + assert(sym!=NULL);
1.1288 + if (sym->vclass==sLOCAL)
1.1289 + stgwrite("\tinc.s ");
1.1290 + else
1.1291 + stgwrite("\tinc ");
1.1292 + outval(sym->addr,TRUE);
1.1293 + code_idx+=opcodes(1)+opargs(1);
1.1294 + } /* if */
1.1295 +}
1.1296 +
1.1297 +/* decrement symbol
1.1298 + *
1.1299 + * in case of an integer pointer, the symbol must be incremented by 2.
1.1300 + */
1.1301 +SC_FUNC void dec(value *lval)
1.1302 +{
1.1303 + symbol *sym;
1.1304 +
1.1305 + sym=lval->sym;
1.1306 + if (lval->ident==iARRAYCELL) {
1.1307 + /* indirect decrement, address already in PRI */
1.1308 + stgwrite("\tdec.i\n");
1.1309 + code_idx+=opcodes(1);
1.1310 + } else if (lval->ident==iARRAYCHAR) {
1.1311 + /* indirect decrement of single character, address already in PRI */
1.1312 + stgwrite("\tpush.pri\n");
1.1313 + stgwrite("\tpush.alt\n");
1.1314 + stgwrite("\tmove.alt\n"); /* copy address */
1.1315 + stgwrite("\tlodb.i "); /* read from PRI into PRI */
1.1316 + outval(sCHARBITS/8,TRUE); /* read one or two bytes */
1.1317 + stgwrite("\tdec.pri\n");
1.1318 + stgwrite("\tstrb.i "); /* write PRI to ALT */
1.1319 + outval(sCHARBITS/8,TRUE); /* write one or two bytes */
1.1320 + stgwrite("\tpop.alt\n");
1.1321 + stgwrite("\tpop.pri\n");
1.1322 + code_idx+=opcodes(8)+opargs(2);
1.1323 + } else if (lval->ident==iREFERENCE) {
1.1324 + assert(sym!=NULL);
1.1325 + stgwrite("\tpush.pri\n");
1.1326 + /* load dereferenced value */
1.1327 + assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */
1.1328 + if (sym->vclass==sLOCAL)
1.1329 + stgwrite("\tlref.s.pri ");
1.1330 + else
1.1331 + stgwrite("\tlref.pri ");
1.1332 + outval(sym->addr,TRUE);
1.1333 + /* decrement */
1.1334 + stgwrite("\tdec.pri\n");
1.1335 + /* store dereferenced value */
1.1336 + if (sym->vclass==sLOCAL)
1.1337 + stgwrite("\tsref.s.pri ");
1.1338 + else
1.1339 + stgwrite("\tsref.pri ");
1.1340 + outval(sym->addr,TRUE);
1.1341 + stgwrite("\tpop.pri\n");
1.1342 + code_idx+=opcodes(5)+opargs(2);
1.1343 + } else {
1.1344 + /* local or global variable */
1.1345 + assert(sym!=NULL);
1.1346 + if (sym->vclass==sLOCAL)
1.1347 + stgwrite("\tdec.s ");
1.1348 + else
1.1349 + stgwrite("\tdec ");
1.1350 + outval(sym->addr,TRUE);
1.1351 + code_idx+=opcodes(1)+opargs(1);
1.1352 + } /* if */
1.1353 +}
1.1354 +
1.1355 +/*
1.1356 + * Jumps to "label" if PRI != 0
1.1357 + */
1.1358 +SC_FUNC void jmp_ne0(int number)
1.1359 +{
1.1360 + stgwrite("\tjnz ");
1.1361 + outval(number,TRUE);
1.1362 + code_idx+=opcodes(1)+opargs(1);
1.1363 +}
1.1364 +
1.1365 +/*
1.1366 + * Jumps to "label" if PRI == 0
1.1367 + */
1.1368 +SC_FUNC void jmp_eq0(int number)
1.1369 +{
1.1370 + stgwrite("\tjzer ");
1.1371 + outval(number,TRUE);
1.1372 + code_idx+=opcodes(1)+opargs(1);
1.1373 +}
1.1374 +
1.1375 +/* write a value in hexadecimal; optionally adds a newline */
1.1376 +SC_FUNC void outval(cell val,int newline)
1.1377 +{
1.1378 + stgwrite(itoh(val));
1.1379 + if (newline)
1.1380 + stgwrite("\n");
1.1381 +}