1 /* This file contains the parser for the 'EM_table'.
2 * Every row in the table is converted to a C-function.
3 * The parser is a fairly simple program, for each row it prints a function-
4 * header and the following actions are printed as C-statements. A CALL is just
5 * copied, and an ASSEM_INSTR will be handed to the routine assemble().
6 * Assemble() will turn the assembly-string into a sequence of C-function calls.
7 * How this should be done is expressed in the 'as_table'.
9 * There are however some complicating factors:
10 * - A row in the 'EM_table' may contain CONDITION's.
11 * The parser supports this by outptutting some "if" and "else"'s.
13 * - The user is allowed to use some abbreviation in the 'EM_table', (s)he
14 * can use the DEF_C_INSTR's ( e.g. C_loe..).
15 * One such DEF_C_INSTR must be expanded in 3 or more C-functions.
16 * The solution is to copy the DEF_C_INSTR to a temporary file. Then
17 * this file will be processed as many times as needed to generate all
20 * - The assembler-instructions must be processed in blocks not one at a
21 * time, so buffering is needed.
23 * - When generating object-code, the user can make use of the dist()
24 * function. This function should return the number of bytes between
25 * the current instruction and a labelled one, both forward and backward
26 * references must be handled.
27 * This requires the saving of the generated function, and
28 * backpatching in a second phase.
38 extern int first_action, last_action, token;
41 int no_conversions = FALSE, library, segment = UNKNOWN;
47 %token C_INSTR, DEF_C_INSTR, CONDITION, ARROW,
48 CALL, ASSEM_INSTR, DEFAULT, ERROR;
50 %start def_row, def_row;
51 %start c_table, c_table;
52 %lexical lex_analyzer ;
58 row : C_INSTR { set_outfile( yytext); header( yytext);
61 [ special | simple] { out( "}\n\n");}
63 | DEF_C_INSTR { init_defaults( yytext);}
64 [ Dspecial | Dsimple] { handle_defaults();}
68 { out( "if( 0 ) ;\n"); }
70 [ CONDITION { out( "else "); question( &yytext[0]);}
71 simple { out( "}\n");}
74 DEFAULT { out( "else {\n");}
75 simple { out( "}\n");}
78 simple : ARROW { save_output();}
79 actionlist { back_patch();}
83 actionlist : { first_action = TRUE;}
84 [ action { first_action = FALSE;}
90 action : CALL { print_call( &yytext[0]);}
92 | as_block { last_action = ( token == '.');
97 as_block : { init_as_block();}
98 ASSEM_INSTR { save_as( &yytext[0]);}
103 %if ( next_token() == ASSEM_INSTR)
105 ASSEM_INSTR { save_as( &yytext[0]);}
111 /* Grammar rules for a default-instruction, just copy to a temporary file. */
113 Dspecial: CONDITION { out( " %s ", yytext);}
116 [ CONDITION { out( " %s ", yytext);}
120 DEFAULT { out( " %s ", yytext);}
124 Dsimple : ARROW { out( "%s", yytext);}
131 [ ';' { out( ";\n");}
138 Daction : CALL { out( "%s", yytext);}
140 | ASSEM_INSTR { out( "\"%s\"", yytext);}
144 def_row : { set_segment(segment);}
145 [ special | simple] { out( "}\n\n");}
150 /* This is a grammar to handle the -c flag ( only one entry has changed).
151 * Skip all entries in the table until the one that has changed, then
152 * use the grammar rules for the normal case above.
158 c_row : %if ( to_change && strcmp( yytext, to_change) == 0)
159 C_INSTR { set_outfile( yytext); header( yytext);
160 set_segment(segment);
162 [ special | simple] { out( "}\n\n"); to_change = 0; }
165 [ c_special | c_simple]
167 | %if ( to_change && strcmp( yytext, to_change) == 0)
168 DEF_C_INSTR { init_defaults( yytext);}
169 [ Dspecial | Dsimple] { handle_defaults(); to_change = 0; }
172 [ c_special | c_simple]
175 c_special : CONDITION
192 [ c_action [ ';' c_action]* ]? '.'
205 int saved = 0, token;
209 LLmessage( inserted_token)
213 if ( inserted_token == 0) {
214 fprint( STDERR, "EM_table : syntax error in line %d, >>",
216 print_token( LLsymb);
217 fprint( STDERR, "<< will be deleted!!\n");
219 else if ( inserted_token < 0) {
220 fprint(STDERR,"EM_table : syntax error in line %d, garbage at end of table\n",
224 fprint( STDERR, "EM_table : syntax error in line %d, >>",
226 print_token( inserted_token);
227 fprint( STDERR, "<< will be inserted!!\n");
237 case C_INSTR : fprint( STDERR, "C_INSTR %s", yytext);
239 case ASSEM_INSTR : fprint( STDERR, "STRING %s", yytext);
241 case CALL : fprint( STDERR, "CALL %s", yytext);
243 case ARROW : fprint( STDERR, "==> ");
245 case CONDITION: fprint( STDERR, "CONDITION %s", yytext);
247 case DEFAULT : fprint( STDERR, "default ");
249 case ERROR : fprint( STDERR, "unmatched %s", yytext);
251 default : fprint( STDERR, " %c", token);
257 /* The lexical analyzer.
258 * It uses mylex() to get the tokens.
259 * It supports the buffering of one token.
260 * If the curent token is a C_INSTR it will set the global variable
261 * 'C_instr_info' to point to the correct information ( arguments, header,
276 if ( token == C_INSTR)
277 set_C_instr_info( yytext);
294 /******************************************************************************/
296 /* Handle arguments :
297 * -l : Library, generate one function per file.
298 * -c <name> : Change, only update the <name> file.
307 if ( strcmp( argv[1], "-l") == 0)
309 else if( strcmp( argv[1], "-c") == 0) {
314 fprint( STDERR, "No rule for %s\n", to_change);