示例#1
0
文件: sc4.c 项目: glockwork/dfu
/*  writetrailer
 *  Not much left of this once important function.
 *
 *  Global references: pc_stksize       (referred to only)
 *                     sc_dataalign     (referred to only)
 *                     code_idx         (altered)
 *                     glb_declared     (altered)
 */
SC_FUNC void writetrailer(void)
{
  assert(sc_dataalign % opcodes(1) == 0);   /* alignment must be a multiple of
                                             * the opcode size */
  assert(sc_dataalign!=0);

  /* pad code to align data segment */
  if ((code_idx % sc_dataalign)!=0) {
    begcseg();
    while ((code_idx % sc_dataalign)!=0)
      nooperation();
  } /* if */

  /* pad data segment to align the stack and the heap */
  assert(litidx==0);            /* literal queue should have been emptied */
  assert(sc_dataalign % sizeof(cell) == 0);
  if (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
    begdseg();
    defstorage();
    while (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) {
      stgwrite("0 ");
      glb_declared++;
    } /* while */
  } /* if */

  stgwrite("\nSTKSIZE ");       /* write stack size (align stack top) */
  outval(pc_stksize - (pc_stksize % sc_dataalign),TRUE,TRUE);
}
示例#2
0
void
setfile(char *name, int fileno)
{
    if ((sc_debug & sSYMBOLIC) != 0)
    {
        begcseg();
        stgwrite("file ");
        outval(fileno, FALSE);
        stgwrite(" ");
        stgwrite(name);
        stgwrite("\n");
        /* calculate code length */
        code_idx += opcodes(1) + opargs(2) + nameincells(name);
    }				/* if */
}
示例#3
0
文件: sc4.c 项目: glockwork/dfu
/* When a subroutine returns to address 0, the AMX must halt. In earlier
 * releases, the RET and RETN opcodes checked for the special case 0 address.
 * Today, the compiler simply generates a HALT instruction at address 0. So
 * a subroutine can savely return to 0, and then encounter a HALT.
 */
SC_FUNC void writeleader(symbol *root,int *lbl_nostate,int *lbl_ignorestate)
{
  symbol *sym;

  assert(code_idx==0);

  assert(lbl_nostate!=NULL);
  assert(lbl_ignorestate!=NULL);
  *lbl_nostate=0;
  *lbl_ignorestate=0;

  begcseg();
  pc_ovl0size[ovlEXIT][0]=code_idx;     /* store offset to the special overlay */
  stgwrite(";program exit point\n");
#if !defined AMX_NO_PACKED_OPC
  if (pc_optimize>sOPTIMIZE_NOMACRO) {
    stgwrite("\thalt.p 0\n\n");
    code_idx+=opcodes(1);
  } else {
#endif
    stgwrite("\thalt 0\n\n");
    code_idx+=opcodes(1)+opargs(1);     /* calculate code length */
#if !defined AMX_NO_PACKED_OPC
  } /* if */
#endif
  pc_ovl0size[ovlEXIT][1]=code_idx-pc_ovl0size[ovlEXIT][0]; /* store overlay code size */

  /* check whether there are any functions that have states */
  for (sym=root->next; sym!=NULL; sym=sym->next)
    if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL)
      break;
  if (sym==NULL)
    return;             /* no function has states, nothing to do next */

  /* generate an error function that is called for an undefined state */
  pc_ovl0size[ovlNO_STATE][0]=code_idx;
  stgwrite(";exit point for functions called from the wrong state\n");
  assert(lbl_nostate!=NULL);
  *lbl_nostate=getlabel();
  setlabel(*lbl_nostate);
#if !defined AMX_NO_PACKED_OPC
  if (pc_optimize>sOPTIMIZE_NOMACRO) {
    assert(AMX_ERR_INVSTATE<(1<<sizeof(cell)*4));
    stgwrite("\thalt.p ");
    outval(AMX_ERR_INVSTATE,TRUE,TRUE);
    code_idx+=opcodes(1);
  } else {
#endif
    stgwrite("\thalt ");
    outval(AMX_ERR_INVSTATE,TRUE,TRUE);
    code_idx+=opcodes(1)+opargs(1);     /* calculate code length */
#if !defined AMX_NO_PACKED_OPC
  } /* if */
