-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.js
308 lines (262 loc) · 10.1 KB
/
data.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* global Utils */
// Module for parsing a data segment
var DataParser = (function () {
// Constants
var base_address = Utils.Parser.const_to_val("0x10000000");
var max_address = Utils.Parser.const_to_val("0x10100000");
var base_stack = Utils.Parser.const_to_val("0x7fff0000");
var max_stack = Utils.Parser.const_to_val("0x80000000");
// Each line must have a label (optional), type, and 1 or more arguments of that type/labels
// This function will extract the label, type, and args
// Then, it will verify them.
var verify = function (raw) {
// Split the line up
for (var i = 0; i < raw.length; i++) {
// Split based on spaces
var current = raw[i].text.split(" ");
// Do we have a label? If so, consume it.
var label_or_null = Utils.Parser.is_label_dec(current[0]);
if (label_or_null) {
raw[i].label = label_or_null;
current = current.slice(1);
} else {
raw[i].label = null;
}
// Then, we MUST have a type.
var type_or_null = Utils.Parser.is_type(current[0]);
if (type_or_null) {
raw[i].type = type_or_null;
current = current.slice(1);
} else {
// FAIL
throw Utils.get_error(4, [raw[i].line]);
}
// Then, we MUST have 1 or more arguments
if (current.length < 1) {
// FAIL
throw Utils.get_error(6, [raw[i].type, raw[i].line]);
} else {
raw[i].args = current;
}
// Unescape any strings in the original text
raw[i].text = Utils.Parser.unescape_string(raw[i].text);
}
// Validate arguments
for (var i = 0; i < raw.length; i++) {
current = raw[i].args;
var type = raw[i].type;
// Certain types can only have one argument
if (type === "space" || type === "align") {
if (current.length > 1) {
throw Utils.get_error(5, [current[1], type, raw[i].line]);
}
}
for (var j = 0; j < current.length; j++) {
// Hand off to validation helper
var validation_result = Utils.Type[type](current[j]);
if (validation_result !== null) {
current[j] = validation_result;
} else {
throw Utils.get_error(5, [current[j], type, raw[i].line]);
}
}
}
// Return the result
return raw;
};
// Find label declarations and replace appropriately
// NOTE: Data must have been validated
var gather_labels = function (raw) {
var labels = {};
var current_address = base_address;
var line_to_size = function (prev, line) {
if (line.type === "word") {
var pre_padding = 4 * line.args.length;
var mod = prev % 4;
if (mod === 0) {
return pre_padding;
} else {
return pre_padding + (4 - mod);
}
}
if (line.type === "half") {
return 2 * line.args.length;
mod = prev % 2;
if (mod === 0) {
return pre_padding;
} else {
return pre_padding + (2 - mod);
}
}
if (line.type === "byte") {
return line.args.length;
}
if (line.type === "ascii" || line.type === "asciiz") {
var length = 0;
for (var i = 0; i < line.args.length; i++) {
length += line.args[i].length;
}
return length;
}
if (line.type === "space") {
return line.args[0];
}
if (line.type === "align") {
var modulus = Math.pow(2, line.args[0]);
mod = prev % modulus;
if (mod == 0) {
return 0;
} else {
return modulus - mod;
}
}
return null;
};
// Assign a base address to each line
for (var i = 0; i < raw.length; i++) {
raw[i].base = current_address;
var space = line_to_size(current_address, raw[i]);
raw[i].space = space;
current_address += space;
// Are we too big?
if (current_address > max_address) {
// FAIL
throw Utils.get_error(7, ["data",raw[i].line]);
}
}
// Gather all the labels
for (var i = 0; i < raw.length; i++) {
var label = raw[i].label;
if (label) {
// Make sure this label declaration is unique
if (Utils.get(labels, label)) {
// FAIL
throw Utils.get_error(9, [label, raw[i].line]);
}
// Make sure the label points to the content, not padding
var label_addr = raw[i].base;
if (raw[i].type === "word") {
label_addr += raw[i].space % 4;
}
if (raw[i].type === "half") {
label_addr += raw[i].space % 2;
}
labels[label] = label_addr;
}
}
// Replace all instances of labels in words with the address
for (var i = 0; i < raw.length; i++) {
var current = raw[i].args;
if (raw[i].type === "word") {
for (var j = 0; j < current.length; j++) {
if (Utils.Parser.is_label(current[j])) {
var match = Utils.get(labels, current[j]);
if (match) {
current[j] = match;
} else {
// FAIL
throw Utils.get_error(8, [current[j], raw[i].line]);
}
}
}
}
}
// Return the updated data segment and labels
return { data: raw, labels: labels };
};
// Convert preliminary data to the final data
var to_final = function (raw) {
var final = {};
var address = base_address;
for (var i = 0; i < raw.length; i++) {
var type = raw[i].type;
var args = raw[i].args;
var space = raw[i].space;
// Fill any padding
if (type === "word" || type === "half") {
var modulus = 4;
if (type === "half") {
modulus = 2;
}
var padding = space % modulus;
for (var j = 0; j < padding; j++) {
final[Utils.Math.to_hex(address)] = 0;
address++;
}
}
// Fill with values
if (type === "space" || type === "align") {
// We need space 0's
for (var j = 0; j < space; j++) {
final[Utils.Math.to_hex(address)] = 0;
address++;
}
}
if (type === "word") {
// Little endianize each word
for (var j = 0; j < args.length; j++) {
var bytes = Utils.Math.split_to_bytes(args[j], 4);
for (var k = 0; k < bytes.length; k++) {
final[Utils.Math.to_hex(address)] = bytes[k];
address++;
}
}
}
if (type === "half") {
// Little endianize each half
for (var j = 0; j < args.length; j++) {
bytes = Utils.Math.split_to_bytes(args[j], 2);
for (var k = 0; k < bytes.length; k++) {
final[Utils.Math.to_hex(address)] = bytes[k];
address++;
}
}
}
if (type === "byte") {
for (var j = 0; j < args.length; j++) {
final[Utils.Math.to_hex(address)] = new Number(args[j]);
address++;
}
}
if (type === "ascii" || type === "asciiz") {
for (var j = 0; j < args.length; j++) {
for (var k = 0; k < args[j].length; k++) {
final[Utils.Math.to_hex(address)] = args[j].charCodeAt(k);
address++;
}
}
}
}
return final;
};
// Chains together the above to complete a data segment parse
var parse = function (raw) {
var post_validation = verify(raw);
var post_label = gather_labels(post_validation);
// Function for resetting the segment to tis original state (simply redo finalization)
var final_segment = function () {
var result = to_final(post_label.data);
return result;
};
return {segment: final_segment, labels: post_label.labels};
};
// Creates a byte-addressed stack segment, initalized ot zero.
var create_stack = function () {
var stack = {};
var current = base_stack;
while (current <= max_stack) {
stack[Utils.Math.to_hex(current)] = 0;
current += 1;
}
return stack;
};
// Return out the interface
return {
parse: parse,
base_address: base_address,
max_address: max_address,
base_stack: base_stack,
max_stack: max_stack,
create_stack: create_stack
};
})();