home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / as_1 / source_c_commands < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-13  |  6.3 KB  |  314 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.   extern int gcc_backend;
  234.   char *filename, *cptr;
  235.   FILE *getfp;
  236.  
  237.   filename = strdup(inputRest());
  238.   if (!filename)
  239.     error(ErrorSerious, TRUE, "strdup failed in c_get()");
  240.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  241.     ;
  242.   if(*cptr) {
  243.     *cptr++ = '\0';  /* must be a space */
  244.     while(*cptr && isspace(*cptr))
  245.       cptr++;
  246.     if(*cptr && *cptr != ';' && (!gcc_backend || *cptr != '@')) {
  247.       error(ErrorError,FALSE,"Skipping extra characters '%s' after filename.",cptr);
  248.     }
  249.   }
  250.   getfp = getInclude(filename,"r");
  251.   if (!getfp) {
  252.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  253.     return;
  254.   }
  255.   push_file(asmfile);
  256.   asmfile = getfp;
  257.   if(verbose)
  258.     printf("including file %s\n", filename);
  259. }
  260.  
  261. void c_lnk(void)
  262. {
  263.   char *filename, *cptr;
  264.   FILE *lnkfp;
  265.  
  266.   filename = strdup(inputRest());
  267.   if (!filename)
  268.     error(ErrorSerious, TRUE, "strdup failed in c_lnk()");
  269.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  270.     ;
  271.   *cptr = '\0';
  272.   inputNextLine();
  273.   lnkfp = getInclude(filename,"r");
  274.   if (!lnkfp) {
  275.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  276.     return;
  277.   }
  278.   skiprest();
  279.   inputFinish();
  280.   asmfile = lnkfp;
  281.   if(verbose)
  282.     printf("linking to file %s\n", filename);
  283. }
  284.  
  285. void c_idfn(void)
  286. {
  287.   idfn_text = strdup(inputRest());
  288.   skiprest();
  289. }
  290.  
  291. void c_bin(void)
  292. {
  293.   char *filename, *cptr;
  294.   FILE *binfp;
  295.  
  296.   filename = strdup(inputRest());
  297.   if (!filename)
  298.     error(ErrorSerious, TRUE, "strdup failed in c_bin()");
  299.   for (cptr = filename; *cptr && !isspace(*cptr); cptr++)
  300.     ;
  301.   *cptr = '\0';
  302.  
  303.   binfp = getInclude(filename,"r");
  304.   if (!binfp) {
  305.     error(ErrorError, TRUE, "cannot open file \"%s\"", filename);
  306.     return;
  307.   }
  308.   if(verbose)
  309.     printf("including file %s\n", filename);
  310.   while(!feof(binfp))
  311.     putData(1,getc(binfp));
  312.   fclose(binfp);
  313. }
  314.