#endif
  stgwrite("\n");
  pc_ovl0size[ovlNO_STATE][1]=code_idx-pc_ovl0size[ovlNO_STATE][0];

  /* check whether there are "exit state" functions */
  for (sym=root->next; sym!=NULL; sym=sym->next)
    if (strcmp(sym->name,uEXITFUNC)==0)
      break;
  if (sym!=NULL) {
    /* generate a stub function that is called for an undefined exit state and
     * that returns immediately to the caller (no error)
     */
    pc_ovl0size[ovlEXITSTATE][0]=code_idx;
    stgwrite(";catch-all for undefined exit states\n");
    assert(lbl_ignorestate!=NULL);
    *lbl_ignorestate=getlabel();
    setlabel(*lbl_ignorestate);
    /* the RET and IRETN instructions pop off the FRM register from the stack,
     * because they assume that a stack frame was set up; for this catch-all
     * routine (for exit states) we therefore need to set up a stack frame
     */
    stgwrite("\tproc\n");
    if (pc_overlays>0)
      stgwrite("\tiretn\n");
    else
      stgwrite("\tret\n");
    code_idx+=opcodes(2);
    pc_ovl0size[ovlEXITSTATE][1]=code_idx-pc_ovl0size[ovlEXITSTATE][0];
  } /* if */
}
示例#4
0
文件: sc4.c 项目: glockwork/dfu
/* writestatetables
 * Creates and dumps the state tables. Every function with states has a state
 * table that contains jump addresses (or overlay indices) the branch to the
 * appropriate function using the (hidden) state variable as the criterion.
 * Technically, this happens in a "switch" (or an "iswitch") instruction.
 * This function also creates the hidden state variables (one for each
 * automaton) in the data segment.
 */
