home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / assembler / as / src / c / commands < prev    next >
Encoding:
Text File  |  1993-03-07  |  5.0 KB  |  256 lines

  1.  
  2. /*
  3.  * commands.c
  4.  * Copyright © 1992 Niklas Röjemo
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <string.h>
  11. #include "error.h"
  12. #include "input.h"
  13. #include "lex.h"
  14. #include "help_lex.h"
  15. #include "symbol.h"
  16. #include "expr.h"
  17. #include "code.h"
  18. #include "value.h"
  19. #include "put.h"
  20. #include "commands.h"
  21. #include "fix.h"
  22. #include "include.h"
  23. #include "filestack.h"
  24. #include "area.h"
  25. #include "lit.h"
  26.  
  27. extern FILE *asmfile;  /* in input.c */
  28.  
  29.  
  30. static Symbol *c_define(char *msg, Symbol *sym, ValueTag legal)
  31. {
  32.   Value value;
  33.   if(!sym)
  34.     error(ErrorError,FALSE,"Missing label before %s.");
  35.   sym->type |= SYMBOL_ABSOLUTE;
  36.   exprBuild();
  37.   value = exprEval(legal);
  38.   switch(value.Tag) {
  39.     case ValueIllegal:
  40.       error(ErrorError,TRUE,"Illegal %s.",msg);
  41.       sym->value.Tag = ValueInt;
  42.       sym->value.ValueInt.i = 0;
  43.       break;
  44.     default:
  45.       sym->value = valueCopy(value);
  46.   }
  47.   sym->type |= SYMBOL_DEFINED;
  48.   sym->area.ptr = 0;
  49.   return sym;
  50. }
  51.  
  52. void c_equ(Symbol *symbol)
  53. {
  54.   (void)c_define("equ",symbol,ValueAll);
  55. }
  56.  
  57. void c_fn(Symbol *symbol)
  58. {
  59.   int no = c_define("float register",symbol,ValueInt)->value.ValueInt.i;
  60.   symbol->type |= SYMBOL_FPUREG;
  61.   if(no<0 || no>7) {
  62.     symbol->value.ValueInt.i = 0;
  63.     error(ErrorError,TRUE,"Illegal fpu register %d (using 0).",no);
  64.   }
  65. }
  66.  
  67. void c_rn(Symbol *symbol)
  68. {
  69.   int no=c_define("register",symbol,ValueInt)->value.ValueInt.i;
  70.   symbol->type |= SYMBOL_CPUREG;
  71.   if(no<0 || no>15) {
  72.     symbol->value.ValueInt.i = 0;
  73.     error(ErrorError,TRUE,"Illegal cpu register %d (using 0).",no);
  74.   }
  75. }
  76.  
  77. void c_cn(Symbol *symbol)
  78. {
  79.   int no = c_define("register",symbol,ValueInt)->value.ValueInt.i;
  80.   symbol->type |= SYMBOL_COPREG;
  81.   if(no<0 || no>15) {
  82.     symbol->value.ValueInt.i = 0;
  83.     error(ErrorError,TRUE,"Illegal cop register %d (using 0).",no);
  84.   }
  85. }
  86.  
  87. void c_ltorg(void)
  88. {
  89.   if(areaCurrent) {
  90.     litOrg(areaCurrent->area.info->lits);
  91.   } else {
  92.     error(ErrorError,TRUE,"No area defined.");
  93.   }
  94. }
  95.  
  96. static void defineint(int size)
  97. {
  98.   Value value;
  99.   WORD word = 0;
  100.   int c;
  101.   do {
  102.     skipblanks();
  103.     exprBuild();
  104.     value = exprEval(ValueInt | ValueString|ValueCode|ValueLateLabel);
  105.     switch(value.Tag) {
  106.     case ValueInt:
  107.       word = fixInt(inputLineNo,size,value.ValueInt.i);
  108.       putData(size,word);
  109.       break;
  110.     case ValueString:
  111.       if(size == 1) { /* Lay out a string */
  112.         int len = value.ValueString.len;
  113.         char *str = value.ValueString.s;
  114.         while(len>0)
  115.           putData(1,lexGetCharFromString(&len,&str));
  116.       } else {
  117.         putData(size,lexChar2Int(FALSE,value.ValueString.len,value.ValueString.s));
  118.       }
  119.       break;
  120.     case ValueCode: case ValueLateLabel:
  121.       relocInt(size,value);
  122.       putData(size,word);
  123.       break;
  124.     default:
  125.       error(ErrorError,TRUE,"Illegal int expression.");
  126.     }
  127.     skipblanks();
  128.   } while(((c=inputGet())!=0) && c == ',');
  129.   inputUnGet(c);
  130. }
  131.  
  132. void c_dcb(void)
  133. {
  134.   defineint(1);
  135. }
  136.  
  137. void c_dcw(void)
  138. {
  139.   defineint(2);
  140. }
  141.  
  142. void c_dcd(void)
  143. {
  144.   defineint(4);
  145. }
  146.  
  147. static void definereal(int size)
  148. {
  149.   Value value;
  150.   int c;
  151.   do {
  152.     skipblanks();
  153.     exprBuild();
  154.     value = exprEval(ValueFloat | ValueLateLabel|ValueCode);
  155.     switch(value.Tag) {
  156.     case ValueFloat:
  157.       putDataFloat(size,value.ValueFloat.f);
  158.       break;
  159.     case ValueCode: case ValueLateLabel:
  160.       relocFloat(size,value);
  161.       putDataFloat(size,0.0);
  162.       break;
  163.     default:
  164.       error(ErrorError,TRUE,"Illegal float expression.");
  165.     }
  166.     skipblanks();
  167.   } while(((c=inputGet())!=0) && c == ',');
  168.   inputUnGet(c);
  169. }
  170.  
  171. void c_dcfd(void)
  172. {
  173.   definereal(8);
  174. }
  175.  
  176. void c_dcfe(void)
  177. {
  178.   error(ErrorError,FALSE,"Not implemented dcfe %s",inputRest());
  179. }
  180.  
  181. void c_dcfs(void)
  182. {
  183.   definereal(4);
  184. }
  185.  
  186. void c_dcfp(void)
  187. {
  188.   error(ErrorError,FALSE,"Not implemented dcfp %s",inputRest());
  189. }
  190.  
  191. void c_globl(void)
  192. {
  193.   Lex lex;
  194.   Symbol *sym;
  195.   lex = lexGetId();
  196.   sym = symbolGet(lex);
  197.   sym->type |= SYMBOL_REFERENCE;
  198. }
  199.  
  200. extern int verbose;
  201.  
  202. char *strdup(char *src)
  203. {
  204.   char *dst = malloc(strlen(src)+1);
  205.   if(dst)
  206.     strcpy(dst,src);
  207.   return dst;
  208. }
  209.  
  210. void c_get(void)
  211. {
  212.   char *filename, *cptr;
  213.   FILE *getfp;
  214.  
  215.   filename = strdup(inputRest());
  216.   if (!filename)
  217.     error(ErrorSerious, TRUE, "strdup failed in c_get()");
  218.   for (cptr = filename; !isspace(*cptr); cptr++)
  219.     ;
  220.   *cptr = '\0';
  221.   inputNextLine();
  222.   getfp = getInclude(filename);
  223.   if (!getfp) {
  224.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  225.     return;
  226.   }
  227.   push_file(asmfile);
  228.   asmfile = getfp;
  229.   if(verbose)
  230.     printf("including file %s\n", filename);
  231. }
  232.  
  233. void c_lnk(void)
  234. {
  235.   char *filename, *cptr;
  236.   FILE *lnkfp;
  237.  
  238.   filename = strdup(inputRest());
  239.   if (!filename)
  240.     error(ErrorSerious, TRUE, "strdup failed in c_lnk()");
  241.   for (cptr = filename; !isspace(*cptr); cptr++)
  242.     ;
  243.   *cptr = '\0';
  244.   inputNextLine();
  245.   lnkfp = getInclude(filename);
  246.   if (!lnkfp) {
  247.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  248.     return;
  249.   }
  250.   skiprest();
  251.   inputFinish();
  252.   asmfile = lnkfp;
  253.   if(verbose)
  254.     printf("linking to file %s\n", filename);
  255. }
  256.