-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.h
222 lines (190 loc) · 7.27 KB
/
header.h
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
/*
Headers file for mmn14
*/
/*__________________ Prevent header duplication __________________*/
#ifndef FILE_H
#define FILE_H
/*__________________ Libraries __________________*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*__________________ Definitions __________________*/
#define WORD_SIZE 14
#define ZERO '.'
#define ONE '/'
#define INSTANT '#'
#define COMMENT ';'
#define SEP ','
#define LABEL_END ':'
#define INST '.'
#define FIRST_ADDR 100
#define IN_EXT ".as"
#define OUT_EXT ".ob"
#define EXT_EXT ".ext"
#define ENT_EXT ".ent"
#define TRUE 1
#define FALSE 0
#define OK 0
#define LOGFILE stderr
enum encodings { A, E, R };
/*__________________ Macros __________________*/
/*Usage prompt: if assembler called without arguments.*/
#define PROMPT \
"Usage: assembler [list of .as files without ext, space " \
"delimitered.]\n" \
"Example: assembler x y z\n"
/*Throw error and exit main function*/
#define PANIC(message) \
{ \
fprintf(LOGFILE, "%s\n", message); \
exit(!OK); \
}
/*Use malloc and exit if allocation failed*/
#define MALLOC(dst, pntr_type, size) \
{ \
dst = (pntr_type *)malloc(size); \
if (dst == NULL) \
PANIC("Memory allocation problem, assembler crashed!") \
}
/*List of assembler operations*/
#define OP_NAMES \
QUOTE(mov) \
QUOTE(cmp) \
QUOTE(add) \
QUOTE(sub) \
QUOTE(not) \
QUOTE(clr) \
QUOTE(lea) \
QUOTE(inc) \
QUOTE(dec) \
QUOTE(jmp) \
QUOTE(bne) \
QUOTE(red) \
QUOTE(prn) \
QUOTE(jsr) \
QUOTE(rts) \
QUOTE(stop) \
Q(EOCL)
/*End of Command List*/
/*List of assembler instructions*/
#define INST_NAMES \
QUOTE(.string) \
QUOTE(.data) \
QUOTE(.entry) \
QUOTE(.extern) \
Q(EOIL)
/*End of Instructions List*/
/*List of assembler registeries*/
#define REG_NAMES \
QUOTE(r1) \
QUOTE(r2) \
QUOTE(r3) \
QUOTE(r4) \
QUOTE(r5) \
QUOTE(r6) \
Q(EORL)
/*End of Reg List*/
/*
Set of macros to create enums and string arrays with matching names,
so we can easily use "array[operation]" and get the operation as
a string.
*/
#define QUOTE(m) m##_,
/*without quotes, to be used with enum*/
#define Q(m) m
enum op_enum { OP_NAMES };
enum inst_enum { i_string, i_data, i_entry, i_extern, EOIL };
enum reg_enum { REG_NAMES };
enum key_words {Op, In, Re};
#undef QUOTE
#undef Q
#define QUOTE(m) #m, /*add quotes to be used as list of strings*/
#define Q(m) #m
#define QM(m) Q(m)
/*__________________ Limits __________________*/
#define OPS_NUMBER (EOCL + 1)
#define INST_NUMBER (EOIL + 1)
#define REGS_NUMBER (EORL + 1)
#define MAX_SRC_LINE 81
#define MAX_LABEL 32
#define MAX_OPERATION 81
#define MAX_INSTRUCTION 10
#define MAX_FILE_NAME 81
#define MAX_ERROR_MESSAGE 80
/*__________________ Data Structures __________________*/
typedef struct node {
void *data;
struct node *next;
} Node;
typedef struct List {
Node *head;
Node *tail;
} List;
/*__________________ Types __________________*/
/*Basic types*/
typedef char boolean;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef char *string;
/*Memory word*/
typedef ushort word;
/*Error info*/
typedef struct Error {
char file[MAX_FILE_NAME + 1];
int line;
char message[MAX_ERROR_MESSAGE];
} Error;
/*Symbol*/
typedef struct Symbol {
char name[MAX_LABEL];
char encoding;
int address;
boolean isdata;
} Symbol;
/*Control - structure to hold assembler run-time variables*/
typedef struct Control {
string ops_array[OPS_NUMBER];
string instnames_array[INST_NUMBER];
string regs_array[REGS_NUMBER];
char filename[MAX_FILE_NAME + 1];
FILE *fp;
uint line;
uint IC;
uint DC;
List errors_array;
List instruction_array;
List data_array;
List symbol_table;
} Control;
/*Macro to initialize control object*/
#define CONTROL_INIT \
{ \
{OP_NAMES}, {INST_NAMES}, { REG_NAMES } \
}
/*__________________ Functions __________________*/
/*Detailed documentation inside each file*/
/*list.c*/
void add_item(List *list, void *data, size_t datasize);
void free_list(List *list);
boolean is_empty_list(List *list);
void print_list(List *list);
void walk_list(List *list, void (*action)(void *));
/*file_handlers.c*/
void get_next_file(Control *ctrl, string file);
string get_line(Control *ctrl, string s);
/*error_handlers.c*/
void add_error(Control *ctrl, string m);
void print_error(void *e);
void dump_errors(Control *ctrl);
/*runtime.c*/
int assembler_first_go(Control *ctrl);
void cleanup(Control *ctrl);
/*assembler_utils.c*/
boolean find_label(Control *ctrl, string s);
int isReservedWord(Control *ctrl, string s, int EOL);
int trim(string s);
boolean edge_case_string(Control *ctrl, string token);
/*__________________ End of header file __________________*/
#endif