SC_FUNC void writestatetables(symbol *root,int lbl_nostate,int lbl_ignorestate)
{
  int lbl_default,lbl_table,lbl_defnostate;
  int statecount;
  symbol *sym;
  constvalue *fsa, *state;
  statelist *stlist;
  int fsa_id,listid;

  assert(code_idx>0);   /* leader must already have been written */

  /* check whether there are any functions that have states */
  for (sym=root->next; sym!=NULL; sym=sym->next)
    if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL)
      break;
  if (sym==NULL)
    return;             /* no function has states, nothing to do next */

  assert(pc_ovl0size[ovlNO_STATE][0]>0); /* state exit point must already have been created */
  assert(pc_ovl0size[ovlNO_STATE][1]>0);

  /* write the "state-selectors" table with all automatons (update the
   * automatons structure too, as we are now assigning the address to
   * each automaton state-selector variable)
   */
  assert(glb_declared==0);
  begdseg();
  for (fsa=sc_automaton_tab.next; fsa!=NULL; fsa=fsa->next) {
    defstorage();
    stgwrite("0\t; automaton ");
    if (strlen(fsa->name)==0)
      stgwrite("(anonymous)");
    else
      stgwrite(fsa->name);
    stgwrite("\n");
    fsa->value=glb_declared*sizeof(cell);
    glb_declared++;
  } /* for */

  /* write stubs and jump tables for all state functions */
  begcseg();
  for (sym=root->next; sym!=NULL; sym=sym->next) {
    if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL) {
      stlist=sym->states->next;
      assert(stlist!=NULL);     /* there should be at least one state item */
      listid=stlist->id;
      assert(listid==-1 || listid>0);
      if (listid==-1 && stlist->next!=NULL) {
        /* first index is the "fallback", take the next one (if available) */
        stlist=stlist->next;
        listid=stlist->id;
      } /* if */
      if (listid==-1) {
        /* first index is the fallback, there is no second... */
        stlist->label=0;        /* insert dummy label number */
        /* this is an error, but we postpone adding the error message until the
         * function definition
         */
        continue;
      } /* if */
      /* generate label numbers for all statelist ids */
      for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
        if (pc_overlays>0) {
          /* code overlay indices should already be set, see gen_ovlinfo() */
          assert(stlist->label>0);
        } else {
          assert(stlist->label==0);
          stlist->label=getlabel();
        } /* if */
      } /* for */
      if (strcmp(sym->name,uENTRYFUNC)==0)
        continue;               /* do not generate stubs for this special function */
      sym->addr=code_idx;       /* fix the function address now */
      /* get automaton id for this function */
      assert(listid>0);
      fsa_id=state_getfsa(listid);
      assert(fsa_id>=0);        /* automaton 0 exists */
      fsa=automaton_findid(fsa_id);
      /* count the number of states actually used; at the same time, check
       * whether there is a default (i.e. "fallback") state function
       */
      statecount=0;
      if (strcmp(sym->name,uEXITFUNC)==0) {
        lbl_default= (pc_overlays>0) ? ovlEXITSTATE : lbl_ignorestate;
      } else {
        lbl_defnostate= (pc_overlays>0) ? ovlNO_STATE : lbl_nostate;
        lbl_default=lbl_defnostate;
      } /* if */
      for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
        if (stlist->id==-1) {
          lbl_default=stlist->label;
        } else {
          statecount+=state_count(stlist->id);
        } /* if */
      } /* for */
      /* generate a stub entry for the functions */
      stgwrite("\tload.pri ");
      outval(fsa->value,TRUE,FALSE);
      stgwrite("\t; ");
      stgwrite(sym->name);
      if (pc_overlays>0) {
        /* add overlay index */
        stgwrite("/");
        outval(sym->index,FALSE,FALSE);
      } /* if */
      stgwrite("\n");
      code_idx+=opcodes(1)+opargs(1);   /* calculate code length */
      lbl_table=getlabel();
      ffswitch(lbl_table,(pc_overlays>0));
      /* generate the jump table */
      setlabel(lbl_table);
      ffcase(statecount,lbl_default,TRUE,(pc_overlays>0));
      for (state=sc_state_tab.next; state!=NULL; state=state->next) {
        if (state->index==fsa_id) {
          /* find the label for this list id */
          for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
            if (stlist->id!=-1 && state_inlist(stlist->id,(int)state->value)) {
              /* when overlays are used, the jump-label for the case statement
               * are overlay indices instead of code labels
               */
              ffcase(state->value,stlist->label,FALSE,(pc_overlays>0));
              break;
            } /* if */
          } /* for */
          if (stlist==NULL && lbl_default==lbl_defnostate)
            error(230,state->name,sym->name);  /* unimplemented state, no fallback */
        } /* if (state belongs to automaton of function) */
      } /* for (state) */
      stgwrite("\n");
      /* the jump table gets its own overlay index, and the size of the jump
       * table must therefore be known (i.e. update the codeaddr field of the
       * function with the address where the jump table ends)
       */
      sym->codeaddr=code_idx;
    } /* if (is function, used & having states) */
  } /* for (sym) */
}
示例#5
0
文件: sc4.c 项目: jte/pawn
/* When a subroutine returns to address 0, the AMX must halt. In earlier
 * releases, the RET and RETN opcodes checked for the special case 0 address.
 * Today, the compiler simply generates a HALT instruction at address 0. So
 * a subroutine can savely return to 0, and then encounter a HALT.
 */
