-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler-parser.y
184 lines (146 loc) · 4.49 KB
/
assembler-parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
%error-verbose
%locations
%{
#include <stdio.h>
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include "bit-stream.h"
#include "symbol-table.h"
#include "code-generator.h"
#define BINARY_MODE
#define ASSERT( x ) \
if( ! ( x ) ) { \
printf( "ASSERT FAILED (" #x "), %s:%d\n", \
__FILE__, \
__LINE__ ); \
\
raise( SIGABRT ); \
} else {}
using namespace std;
int yylex();
extern int yyparse();
extern void yyerror( const char* s );
extern FILE* yyin;
extern int yylineno;
void codegen( const Instruction& ins );
%}
%union {
long long ival;
double dval;
char* sval;
int token;
}
%token TADD TAND TASSIGN TCALL TCMP TCMPGT TCMPGTE TCMPLT TCMPLTE TCMPNE TDOUBLE
%token TFUNC TIDENT TINT TJZ TNIL TOR TPOP TPUSH TRET TSTR TENDL
%token <sval> TREF TSTRVAL TIDENTVAL
%token <ival> TINTVAL
%token <dval> TDOUBLEVAL
%type <token> TFUNC TIDENT TINT TSTR TNIL
%start bsil
%%
bsil : definitions
;
definitions : definition
| definitions definition
;
definition : string_definition
| function_definition
| reference_point
| instructions
;
string_definition : TREF TSTR TINTVAL TSTRVAL TENDL { codegen( STRINGDEF( $1, $3, $4 ) ); }
;
function_definition : TREF TFUNC TINTVAL TENDL { codegen( FUNCDEF( $1, $3 ) ); }
;
reference_point : TREF TENDL { codegen( REFERENCEDEF( $1 ) ); }
instructions : TENDL
| instruction TENDL
| instructions instruction TENDL
;
instruction : TADD { codegen( ADD() ); }
| TAND { codegen( AND() ); }
| TASSIGN { codegen( ASSIGN() ); }
| TCALL TIDENTVAL TINTVAL { codegen( CALL( $2, $3 ) ); }
| TCMP { codegen( CMP() ); }
| TCMPGT { codegen( CMPGT() ); }
| TCMPGTE { codegen( CMPGTE() ); }
| TCMPLT { codegen( CMPLT() ); }
| TCMPLTE { codegen( CMPLTE() ); }
| TCMPNE { codegen( CMPNE() ); }
| TJZ TREF { codegen( JZ( $2 ) ); }
| TOR { codegen( OR() ); }
| TPUSH TNIL { codegen( PUSH( $2, 0.0 ) ); }
| TPUSH TSTR TREF { codegen( PUSH( $2, $3 ) ); }
| TPUSH TIDENT TIDENTVAL { codegen( PUSH( $2, $3 ) ); }
| TPUSH TINT TINTVAL { codegen( PUSH( $2, $3 ) ); }
| TPUSH TFUNC TREF { codegen( PUSH( $2, $3 ) ); }
| TPOP TIDENTVAL { codegen( POP( $2 ) ); }
| TRET { codegen( RET() ); }
;
%%
class BinaryHeader {
public:
BinaryHeader()
: num_symbols( 0 ), symtab_loc( 0 ) {}
void
update( const SymbolTable& sym_table )
{
num_symbols = sym_table.num_symbols();
symtab_loc = sym_table.offset();
}
private:
unsigned short num_symbols;
unsigned int symtab_loc;
friend ostream &
operator<<( ostream& oss, const BinaryHeader& header )
{
// We should be at the start of the stream when we write the header
ASSERT( oss.tellp() == 0 );
oss << U16( 0x100 ); // Version
oss << U16( header.num_symbols ); // Number of symbols needing linked
oss << U32( header.symtab_loc ); // Location of symbol table
}
};
SymbolTable sym_table;
stringstream file;
void
codegen( const Instruction& ins )
{
ins.text( file );
}
void
usage( const char* cmd )
{
printf( "Usage: %s <filename>\n", cmd );
exit( -1 );
}
main( int argc, char* argv[] )
{
if( argc != 2 ) {
usage( argv[ 0 ] );
}
FILE* f = fopen( argv[ 1 ], "r" );
if( !f ) {
perror( "fopen" );
exit( -1 );
}
// Reserve space for the header
BinaryHeader header;
file << header;
yyin = f;
do {
yyparse();
} while( !feof( yyin ) );
sym_table.text( file );
sym_table.link( file );
header.update( sym_table );
// Write the updated header
file.seekp( 0 );
file << header;
cout << file.str();
}
void yyerror( const char* s ) {
printf( "autobot.bs:%d: %s\n", yylineno, s );
exit( -1 );
}