home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / a / as1_23 / source / c / commands < prev    next >
Encoding:
Text File  |  1993-12-28  |  6.0 KB  |  307 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 "output.h"
  14. #include "lex.h"
  15. #include "help_lex.h"
  16. #include "symbol.h"
  17. #include "expr.h"
  18. #include "code.h"
  19. #include "value.h"
  20. #include "put.h"
  21. #include "commands.h"
  22. #include "fix.h"
  23. #include "include.h"
  24. #include "filestack.h"
  25. #include "area.h"
  26. #include "lit.h"
  27.  
  28. extern FILE *asmfile;  /* in input.c */
  29.  
  30.  
  31. static Symbol *c_define(char *msg, Symbol *sym, ValueTag legal)
  32. {
  33.   Value value;
  34.   if(!sym)
  35.     error(ErrorError,FALSE,"Missing label before %s.");
  36.   sym->type |= SYMBOL_ABSOLUTE;
  37.   exprBuild();
  38.   value = exprEval(legal);
  39.   switch(value.Tag) {
  40.     case ValueIllegal:
  41.       error(ErrorError,TRUE,"Illegal %s.",msg);
  42.       sym->value.Tag = ValueInt;
  43.       sym->value.ValueInt.i = 0;
  44.       break;
  45.     default:
  46.       sym->value = valueCopy(value);
  47.   }
  48.   sym->type |= SYMBOL_DEFINED;
  49.   sym->area.ptr = 0;
  50.   return sym;
  51. }
  52.  
  53. void c_equ(Symbol *symbol)
  54. {
  55.   (void)c_define("equ",symbol,ValueAll);
  56. }
  57.  
  58. void c_fn(Symbol *symbol)
  59. {
  60.   int no = c_define("float register",symbol,ValueInt)->value.ValueInt.i;
  61.   symbol->type |= SYMBOL_FPUREG;
  62.   if(no<0 || no>7) {
  63.     symbol->value.ValueInt.i = 0;
  64.     error(ErrorError,TRUE,"Illegal fpu register %d (using 0).",no);
  65.   }
  66. }
  67.  
  68. void c_rn(Symbol *symbol)
  69. {
  70.   int no=c_define("register",symbol,ValueInt)->value.ValueInt.i;
  71.   symbol->type |= SYMBOL_CPUREG;
  72.   if(no<0 || no>15) {
  73.     symbol->value.ValueInt.i = 0;
  74.     error(ErrorError,TRUE,"Illegal cpu register %d (using 0).",no);
  75.   }
  76. }
  77.  
  78. void c_cn(Symbol *symbol)
  79. {
  80.   int no = c_define("register",symbol,ValueInt)->value.ValueInt.i;
  81.   symbol->type |= SYMBOL_COPREG;
  82.   if(no<0 || no>15) {
  83.     symbol->value.ValueInt.i = 0;
  84.     error(ErrorError,TRUE,"Illegal cop register %d (using 0).",no);
  85.   }
  86. }
  87.  
  88. void c_ltorg(void)
  89. {
  90.   if(areaCurrent) {
  91.     litOrg(areaCurrent->area.info->lits);
  92.   } else {
  93.     error(ErrorError,TRUE,"No area defined.");
  94.   }
  95. }
  96.  
  97. static void defineint(int size)
  98. {
  99.   Value value;
  100.   WORD word = 0;
  101.   int c;
  102.   do {
  103.     skipblanks();
  104.     exprBuild();
  105.     value = exprEval(ValueInt | ValueString|ValueCode|ValueLateLabel);
  106.     switch(value.Tag) {
  107.     case ValueInt:
  108.       word = fixInt(inputLineNo,size,value.ValueInt.i);
  109.       putData(size,word);
  110.       break;
  111.     case ValueString:
  112.       if(size == 1) { /* Lay out a string */
  113.         int len = value.ValueString.len;
  114.         char *str = value.ValueString.s;
  115.         while(len>0)
  116.           putData(1,lexGetCharFromString(&len,&str));
  117.       } else {
  118.         putData(size,lexChar2Int(FALSE,value.ValueString.len,value.ValueString.s));
  119.       }
  120.       break;
  121.     case ValueCode: case ValueLateLabel:
  122.       relocInt(size,value);
  123.       putData(size,word);
  124.       break;
  125.     default:
  126.       error(ErrorError,TRUE,"Illegal int expression.");
  127.     }
  128.     skipblanks();
  129.   } while(((c=inputGet())!=0) && c == ',');
  130.   inputUnGet(c);
  131. }
  132.  
  133. void c_dcb(void)
  134. {
  135.   defineint(1);
  136. }
  137.  
  138. void c_dcw(void)
  139. {
  140.   defineint(2);
  141. }
  142.  
  143. void c_dcd(void)
  144. {
  145.   defineint(4);
  146. }
  147.  
  148. static void definereal(int size)
  149. {
  150.   Value value;
  151.   int c;
  152.   do {
  153.     skipblanks();
  154.     exprBuild();
  155.     value = exprEval(ValueFloat | ValueLateLabel|ValueCode);
  156.     switch(value.Tag) {
  157.     case ValueFloat:
  158.       putDataFloat(size,value.ValueFloat.f);
  159.       break;
  160.     case ValueCode: case ValueLateLabel:
  161.       relocFloat(size,value);
  162.       putDataFloat(size,0.0);
  163.       break;
  164.     default:
  165.       error(ErrorError,TRUE,"Illegal float expression.");
  166.     }
  167.     skipblanks();
  168.   } while(((c=inputGet())!=0) && c == ',');
  169.   inputUnGet(c);
  170. }
  171.  
  172. void c_dcfd(void)
  173. {
  174.   definereal(8);
  175. }
  176.  
  177. void c_dcfe(void)
  178. {
  179.   error(ErrorError,FALSE,"Not implemented dcfe %s",inputRest());
  180. }
  181.  
  182. void c_dcfs(void)
  183. {
  184.   definereal(4);
  185. }
  186.  
  187. void c_dcfp(void)
  188. {
  189.   error(ErrorError,FALSE,"Not implemented dcfp %s",inputRest());
  190. }
  191.  
  192. void c_globl(void)
  193. {
  194.   Lex lex;
  195.   Symbol *sym;
  196.   lex = lexGetId();
  197.   sym = symbolGet(lex);
  198.   sym->type |= SYMBOL_REFERENCE;
  199. }
  200.  
  201. void c_import(void)
  202. {
  203.   Lex lex;
  204.   int c;
  205.   Symbol *sym;
  206.   lex = lexGetId();
  207.   sym = symbolGet(lex);
  208.   sym->type |= SYMBOL_REFERENCE;
  209.   if ((c=inputGet()) == ',') {
  210.     Lex attribute = lexGetId();
  211.     if(!strncmp("WEAK",attribute.LexId.str,attribute.LexId.len))
  212.       sym->type |= SYMBOL_WEAK;
  213.     else
  214.       error(ErrorError,TRUE,"Illegal IMPORT attribute %s.",attribute.LexId.str);
  215.   }
  216. }
  217.  
  218. extern int verbose;
  219.  
  220. #if !(defined(UNIX) && defined(USG))
  221. char *strdup(char *src)
  222. {
  223.   char *dst = malloc(strlen(src)+1);
  224.   if(dst)
  225.     strcpy(dst,src);
  226.   return dst;
  227. }
  228. #endif
  229.  
  230.  
  231. void c_get(void)
  232. {
  233.   char *filename, *cptr;
  234.   FILE *getfp;
  235.  
  236.   filename = strdup(inputRest());
  237.   if (!filename)
  238.     error(ErrorSerious, TRUE, "strdup failed in c_get()");
  239.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  240.     ;
  241.   *cptr = '\0';
  242.   inputNextLine();
  243.   getfp = getInclude(filename,"r");
  244.   if (!getfp) {
  245.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  246.     return;
  247.   }
  248.   push_file(asmfile);
  249.   asmfile = getfp;
  250.   if(verbose)
  251.     printf("including file %s\n", filename);
  252. }
  253.  
  254. void c_lnk(void)
  255. {
  256.   char *filename, *cptr;
  257.   FILE *lnkfp;
  258.  
  259.   filename = strdup(inputRest());
  260.   if (!filename)
  261.     error(ErrorSerious, TRUE, "strdup failed in c_lnk()");
  262.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  263.     ;
  264.   *cptr = '\0';
  265.   inputNextLine();
  266.   lnkfp = getInclude(filename,"r");
  267.   if (!lnkfp) {
  268.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  269.     return;
  270.   }
  271.   skiprest();
  272.   inputFinish();
  273.   asmfile = lnkfp;
  274.   if(verbose)
  275.     printf("linking to file %s\n", filename);
  276. }
  277.  
  278. void c_idfn(void)
  279. {
  280.   idfn_text = strdup(inputRest());
  281.   skiprest();
  282. }
  283.  
  284. void c_bin(void)
  285. {
  286.   char *filename, *cptr;
  287.   FILE *binfp;
  288.  
  289.   filename = strdup(inputRest());
  290.   if (!filename)
  291.     error(ErrorSerious, TRUE, "strdup failed in c_bin()");
  292.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  293.     ;
  294.   *cptr = '\0';
  295.  
  296.   binfp = getInclude(filename,"r");
  297.   if (!binfp) {
  298.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  299.     return;
  300.   }
  301.   if(verbose)
  302.     printf("including file %s\n", filename);
  303.   while(!feof(binfp))
  304.     putData(1,getc(binfp));
  305.   fclose(binfp);
  306. }
  307.