SC_FUNC void writeleader(symbol *root)
{
  int lbl_nostate,lbl_table;
  int statecount;
  symbol *sym;
  constvalue *fsa, *state, *stlist;
  int fsa_id,listid;
  char lbl_default[sNAMEMAX+1];

  assert(code_idx==0);

  begcseg();
  stgwrite(";program exit point\n");
  stgwrite("\thalt 0\n\n");
  code_idx+=opcodes(1)+opargs(1);       /* calculate code length */

  /* check whether there are any functions that have states */
  for (sym=root->next; sym!=NULL; sym=sym->next)
    if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL)
      break;
  if (sym==NULL)
    return;             /* no function has states, nothing to do next */

  /* generate an error function that is called for an undefined state */
  stgwrite("\n;exit point for functions called from the wrong state\n");
  lbl_nostate=getlabel();
  setlabel(lbl_nostate);
  stgwrite("\thalt ");
  outval(AMX_ERR_INVSTATE,TRUE);
  code_idx+=opcodes(1)+opargs(1);       /* calculate code length */

  /* write the "state-selectors" table with all automatons (update the
   * automatons structure too, as we are now assigning the address to
   * each automaton state-selector variable)
   */
  assert(glb_declared==0);
  begdseg();
  for (fsa=sc_automaton_tab.next; fsa!=NULL; fsa=fsa->next) {
    defstorage();
    stgwrite("0\t; automaton ");
    if (strlen(fsa->name)==0)
      stgwrite("(anonymous)");
    else
      stgwrite(fsa->name);
    stgwrite("\n");
    fsa->value=glb_declared*sizeof(cell);
    glb_declared++;
  } /* for */

  /* write stubs and jump tables for all state functions */
  begcseg();
  for (sym=root->next; sym!=NULL; sym=sym->next) {
    if (sym->ident==iFUNCTN && (sym->usage & (uPUBLIC | uREAD))!=0 && sym->states!=NULL) {
      stlist=sym->states->next;
      assert(stlist!=NULL);     /* there should be at least one state item */
      listid=stlist->index;
      assert(listid==-1 || listid>0);
      if (listid==-1 && stlist->next!=NULL) {
        /* first index is the "fallback", take the next one (if available) */
        stlist=stlist->next;
        listid=stlist->index;
      } /* if */
      if (listid==-1) {
        /* first index is the fallback, there is no second... */
        strcpy(stlist->name,"0"); /* insert dummy label number */
        /* this is an error, but we postpone adding the error message until the
         * function definition
         */
        continue;
      } /* if */
      /* generate label numbers for all statelist ids */
      for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
        assert(strlen(stlist->name)==0);
        strcpy(stlist->name,itoh(getlabel()));
      } /* for */
      if (strcmp(sym->name,uENTRYFUNC)==0)
        continue;               /* do not generate stubs for this special function */
      sym->addr=code_idx;       /* fix the function address now */
      /* get automaton id for this function */
      assert(listid>0);
      fsa_id=state_getfsa(listid);
      assert(fsa_id>=0);        /* automaton 0 exists */
      fsa=automaton_findid(fsa_id);
      /* count the number of states actually used; at the sane time, check
       * whether there is a default state function
       */
      statecount=0;
      strcpy(lbl_default,itoh(lbl_nostate));
      for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
        if (stlist->index==-1) {
          assert(strlen(stlist->name)<sizeof lbl_default);
          strcpy(lbl_default,stlist->name);
        } else {
          statecount+=state_count(stlist->index);
        } /* if */
      } /* for */
      /* generate a stub entry for the functions */
      stgwrite("\tload.pri ");
      outval(fsa->value,FALSE);
      stgwrite("\t; ");
      stgwrite(sym->name);
      stgwrite("\n");
      code_idx+=opcodes(1)+opargs(1);   /* calculate code length */
      lbl_table=getlabel();
      ffswitch(lbl_table);
      /* generate the jump table */
      setlabel(lbl_table);
      ffcase(statecount,lbl_default,TRUE);
      for (state=sc_state_tab.next; state!=NULL; state=state->next) {
        if (state->index==fsa_id) {
          /* find the label for this list id */
          for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) {
            if (stlist->index!=-1 && state_inlist(stlist->index,(int)state->value)) {
              ffcase(state->value,stlist->name,FALSE);
              break;
            } /* if */
          } /* for */
          if (stlist==NULL && strtol(lbl_default,NULL,16)==lbl_nostate)
            error(230,state->name,sym->name);  /* unimplemented state, no fallback */
        } /* if (state belongs to automaton of function) */
      } /* for (state) */
      stgwrite("\n");
    } /* if (is function, used & having states) */
  } /* for (sym) */
}