home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / rdp / rdp_cs3460.tar / mvmasm.bnf < prev    next >
Text File  |  1998-05-07  |  7KB  |  163 lines

  1. (*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * mvmasm.bnf - an assembler for Mini Virtual Machine assembler language
  6. *
  7. * This file may be freely distributed. Please mail improvements to the author.
  8. *
  9. *******************************************************************************)
  10. TITLE("mvmasm v1.5 - absolute assembler for mvm")
  11. SUFFIX("mvm")
  12. USES("mvm_aux.h")
  13. USES("math.h")
  14. USES("mvm_def.h")
  15. PASSES(3)
  16. PARSER(unit) (* name of start production *)
  17.  
  18. PRE_PARSE([* init(rdp_outputfilename); *])
  19. POST_PARSE([* quit(rdp_outputfilename); *])
  20.  
  21. ARG_BOOLEAN(x execute_sim "execute assembled code using mvmsim simulator")
  22. ARG_BLANK("")
  23. ARG_BLANK("You can contact the author (Adrian Johnstone) at:")
  24. ARG_BLANK("")
  25. ARG_BLANK("Computer Science Department, Royal Holloway, University of London")
  26. ARG_BLANK("Egham, Surrey, TW20 0EX UK. Email: A.Johnstone@rhbnc.ac.uk")
  27.  
  28. SYMBOL_TABLE(mvmasm 101 31
  29.          symbol_compare_string
  30.          symbol_hash_string
  31.          symbol_print_string
  32.          [* char *id;
  33.                integer val;
  34.          *]
  35.         )
  36.  
  37. unit    ::= [* emit_code = (rdp_pass == 3);
  38.            data_location = code_location = 0;  /* clear location counters */
  39.            location = &code_location;          /* make code counter current */
  40.            dummy_label = symbol_new_symbol(sizeof(mvmasm_data));             /* initialise error symbol */
  41.         *]
  42.         { code }.
  43.  
  44. code  ::= [* emit_eoln(); emit_loc(); last_label = NULL; *]
  45.           [label ':'] [instr] [* emit_fill(); *] EOLN.
  46.  
  47. label ::= ID:lab
  48.       [* if ((last_label = symbol_lookup_key(mvmasm, &lab, NULL)) == NULL)
  49.            last_label = symbol_insert_key(mvmasm, &lab, sizeof(char*), sizeof(mvmasm_data));
  50.          mvmasm_cast(last_label)->val = *location;
  51.       *].
  52.  
  53. instr   ::= diadic | copy | branch | print | halt | directive.
  54.  
  55. diadic ::= [* int op, m1 = 1, m2 = 1; *]
  56.            (
  57.            'ADD' [* op = OP_ADD; *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  58.            'SUB' [* op = OP_SUB; *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  59.            'MUL' [* op = OP_MUL; *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  60.            'DIV' [* op = OP_DIV; *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  61.            'EXP' [* op = OP_EXP; *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  62.  
  63.            'EQ'  [* op = OP_EQ;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  64.            'NE'  [* op = OP_NE;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  65.            'GT'  [* op = OP_GT;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  66.            'GE'  [* op = OP_GE;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  67.            'LT'  [* op = OP_LT;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 |
  68.            'LE'  [* op = OP_LE;  *] e1: dst ',' ['#' [* m1=0; *] ] e1: src1 ',' ['#' [* m2=0; *] ] e1:src2 
  69.            ) [* emit_op(op, dst, src1, src2, m1, m2, 3); *] .
  70.  
  71. copy   ::= [* int m1 = 1; *]
  72.            'CPY' e1: dst ',' ['#' [* m1=0; *] ] e1: src [* emit_op(OP_CPY, dst, src, 0, m1, 1, 2); *] .
  73.  
  74. branch ::= 'BEQ' e1: src ',' e1: label [* emit_op(OP_BEQ, label, src, 0, 1, 1, 2); *] |
  75.            'BNE' e1: src ',' e1: label [* emit_op(OP_BNE, label, src, 0, 1, 1, 2); *] |
  76.            'BRA' e1: label [* emit_op(OP_BEQ, label, 0, 0, 0, 1, 2); /* force immediate mode */ *] .
  77.  
  78. print  ::= [* int m1 = 1; *]
  79.            (
  80.            'PRTS' e1: src [* emit_op(OP_PRTS, 0, src, 0, 0, 1, 2); /* force immediate mode */ *] |
  81.            'PRTI' ['#' [* m1=0; *] ] e1: src [* emit_op(OP_PRTI, 0, src, 0, m1, 1, 2); *]
  82.            ).
  83.  
  84. halt   ::= 'HALT' [* emit_op(OP_HALT, 0, 0, 0, 1, 1, 0); *] .
  85.  
  86. directive  ::= 'INCLUDE' '(' string: filename ')'
  87.            [* if (text_open(filename) == NULL)
  88.              text_message(TEXT_ERROR_ECHO, "include file '%s' not found\n", filename);
  89.           else
  90.           {
  91.             text_get_char();
  92.             scan_();
  93.           }
  94.            *] |
  95.  
  96.            'CODE' [* location = &code_location; *] [ e1:n [* *location = n; *] ] |
  97.            'DATA' [* location = &data_location; *] [ e1:n [* *location = n; *] ] |
  98.            'WORD' e1:val [* emit2(val); *] |
  99.            'BLOCKW' e1:val [* *location += 2 * val; *] |
  100.            'STRING' string:str [* while (*str!=0) emit1(*str++); emit1(0); *] |
  101.            'EQU' e1:val [* mvmasm_cast(current_label())->val = val; *] |
  102.                'END' e1: val [* transfer = val; emit_transfer(); *] .
  103.  
  104. (* Expression interpreter using C operators and long int data ***************)
  105.  
  106. e1:integer ::= e2:result ['>'  e2:right [* result = result > right;  *] |  (* Greater than *)
  107.               '<'  e2:right [* result = result < right;  *] |  (* Less than *)
  108.               '>=' e2:right [* result = result >= right; *] |  (* Greater than or equal to *)
  109.               '<=' e2:right [* result = result <= right; *] |  (* Less than or equal to *)
  110.               '==' e2:right [* result = result == right; *] |  (* Equal to *)
  111.               '!=' e2:right [* result = result != right; *] ]. (* Not equal to *)
  112.  
  113. e2:integer ::= e3:result {'||' e3:right  [* result = result || right; *]}. (* Logical inclusive OR *)
  114.  
  115. e3:integer ::= e4:result {'&&' e4:right  [* result = result && right; *]}. (* Logical AND *)
  116.  
  117. e4:integer ::= e5:result {'^' e5:right  [* result ^= right; *]}.           (* Bitwise exclusive OR *)
  118.  
  119. e5:integer ::= e6:result {'|' e6:right  [* result |= right; *]}.           (* Bitwise inclusive OR *)
  120.  
  121. e6:integer ::= e7:result {'&' e7:right  [* result &= right; *]}.           (* Bitwise AND *)
  122.  
  123. e7:integer ::= e8:result {'<<' e8:right [* result <<= right; *] |          (* Shift left  *)
  124.               '>>' e8:right [* result >>= right; *] }.         (* Shift right *)
  125.  
  126. e8:integer ::= e9:result {'+' e9:right  [* result += right; *] |           (* Add *)
  127.               '-' e9:right  [* result -= right; *] }.          (* Subtract *)
  128.  
  129. e9:integer ::= e10:result {'*' e10:right  [* result *= right; *] |         (* Divide *)
  130.                '/' e10:right  [* result /= right; *] }.        (* Multiply *)
  131.  
  132. e10:integer ::= '+' e10:result |                                           (* Posite *)
  133.         '-' e10:result [* result = -result; *] |                   (* Negate *)
  134.         '~' e10:result [* result = ~result; *] |                   (* Bitwise complement *)
  135.         '!' e10:result [* result = !result; *] |                   (* Logical not *)
  136.         e11:result.
  137.  
  138. e11:integer ::= e0:result ['**' e10:right [* result = (integer) pow((double) result, (double) right); *]]. (* Exponentiate *)
  139.  
  140. e0:integer ::= [* mvmasm_data* temp; *]
  141.            ID:name
  142.            [* temp = mvmasm_cast(symbol_lookup_key(mvmasm, &name, NULL));
  143.           if (temp == NULL)
  144.           {
  145.                     if (rdp_pass == 3)
  146.                text_message(TEXT_ERROR_ECHO,"Undefined symbol '%s'\n", name);
  147.             result = 0;
  148.           }
  149.           else
  150.             result = temp->val;
  151.            *] |                                                        (* Variable *)
  152.            INTEGER:result |                                            (* Numeric literal *)
  153.            'TRUE' [* result = 1; *] |                                  (* Logical TRUE *)
  154.            'FALSE' [* result = 0; *] |                                 (* Logical FALSE *)
  155.  
  156.            '(' e2:result ')'.                                          (* Do-first *)
  157.  
  158. string: char* ::= STRING_ESC('"' '\\'):result.
  159.  
  160. Comment ::= COMMENT_LINE(';').
  161.  
  162.  
  163.