-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
261 lines (240 loc) · 5.75 KB
/
main.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// main.c
// a component of basm
// latest version available at:
// https://github.com/JustinLardinois/ls-for-Windows
//
// Copyright 2014 Justin Lardinois
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EMPTY 0xFF
#define LINE_BUFFER_SIZE 1024
#define OUTPUT_NAME_MAX_LENGTH 1024
typedef unsigned char byte;
char* program_name;
char* input;
char output[OUTPUT_NAME_MAX_LENGTH];
// symbolic names of all BASIL opcodes, save for push which is a special case
char* ops[] = {
NULL ,
"and" ,
"not" ,
"or" ,
"mul" ,
"div" ,
"add" ,
"cmp" ,
"pop" ,
"swp" ,
"dup" ,
"ppc" ,
"get" ,
"put" ,
"br" ,
NULL
};
void usage()
{
fprintf(stderr,"Usage: %s SOURCE [-o OBJECT]\n",program_name);
exit(EXIT_FAILURE);
}
bool streq(char* a , char* b)
{
return strcmp(a,b) == 0;
}
// figures out names of input and output files
void parse_args(int argc , char** argv)
{
if(argc == 2)
{
input = argv[1];
snprintf(output,OUTPUT_NAME_MAX_LENGTH-2,"%s",input);
int length = strlen(output);
if(output[length-4] == '.' && output[length-3] == 'a' // .asm extension
&& output[length-2] == 's' && output[length-1] == 'm')
{
output[length-3] = 'o';
output[length-2] = '\0';
}
else strcat(output,".o");
}
else if (argc == 4)
{
if(streq(argv[1],"-o"))
{
snprintf(output,OUTPUT_NAME_MAX_LENGTH,"%s",argv[2]);
input = argv[3];
}
else if(streq(argv[2],"-o"))
{
input = argv[1];
snprintf(output,OUTPUT_NAME_MAX_LENGTH,"%s",argv[3]);
}
else usage();
}
else usage();
}
// removes leading and trailing whitespace from a string
char* trim(char* s)
{
if(s[0] == '\0') return s;
else if(isspace(s[0])) return trim(s+1);
else
{
int length = strlen(s);
for(int i = length -1; i >= 0; i--)
{
if(isspace(s[i])) s[i] = '\0';
else return s;
}
}
return s;
}
// plugs comments (which start with a semicolon) with a null byte
char* trim_comment(char* s)
{
char* semicolon = strchr(s,';');
if(semicolon != NULL) *semicolon = '\0';
return s;
}
// converts all characters in a string to lowercase
char* make_lower(char* s)
{
for(int i = 0; s[i] != '\0'; i++)
{
s[i] = tolower(s[i]);
}
return s;
}
// checks if s begins with the contents of substr
bool starts_with(char* s , char* substr)
{
for(int i = 0;;i++)
{
if(substr[i] == '\0') return true;
else if(s[i] != substr[i]) return false;
}
}
// reads an instruction line from the input file
// lines consisting solely of whitespace and/or comments are skipped
char* get_line(FILE* in , char* buffer)
{
if(fgets(buffer,LINE_BUFFER_SIZE,in) == NULL) return NULL;
char* line = make_lower(trim(trim_comment(buffer)));
if(streq(line,"")) return get_line(in,buffer);
else return line;
}
// returns the opcode for the symbolic instruction name passed in line
// if the instruction was push, immu is set to the operand
byte op_lookup(char* line , byte* immu)
{
if(starts_with(line,"push"))
{
char* operand = strchr(line,'#');
if(operand != NULL)
{
operand++;
int value;
if(sscanf(operand,"%d",&value) == 1)
{
if(value >= 0 && value <= 15)
{
*immu = value;
return 0;
}
}
}
}
else
{
for(int i = 1; ops[i] != NULL; i++)
{
if(streq(line,ops[i])) return i;
}
}
fprintf(stderr,"%s: invalid expression \"%s\"\n",input,line);
exit(EXIT_FAILURE);
}
// collects nibbles and outputs them to the output file
void assemble(FILE* in , FILE* out)
{
char buffer[LINE_BUFFER_SIZE];
char* line;
byte upper_nibble = EMPTY;
byte lower_nibble = EMPTY;
while((line = get_line(in,buffer)) != NULL)
{
byte immu = 0;
byte nibble = op_lookup(line,&immu);
if(upper_nibble == EMPTY)
{
upper_nibble = nibble << 4;
if(nibble == 0)
{
lower_nibble = immu;
nibble = upper_nibble | lower_nibble;
fwrite(&nibble,1,1,out);
upper_nibble = EMPTY;
lower_nibble = EMPTY;
}
}
else
{
lower_nibble = nibble;
nibble = upper_nibble | lower_nibble;
fwrite(&nibble,1,1,out);
if(lower_nibble == 0) upper_nibble = immu << 4;
else upper_nibble = EMPTY;
lower_nibble = EMPTY;
}
}
if(upper_nibble != EMPTY)
{
fwrite(&upper_nibble,1,1,out);
}
}
void die(char* file)
{
fprintf(stderr,"%s: %s\n",file,strerror(errno));
exit(EXIT_FAILURE);
}
int main(int argc , char** argv)
{
setbuf(stdout,0);
program_name = argv[0];
parse_args(argc,argv);
FILE* in = fopen(input,"r");
if(in == NULL) die(input);
FILE* out = fopen(output,"wb");
// I had what was quite possibly the worst bug ever in this line.
// Apparently, "w" is not sufficient on non-Unix C89 implementations
// if you're writing binary data. You may wonder if people still use
// such archaic systems. I do; it's called Windows 8.1.
//
// My problem was that if the hex byte 0A was fwritten (which is PUSH #10
// in BASIL assembly) the byte 0D was written after it. After a lot of pain
// and tears, I discovered that since 0A is a line feed (\n), a carriage
// return (\r), which 0D represents, was being written after it. I solved
// the bug by using "wb" instead of "w", which writes literal bytes.
if(out == NULL) die(output);
assemble(in,out);
fclose(in);
fclose(out);
exit(EXIT_SUCCESS);
}