assembler.lex 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. %{
  2. /*
  3. * Andrea Di Biagio
  4. * Politecnico di Milano, 2007
  5. *
  6. * assembler.lex
  7. * Formal Languages & Compilers Machine, 2007/2008
  8. *
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15. #include "asm_struct.h"
  16. #include "collections.h"
  17. #include "assembler.tab.h"
  18. extern int line_num;
  19. extern int num_error;
  20. %}
  21. %x comment
  22. %s DATA
  23. %option noyywrap
  24. DIGIT [0-9]
  25. ID [a-zA-Z_][a-zA-Z0-9_]*
  26. %%
  27. [ \t\f\v]+ { /* Ignore whitespace. */ }
  28. "/*" { BEGIN(comment); return BEGIN_COMMENT; }
  29. <comment>[^*]* { yylval.svalue = strdup(yytext); return COMMENT; }
  30. <comment>"*"+[^*/]* { yylval.svalue = strdup(yytext); return COMMENT; }
  31. <comment>"*"+"/" { BEGIN(INITIAL); return END_COMMENT; }
  32. "(" { return LPAR; }
  33. ")" { return RPAR; }
  34. ":" { return COLON; }
  35. "#" { return BEGIN_IMMEDIATE; }
  36. "-" { return MINUS; }
  37. "add"|"ADD" {yylval.opcode = ADD_OP; return OPCODE3; }
  38. "sub"|"SUB" {yylval.opcode = SUB_OP; return OPCODE3; }
  39. "andl"|"ANDL" {yylval.opcode = ANDL_OP; return OPCODE3; }
  40. "orl"|"ORL" {yylval.opcode = ORL_OP; return OPCODE3; }
  41. "xorl"|"XORL" {yylval.opcode = XORL_OP; return OPCODE3; }
  42. "andb"|"ANDB" {yylval.opcode = ANDB_OP; return OPCODE3; }
  43. "orb"|"ORB" {yylval.opcode = ORB_OP; return OPCODE3; }
  44. "xorb"|"XORB" {yylval.opcode = XORB_OP; return OPCODE3; }
  45. "mul"|"MUL" {yylval.opcode = MUL_OP; return OPCODE3; }
  46. "div"|"DIV" {yylval.opcode = DIV_OP; return OPCODE3; }
  47. "shl"|"SHL" {yylval.opcode = SHL_OP; return OPCODE3; }
  48. "shr"|"SHR" {yylval.opcode = SHR_OP; return OPCODE3; }
  49. "rotl"|"ROTL" {yylval.opcode = ROTL_OP; return OPCODE3; }
  50. "rotr"|"ROTR" {yylval.opcode = ROTR_OP; return OPCODE3; }
  51. "neg"|"NEG" {yylval.opcode = NEG_OP; return OPCODE3; }
  52. "spcl"|"SPCL" {yylval.opcode = SPCL_OP; return OPCODE3; }
  53. "addi"|"ADDI" {yylval.opcode = ADDI_OP; return OPCODE2; }
  54. "subi"|"SUBI" {yylval.opcode = SUBI_OP; return OPCODE2; }
  55. "andli"|"ANDLI" {yylval.opcode = ANDLI_OP; return OPCODE2; }
  56. "orli"|"ORLI" {yylval.opcode = ORLI_OP; return OPCODE2; }
  57. "xorli"|"XORLI" {yylval.opcode = XORLI_OP; return OPCODE2; }
  58. "andbi"|"ANDBI" {yylval.opcode = ANDBI_OP; return OPCODE2; }
  59. "orbi"|"ORBI" {yylval.opcode = ORBI_OP; return OPCODE2; }
  60. "xorbi"|"XORBI" {yylval.opcode = XORBI_OP; return OPCODE2; }
  61. "muli"|"MULI" {yylval.opcode = MULI_OP; return OPCODE2; }
  62. "divi"|"DIVI" {yylval.opcode = DIVI_OP; return OPCODE2; }
  63. "shli"|"SHLI" {yylval.opcode = SHLI_OP; return OPCODE2; }
  64. "shri"|"SHRI" {yylval.opcode = SHRI_OP; return OPCODE2; }
  65. "rotli"|"ROTLI" {yylval.opcode = ROTLI_OP; return OPCODE2; }
  66. "rotri"|"ROTRI" {yylval.opcode = ROTRI_OP; return OPCODE2; }
  67. "notl"|"NOTL" {yylval.opcode = NOTL_OP; return OPCODE2; }
  68. "notb"|"NOTB" {yylval.opcode = NOTB_OP; return OPCODE2; }
  69. "nop"|"NOP" {yylval.opcode = NOP_OP; return NOP; }
  70. "mova"|"MOVA" {yylval.opcode = MOVA_OP; return OPCODEI; }
  71. "load"|"LOAD" {yylval.opcode = LOAD_OP; return OPCODEI; }
  72. "store"|"STORE" {yylval.opcode = STORE_OP; return OPCODEI; }
  73. "jsr"|"JSR" {yylval.opcode = JSR_OP; return OPCODEI; }
  74. "ret"|"RET" {yylval.opcode = RET_OP; return OPCODEI; }
  75. "seq"|"SEQ" {yylval.opcode = SEQ_OP; return OPCODEI; }
  76. "sge"|"SGE" {yylval.opcode = SGE_OP; return OPCODEI; }
  77. "sgt"|"SGT" {yylval.opcode = SGT_OP; return OPCODEI; }
  78. "sle"|"SLE" {yylval.opcode = SLE_OP; return OPCODEI; }
  79. "slt"|"SLT" {yylval.opcode = SLT_OP; return OPCODEI; }
  80. "sne"|"SNE" {yylval.opcode = SNE_OP; return OPCODEI; }
  81. "read"|"READ" {yylval.opcode = READ_OP; return OPCODEI; }
  82. "write"|"WRITE" {yylval.opcode = WRITE_OP; return OPCODEI; }
  83. "halt"|"HALT" {yylval.opcode = HALT_OP; return HALT; }
  84. "bt"|"BT" {yylval.opcode = BT_OP; return CCODE; }
  85. "bf"|"BF" {yylval.opcode = BF_OP; return CCODE; }
  86. "bhi"|"BHI" {yylval.opcode = BHI_OP; return CCODE; }
  87. "bls"|"BLS" {yylval.opcode = BLS_OP; return CCODE; }
  88. "bcc"|"BCC" {yylval.opcode = BCC_OP; return CCODE; }
  89. "bcs"|"BCS" {yylval.opcode = BCS_OP; return CCODE; }
  90. "bne"|"BNE" {yylval.opcode = BNE_OP; return CCODE; }
  91. "beq"|"BEQ" {yylval.opcode = BEQ_OP; return CCODE; }
  92. "bvc"|"BVC" {yylval.opcode = BVC_OP; return CCODE; }
  93. "bvs"|"BVS" {yylval.opcode = BVS_OP; return CCODE; }
  94. "bpl"|"BPL" {yylval.opcode = BPL_OP; return CCODE; }
  95. "bmi"|"BMI" {yylval.opcode = BMI_OP; return CCODE; }
  96. "bge"|"BGE" {yylval.opcode = BGE_OP; return CCODE; }
  97. "blt"|"BLT" {yylval.opcode = BLT_OP; return CCODE; }
  98. "bgt"|"BGT" {yylval.opcode = BGT_OP; return CCODE; }
  99. "ble"|"BLE" {yylval.opcode = BLE_OP; return CCODE; }
  100. ".data"|".DATA" BEGIN(DATA); {line_num++; }
  101. ".text"|".TEXT" BEGIN(INITIAL); {line_num++; }
  102. <DATA>".word"|".WORD" {return _WORD;}
  103. <DATA>".space"|".SPACE" {return _SPACE;}
  104. ["R"|"r"]{DIGIT}+ { yylval.immediate = atoi(&yytext[1]); return REG; }
  105. \n { /* DOES NOTHING */ }
  106. {DIGIT}+ { yylval.immediate = atoi(yytext); return IMM; };
  107. {ID} { yylval.svalue = strdup(yytext); return ETI; };
  108. . { return(yytext[0]); num_error++; }
  109. %%