Repository navigation
Expand file tree
/
Copy pathasm.js
More file actions
256 lines (218 loc) · 8.15 KB
/
Copy pathasm.js
File metadata and controls
256 lines (218 loc) · 8.15 KB
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
// this is a main file, you know...
import {lst, html, lmap} from "./listing.js"
import { pass1 } from "./pass1.js";
import {pass2} from "./pass2.js";
import {objCode, linkModules} from "./objcode.js"
import * as Parser from "./parser.js";
import { buildLibrary } from "./libcode.js";
import { resolveLibrary } from "./semver-resolve.js";
import { parseLnk } from "./parseLnk.js";
//import all CPUs
import {I8080} from "./cpu/i8080.js";
import {M6800} from "./cpu/m6800.js";
import { C6502 } from "./cpu/c6502.js";
import { Z80 } from "./cpu/z80.js";
import { I8008 } from "./cpu/i8008.js";
import { CDP1802 } from "./cpu/cdp1802.js";
import { M6809 } from "./cpu/m6809.js";
import { H6309 } from "./cpu/h6309.js";
import { C65816 } from "./cpu/c65816.js";
import { Z180 } from "./cpu/z180.js";
import { M6803 } from "./cpu/m6803.js";
import { S2650 } from "./cpu/s2650.js";
import { SM83 } from "./cpu/sm83.js";
const cpus = [I8080, M6800, M6803, C6502, Z80, Z180, I8008, CDP1802, M6809, H6309, C65816, S2650, SM83];
/**
* Compiles assembly source code into machine code
* @param {string} source - Assembly source code
* @param {Object} fileSystem - File system interface with readFile method
* @param {Object} opts - Compilation options
* @param {string} opts.assembler - Assembler name or object
* @param {string} filename - Source filename for error reporting
* @returns {Object} Compilation result with dump, vars, xref, and opts
* @throws {Object} Error object with compilation details
*/
export const compile = async (source, fileSystem, opts = {assembler:null}, filename="noname") => {
if (typeof opts.assembler == "string") {
opts.assembler = cpus.find(x=>x.cpu.toUpperCase()==opts.assembler.toUpperCase());
}
if (!opts.assembler || typeof opts.assembler != "object") {
throw {msg:"No assembler specified", s:"Assembler error"};
}
opts = {...opts, readFile: fileSystem.readFile, childOpts: fileSystem.childOpts, resolvePath: fileSystem.resolvePath,
ENT:null,
BINFROM:null,
BINTO:null,
ENGINE:null,
PRAGMAS:[],
includedFiles:{},
endian:opts.assembler.endian || false,
xfre: {},
xref: {},
}
if (opts.relaxed) opts.errors = [];
let parsePhaseErrors = [];
try {
// parse source code into internal representation
let parsedSource = await Parser.parse(source, opts);
// preserve any preprocessor errors collected during parse
if (opts.relaxed) parsePhaseErrors = [...(opts.errors || [])];
// pass 1: prepare instruction codes and try to evaluate expressions
if (opts.relaxed) opts.errors = [];
let metacode = await pass1(parsedSource, null, opts)
// metacode is passed again and again until all expressions are evaluated
if (opts.relaxed) opts.errors = [];
metacode = await pass1(metacode[0], metacode[1], opts);
if (opts.relaxed) opts.errors = [];
metacode = await pass1(metacode[0], metacode[1], opts);
if (opts.relaxed) opts.errors = [];
metacode = await pass1(metacode[0], metacode[1], opts);
// DO NOT reset opts.errors here — pass1 errors must survive into pass2
metacode[1]["__PRAGMAS"] = opts.PRAGMAS;
// pass 2: assign addresses to labels and evaluate expressions
// (this pass is not repeated)
// It should be all resolved aftrer the 2nd pass
metacode = pass2(metacode, opts);
//new output, broke backward compatibility
let out = {
dump: metacode[0],
vars: metacode[1],
xref: opts.xref,
opts: opts,
}
// is it a module?
let vars = metacode[1];
if (vars && typeof vars.__PRAGMAS !== "undefined" && vars.__PRAGMAS.indexOf("MODULE") != -1) {
let obj = objCode(metacode[0],metacode[1],opts,filename)
out.obj = obj;
}
if (opts.relaxed) {
const allErrors = [...parsePhaseErrors, ...(opts.errors || [])];
if (allErrors.length > 0) {
throw { errors: allErrors };
}
}
return out
} catch (e) {
if (opts.relaxed) {
// Re-throw already-wrapped error arrays without double-wrapping
if (e.errors) throw e;
const fatalError = {
msg: e.msg || "Internal error",
s: e.s || "Fatal error",
wline: opts.WLINE
};
throw { errors: [...parsePhaseErrors, ...(opts.errors || []), fatalError] };
}
// Some error occured
//console.log(e)
let s = e.s || "Internal error";
//no message, so we use the general one
//FALLBACK - should be removed in future version
if (!e.msg) {
console.error(e);
throw {
error:
{
msg: `Cannot evaluate line ${opts.WLINE.numline}, there is some unspecified error (e.g. reserved world as label etc.)`,
wline: opts.WLINE
}
}
}
if (!e.s) e.s = s;
throw {
error: {
msg: e.msg,
s: e.s,
wline: opts.WLINE
}
};
}
}
/**
* Extracts filename from full path
* @param {string} fullpath - Full file path
* @returns {string} Filename without path
*/
const getfn = (fullpath) => {
let parts = fullpath.split("/");
return parts[parts.length-1];
}
/**
* Compiles assembly code from a file
* @param {string} filePath - Path to assembly source file
* @param {Object} fileSystem - File system interface with readFile method
* @param {Object} opts - Compilation options
* @returns {Object} Compilation result
*/
export const compileFromFile = async (filePath, fileSystem, opts = {assembler:null}) => {
let source = await fileSystem.readFile(filePath);
return compile(source, fileSystem, opts, getfn(filePath));
}
//----------------------------------------
// linker
/**
* Links multiple object modules into a single executable
* @param {Object} linkList - Link list configuration
* @param {Array} linkList.modules - Array of module names to link
* @param {Array} linkList.library - Array of library names to link
* @param {Object} fileSystem - File system interface with readFile method
* @param {string} name - Output name for the linked executable
* @returns {Object} Linked executable data
* @throws {Object} Error if modules have incompatible CPU or endian settings
*/
/**
* Resolve the filesystem path for a module entry from a .lnk recipe.
* New-style entries include the file extension (e.g. "main.obj80");
* legacy entries are bare names (e.g. "main") and get ".obj" appended.
* @param {string} entry - Module name from the recipe
* @returns {string} Filename to pass to fileSystem.readFile
*/
const moduleFilename = (entry) => entry.includes(".") ? entry : entry + ".obj";
const link = async (linkList, fileSystem, name="noname") => {
let cpu = null
let endian = null
const checkModule = (f, label) => {
if (!cpu) cpu = f.cpu;
if (cpu !== f.cpu) throw {msg:"Different CPU in " + label, s:"Linker error"};
if (endian === null) endian = f.endian;
if (endian !== f.endian) throw {msg:"Different endian in " + label, s:"Linker error"};
};
const modules = await Promise.all(linkList.modules.map(async m => {
const f = JSON.parse(await fileSystem.readFile(moduleFilename(m)));
checkModule(f, "module " + m);
return f;
}));
// Library entries: new-style ".lib80" bundles or legacy bare names (".obj" files).
const library = (await Promise.all(linkList.library.map(async m => {
const filename = moduleFilename(m);
const raw = JSON.parse(await fileSystem.readFile(filename));
// .lib80 bundle: { modules: [{name, obj}, ...], symbolIndex, ... }
if (Array.isArray(raw.modules)) {
return raw.modules.map(entry => {
checkModule(entry.obj, "library " + m + " / " + entry.name);
return entry.obj;
});
}
// Legacy single-obj file
checkModule(raw, "library file " + m);
return [raw];
}))).flat();
linkList.endian = endian;
return linkModules(linkList, modules, library);
}
/**
* Main assembler interface object
*/
export const asm = {
lst,
html,
lmap,
compile,
compileFromFile,
link,
cpus,
buildLibrary,
resolveLibrary,
parseLnk,
}