-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasm.y
176 lines (147 loc) · 4.03 KB
/
hasm.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
/*
* This file is part of the HASM Assembler
* Copyright (c) 2019 Madhav Kanbur
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include "hasm.h"
#include "symbol_table.h"
void yyerror(const char *msg);
int yylex();
extern FILE *yyout;
extern FILE *yyin;
extern char labelID[2048];
int isFirstPass;
%}
%token NUMBER ID REGISTER JUMP
%token A D M AD AMD AM MD
%token JGT JEQ JGE JLT JNE JLE JMP
%define parse.error verbose
%%
Start: InstructionList;
InstructionList: InstructionList Instruction |;
Instruction : A_Instruction { ++instructionCount; }
| C_Instruction { ++instructionCount; }
| Label;
Label: '(' ID ')' {
if(isFirstPass) {
// insert into symbol table
insertSymbol(labelID, instructionCount);
}
};
A_Instruction: '@' Number {
if(isFirstPass) break;
setRegisterA($2);
fprintf(yyout, "%s\n", opcode);
RESET_OPCODE();
} | '@' ID {
if(isFirstPass) break;
setRegisterA(getSymbolAddress(labelID));
fprintf(yyout, "%s\n", opcode);
RESET_OPCODE();
};
C_Instruction: Dest '=' Comp ';' Jump { if(isFirstPass) break; fprintf(yyout, "%s\n", opcode); RESET_OPCODE(); }
| Comp ';' Jump { if(isFirstPass) break; fprintf(yyout, "%s\n", opcode); RESET_OPCODE(); }
| Dest '=' Comp { if(isFirstPass) break; fprintf(yyout, "%s\n", opcode); RESET_OPCODE(); };
Dest: Register { if(isFirstPass) break; setDest($1); };
Jump: Branch { if(isFirstPass) break; setJump($1); };
Comp: Assign_Constant
| Assign_Register
| Assign_Register_Unary
| Register_Increment
| Register_Decrement
| Register_Addition
| Register_Subtraction
| Register_Bitwise_And
| Register_Bitwise_Or
Assign_Constant: Number {
if(isFirstPass) break;
assignConstant($1);
};
Assign_Register: Register {
if(isFirstPass) break;
assignRegister($1);
};
Assign_Register_Unary: '!' Register {
if(isFirstPass) break;
assignRegisterUnary($2, '!');
} | '-' Register {
if(isFirstPass) break;
assignRegisterUnary($2, '-');
};
Register_Increment: Register '+' Number {
if(isFirstPass) break;
if($3 != 1) {
// Error
printf("Error\n");
exit(0);
}
registerIncrement($1);
};
Register_Decrement: Register '-' Number {
if(isFirstPass) break;
if($3 != 1) {
// Error
printf("Error\n");
exit(0);
}
registerDecrement($1);
};
Register_Addition: Register '+' Register {
if(isFirstPass) break;
registerAddition($1, $3);
};
Register_Subtraction: Register '-' Register {
if(isFirstPass) break;
registerSubtraction($1, $3);
};
Register_Bitwise_And: Register '&' Register {
if(isFirstPass) break;
registerBitwiseAnd($1, $3);
};
Register_Bitwise_Or: Register '|' Register {
if(isFirstPass) break;
registerBitwiseOr($1, $3);
};
Register: REGISTER { $$ = yylval; }
Branch: JUMP { $$ = yylval; }
Number: NUMBER { $$ = yylval; }
| '-' NUMBER { $$ = yylval * -1; };
%%
void yyerror(const char *msg)
{
fprintf(stderr, "%s\n", msg);
return;
}
int main(int argc, char **argv)
{
if(argc != 2) {
printf("Usage : ./hasm.out <asm file>\n");
return 0;
}
yyin = fopen(argv[1], "r");
if(!yyin) {
printf("Error opening file \'%s\'\n", argv[1]);
return 0;
}
isFirstPass = 1;
yyparse();
fclose(yyin);
yyin = fopen(argv[1], "r");
isFirstPass = 0;
yyparse();
//print_table();
}