home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / as / source / c / get < prev    next >
Encoding:
Text File  |  1994-09-27  |  5.2 KB  |  211 lines

  1.  
  2. /*
  3.  * get.c
  4.  * Copyright © 1992 Niklas Röjemo
  5.  */
  6.  
  7. #include <string.h>
  8. #include "get.h"
  9. #include "error.h"
  10. #include "input.h"
  11. #include "lex.h"
  12. #include "symbol.h"
  13. #include "expr.h"
  14. #include "reloc.h"
  15. #include "help_cpu.h"
  16. #include "fix.h"
  17.  
  18. extern int gcc_backend;
  19.  
  20. struct reg_id {
  21.     int len;
  22.     char *name;
  23.   };
  24.  
  25. WORD getCpuReg(void)
  26. {
  27.   static struct reg_id cpu_regs[16] =
  28.   {2, "r0", 2, "r1", 2, "r2", 2, "r3", 2, "r4", 2, "r5", 2, "r6", 2, "r7",
  29.    2, "r8", 3, "rfp", 2, "sl", 2, "fp", 2, "ip", 2, "sp", 2, "lr", 2, "pc"};
  30.  
  31.   Lex lexSym;
  32.   int loop;
  33.   Symbol *sym;
  34.  
  35.   lexSym = lexGetId ();
  36.   if (gcc_backend && (lexSym.tag == LexId))
  37.     for (loop = 0; loop < 16; loop++) {
  38.       if ((cpu_regs[loop].len == lexSym.LexId.len) &&
  39.       !strncmp (cpu_regs[loop].name, lexSym.LexId.str, lexSym.LexId.len))
  40.     return loop;
  41.     }
  42.  
  43.   sym = symbolGet (lexSym);
  44.  
  45.   if(sym->type & SYMBOL_DEFINED && sym->type & SYMBOL_ABSOLUTE) {
  46.     if(SYMBOL_GETREG(sym->type) == SYMBOL_CPUREG)
  47.       return sym->value.ValueInt.i;
  48.     else
  49.       error(ErrorError,TRUE,"'%s' (=%d) is not a cpu register.",sym->str,sym->value);
  50.   } else
  51.     error(ErrorError,TRUE,"Undefined register %s.",sym->str);
  52.   return 0;
  53. }
  54.  
  55. WORD getFpuReg(void)
  56. {
  57.   /* NB since len is fixed at 2, we dont actualy use the length */
  58.   static struct reg_id cpu_regs[8] =
  59.   {2, "f0", 2, "f1", 2, "f2", 2, "f3", 2, "f4", 2, "f5", 2, "f6", 2, "f7"};
  60.  
  61.   Lex lexSym;
  62.   Symbol *sym;
  63.   int loop;
  64.  
  65.   lexSym = lexGetId ();
  66.   if (gcc_backend && (lexSym.tag == LexId) && (lexSym.LexId.len == 2))
  67.     for (loop = 0; loop < 8; loop++) {
  68.       if (!strncmp (cpu_regs[loop].name, lexSym.LexId.str, 2))
  69.         return loop;
  70.     }
  71.  
  72.   sym = symbolGet (lexSym);
  73.  
  74.  
  75.   if(sym->type & SYMBOL_DEFINED && sym->type & SYMBOL_ABSOLUTE) {
  76.     if(SYMBOL_GETREG(sym->type) == SYMBOL_FPUREG)
  77.       return sym->value.ValueInt.i;
  78.     else
  79.       error(ErrorError,TRUE,"'%s' (=%d) is not a fpu register.",sym->str,sym->value);
  80.   } else
  81.     error(ErrorError,TRUE,"Undefined float register %s.",sym->str);
  82.   return 0;
  83. }
  84.  
  85. WORD getCopReg(void)
  86. {
  87.   Symbol *sym = symbolGet(lexGetId());
  88.  
  89.   if(sym->type & SYMBOL_DEFINED && sym->type & SYMBOL_ABSOLUTE) {
  90.     if(SYMBOL_GETREG(sym->type) == SYMBOL_COPREG)
  91.       return sym->value.ValueInt.i;
  92.     else
  93.       error(ErrorError,TRUE,"'%s' (=%d) is not a cop register.",sym->str,sym->value);
  94.   } else
  95.     error(ErrorError,TRUE,"Undefined coprocessor register %s.",sym->str);
  96.   return 0;
  97. }
  98.  
  99. static WORD getShiftOp(void)
  100. {
  101.   WORD r;
  102.   switch(inputLook()) {
  103.     case 'a':case 'A':
  104.       if(inputLookN(1) == 's' || inputLookN(1) == 'S') {
  105.         switch(inputLookN(2)) {
  106.           case 'l':case 'L': r = ASL; inputSkipN(3); break;
  107.           case 'r':case 'R': r = ASR; inputSkipN(3); break;
  108.           default:  goto illegal;
  109.         }
  110.       } else
  111.         goto illegal;
  112.       break;
  113.     case 'l':case 'L':
  114.       if(inputLookN(1) == 's' || inputLookN(1) == 'S') {
  115.         switch(inputLookN(2)) {
  116.           case 'l':case 'L': r = LSL; inputSkipN(3); break;
  117.           case 'r':case 'R': r = LSR; inputSkipN(3); break;
  118.           default:  goto illegal;
  119.         }
  120.       } else
  121.         goto illegal;
  122.       break;
  123.     case 'r':case 'R':
  124.       switch(inputLookN(1)) {
  125.         case 'o':case 'O':
  126.           if(inputLookN(2) == 'r' || inputLookN(2) == 'R') { r = ROR; inputSkipN(3); break;}
  127.           else goto illegal;
  128.         case 'r':case 'R':
  129.           if(inputLookN(2) == 'x' || inputLookN(2) == 'X') { r = RRX; inputSkipN(3); break;}
  130.           else goto illegal;
  131.         default:   goto illegal;
  132.       }
  133.       break;
  134.     default:
  135. illegal:
  136.       error(ErrorError,TRUE,"Illegal shiftop %c%c%c.",inputLook(),inputLookN(1),inputLookN(2));
  137.   }
  138.   return r;
  139. }
  140.  
  141.  
  142. static WORD getShift(BOOL immonly)
  143. {
  144.   WORD shift;
  145.   WORD op = 0;
  146.   Value im;
  147.   shift = getShiftOp();
  148.  
  149.   if(shift != RRX) {
  150.     skipblanks();
  151.     if(inputLook() == '#') {
  152.       inputSkip();
  153.       exprBuild();
  154.       im = exprEval(ValueInt | ValueCode|ValueLateLabel);
  155.       switch(im.Tag) {
  156.       case ValueInt:
  157.         op = fixShiftImm(inputLineNo,shift,im.ValueInt.i);   /* !! Fixed !! */
  158.         break;
  159.       case ValueCode: case ValueLateLabel:
  160.         relocShiftImm(shift,im);
  161.         op = SHIFT_OP(shift);                                /* !! Fixed !! */
  162.         break;
  163.       default:
  164.         error(ErrorError,TRUE,"Illegal shift expression.");
  165.         break;
  166.       }
  167.     } else {
  168.       if(immonly) {
  169.         error(ErrorError,TRUE,"Only shift immediate allowed here.");
  170.       } else {
  171.         op = SHIFT_REG(getCpuReg()) | SHIFT_OP(shift);
  172.       }
  173.     }
  174.   } else
  175.     op = SHIFT_OP(shift);
  176.   return op;
  177. }
  178.  
  179. WORD getRhs(BOOL immonly, WORD  ir)
  180. {
  181.   Value im;
  182.   if(inputLook() == '#') {
  183.     ir |= IMM_RHS;
  184.     inputSkip();
  185.     exprBuild();
  186.     im = exprEval(ValueInt | ValueCode|ValueLateLabel);
  187.     switch(im.Tag) {
  188.       case ValueInt:
  189.         ir = fixImm8s4(inputLineNo,ir,im.ValueInt.i);
  190.         break;
  191.       case ValueCode: case ValueLateLabel:
  192.         relocImm8s4(ir,im);
  193.         break;
  194.       default:
  195.         error(ErrorError,TRUE,"Illegal immediate expression.");
  196.         break;
  197.     }
  198.   } else {
  199.     ir |= getCpuReg();
  200.     skipblanks();
  201.     if(inputLook() == ',') {
  202.       inputSkip();
  203.       skipblanks();
  204.       ir |= getShift(immonly);
  205.     } else {
  206.       ir |= NO_SHIFT;
  207.     }
  208.   }
  209.   return ir;
  210. }
  211.