This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmacro10.js
2093 lines (1931 loc) · 70.2 KB
/
macro10.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview A MACRO-10 "work-alike" Mini-Assembler for the PDP-10
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs 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, either version 3
* of the License, or (at your option) any later version.
*
* PCjs 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 PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Web = require("../../shared/lib/weblib");
var Debugger = require("../../shared/lib/debugger");
var PDP10 = require("./defines");
var DebuggerPDP10 = require("./debugger");
}
/**
* Elements of tblMacros.
*
* @typedef {{
* name:(string),
* nOperand:(number),
* aParms:(Array.<string>),
* aDefaults:(Array.<string>),
* aValues:(Array.<string>),
* sText:(string),
* nLine:(number)
* }}
*/
var Mac;
/**
* Elements of tblSymbols.
*
* @typedef {{
* name:(string),
* value:(number),
* nType:(number),
* nLine:(number)
* }}
*/
var Sym;
/**
* Elements of aLiterals.
*
* @typedef {{
* name:(string),
* aWords:(Array.<number>),
* aFixups:(Array.<string>)
* }}
*/
var Lit;
/**
* Elements of stackScopes.
*
* @typedef {{
* name:(string|undefined),
* aWords:(Array.<number>),
* aFixups:(Array.<string>),
* nLocation:(number),
* nLocationScope:(number),
* nLine:(number)
* }}
*/
var Scope;
/**
* @class Macro10
* @property {string} sURL
* @property {number|null} addrLoad
* @property {string} sOptions
* @property {DebuggerPDP10} dbg
* @property {function(...)|undefined} done
* @property {number} iURL
* @property {Array.<string>} aURLs
* @property {Array.<number>} anLines
* @property {Array.<string>} asLines
* @property {number|null|undefined} addrStart
* @unrestricted
*/
class Macro10 {
/**
* Macro10(dbg)
*
* A "mini" version of DEC's MACRO-10 assembler, with just enough features to support the handful
* of DEC diagnostic source code files that we choose to throw at it.
*
* We rely on the calling component (dbg) to provide a variety of helper services (eg, println(),
* parseExpression(), etc). This is NOT a subclass of Component, so Component services are not part
* of this class.
*
* @this {Macro10}
* @param {DebuggerPDP10} dbg (used to provide helper services to the Macro10 class)
*/
constructor(dbg)
{
this.dbg = dbg;
}
/**
* init(addrLoad, sOptions, done)
*
* Initializes all instance properties. Called by assembleFiles() and assembleString().
*
* Supported options include:
*
* 'd': leave all symbols in the debugger's variable table
* 'p': print the preprocessed resource(s) without assembling them
* 'l': print lines as they are parsed
* 's': treat the URL as a string and assemble it (assembleFiles() only)
*
* The done() callback is called after the resource(s) have been loaded (if necessary), parsed, and
* assembled. The caller must use other methods to obtain further results (eg, getImage(), getStart()).
*
* The callback includes a non-zero error code if there was an error (and the URL):
*
* done(nErrorCode, sURL)
*
* @this {Macro10}
* @param {number|null} [addrLoad] (the absolute address to assemble the code at, if any)
* @param {string|undefined} [sOptions] (zero or more letter codes to control the assembly process)
* @param {function(...)|undefined} [done]
*/
init(addrLoad, sOptions, done)
{
this.addrLoad = addrLoad;
this.sOptions = (sOptions || "").toLowerCase();
this.done = done;
this.iURL = 0;
this.aURLs = [];
/**
* Number of lines per file.
*
* @type {Array.<number>}
*/
this.anLines = [];
/**
* Lines from all the resources.
*
* @type {Array.<string>}
*/
this.asLines = [];
if (MAXDEBUG) this.println("starting PCjs MACRO-10 Mini-Assembler...");
/*
* Initialize all the tables and other data structures that MACRO-10 uses.
*
* The Macros (tblMacros) and Symbols (tblSymbols) tables are fairly straightforward: they are
* indexed by a macro or symbol name, and each element is a Mac or Sym object, respectively.
*
* We also treat REPEAT blocks and CONDITIONAL (eg, IFE) blocks like macros, except that they are
* anonymous, parameter-less, and immediately invoked. REPEAT blocks are always immediately invoked
* (repeatedly, based on the repeat count), whereas CONDITIONAL blocks are either immediately
* invoked if the associated condition is true or skipped if the condition is false.
*
* Finally, we have LITERAL blocks, which are semi-anonymous (we give each one an auto-generated
* name based on the current location) and are automatically but not immediately invoked. Instead,
* after we've finished processing all the lines in the original input file, we run through all
* the LITERAL blocks in tblMacros and process the associated statement(s) at that time.
*
* Macros have the name that was assigned to them, REPEAT and conditional blocks have generated names
* that match the pseudo-op (eg, "?REPEAT", "?IFE"), and LITERAL blocks have generated location-based
* names. All generated names use a leading question mark ('?') so that they don't conflict with
* normal MACRO-10 symbols.
*/
/**
* @type {Object.<Mac>}
*/
this.tblMacros = {};
/**
* @type {Object.<Sym>}
*/
this.tblSymbols = {};
/**
* @type {Array.<Lit>}
*/
this.aLiterals = []; // array of literals
/**
* This keeps track of symbols suffixed with '#', which are later assembled into a variable pool,
* following the literal pool.
*
* @type {Array.<string>}
*/
this.aVariables = [];
/**
* @type {Array.<number>}
*/
this.aWords = []; // filled in by the various genXXX() functions
/**
* This sparse array is indexed by location, and each used entry contains any undefined symbols that
* must be evaluated to fully resolve the word at the corresponding location. NOTE: There's no requirement
* that the array be sparse; we could certainly fill each unused entry with null (ie, for locations that
* don't require a fixup).
*
* @type {Array.<string>}
*/
this.aFixups = [];
/**
* This array parallels aFixups, providing context (ie, line numbers) for any fixups that we want to analyze.
*
* @type {Array.<number>}
*/
this.aLineRefs = [];
this.nLine = 0;
this.nError = 0;
this.nLiteral = 0; // used to uniquely number literals
/**
* @type {number}
*/
this.nLocation = this.addrLoad || 0;
/**
* @type {number}
*/
this.nLocationScope = -1;
/**
* @type {Array.<Scope>}
*/
this.stackScopes = [];
this.sOperator = null; // the active operator, if any
this.nMacroDef = 0; // the active macro definition state
this.sMacroDef = null; // the active macro definition name
this.chMacroOpen = this.chMacroClose = '';
/*
* This regular expression breaks each MACRO-10 line into the following elements:
*
* [1]: label (with trailing colon), if any
* [2]: operator (eg, opcode mnemonic or pseudo-op), if any
* [3]: operator/operand whitespace separator, if any
* [4]: operand(s), if any
* [5]: comment, if any
*/
this.reLine = /^[ \t]*([A-Z$%.?][0-9A-Z$%.]*:|)[ \t]*([A-Z$%.][0-9A-Z$%.]*|)([ \t]*)([^;]+|)(;?[\s\S]*)/i;
this.macroCall = null; // the active macro being called, if any
/**
* If an ASCII/ASCIZ/SIXBIT pseudo-op is active, chASCII is set to the separator
* and sASCII collects the intervening character(s).
*
* @type {null|string}
*/
this.chASCII = null;
/**
* @type {string}
*/
this.sASCII = "";
this.addrStart = null;
}
/**
* assembleFiles(sURL, addrLoad, sOptions, done)
*
* Requests the resource(s) specified by sURL; multiple resources can be requested by separating
* them with semicolons. The resources are requested and combined in the same order they are listed,
* and after the last resource has been received, they are assembled as a single unit.
*
* As a courtesy to the Debugger's doAssemble() function, we allow it to select between assembleFiles()
* and assembleString() by specifying an 's' option here, rather than having two separate code paths.
*
* @this {Macro10}
* @param {string} sURL (the URL(s) of the resource to be assembled)
* @param {number|null} [addrLoad] (the absolute address to assemble the code at, if any)
* @param {string|undefined} [sOptions] (zero or more letter codes to control the assembly process)
* @param {function(...)|undefined} [done]
*/
assembleFiles(sURL, addrLoad, sOptions, done)
{
if (sOptions && sOptions.indexOf('s') >= 0) {
this.assembleString(sURL, addrLoad, sOptions, done);
return;
}
this.init(addrLoad, sOptions, done);
this.aURLs = sURL.split(';');
this.loadNextResource();
}
/**
* assembleString(sText, addrLoad, sOptions, done)
*
* Assembles the given text.
*
* @this {Macro10}
* @param {string} sText
* @param {number|null} [addrLoad] (the absolute address to assemble the code at, if any)
* @param {string|undefined} [sOptions] (zero or more letter codes to control the assembly process)
* @param {function(...)|undefined} [done]
* @return {number}
*/
assembleString(sText, addrLoad, sOptions, done)
{
this.init(addrLoad, sOptions, done);
this.asLines = sText.split(/(\r?\n)/);
this.anLines.push(this.asLines.length);
this.parseResources();
if (this.done) this.done(this.nError);
return this.nError;
}
/**
* loadNextResource()
*
* @this {Macro10}
*/
loadNextResource()
{
if (this.iURL == this.aURLs.length) {
this.parseResources();
if (this.done) this.done(this.nError);
return;
}
var macro10 = this;
var sURL = this.aURLs[this.iURL];
this.println("loading " + Str.getBaseName(sURL));
/*
* We know that local resources ending with ".MAC" are actually stored with a ".txt" extension.
*/
if (sURL.indexOf(':') < 0) {
var sExt = sURL.slice(-4).toUpperCase();
if (".MAC.KLM".indexOf(sExt) >= 0) sURL += ".txt";
}
Web.getResource(sURL, null, true, function processMacro10(sFile, sResource, nErrorCode) {
if (nErrorCode) {
if (macro10.done) macro10.done(nErrorCode, sFile);
return;
}
var sText = sResource;
if (Str.endsWith(sFile, ".html")) {
/*
* We want to parse ONLY the text between <PRE>...</PRE> tags, and eliminate any HTML entities.
*/
sText = "";
var match, re = /<pre>([\s\S]*?)<\/pre>/gi;
while ((match = re.exec(sResource))) {
var s = match[1];
if (s.indexOf('&') >= 0) s = s.replace(/</gi, '<').replace(/>/gi, '>').replace(/&/gi, '&');
sText += s;
}
match = sText.match(/&[a-z]+;/i);
if (match) macro10.warning("unrecognized HTML entity '" + match[0] + "'");
}
/*
* For a file containing N lines, split() will return an array of N*2+1 entries, with every even entry
* containing the characters, if any, preceding a line-ending, and every odd entry (including the final
* entry) containing a line-ending.
*
* If, for some reason, split() doesn't do that, then we try to warn and patch things up as best we can.
*/
var asLines = sText.split(/(\r?\n)/);
if (asLines.length & 1) {
s = asLines.pop();
if (s) {
macro10.warning("unexpected line '" + s + "'");
asLines.push(s);
asLines.push("");
}
} else {
macro10.warning("unexpected number of lines (" + asLines.length + ")");
}
macro10.asLines = macro10.asLines.concat(asLines);
macro10.anLines[macro10.iURL] = (asLines.length >> 1);
macro10.iURL++;
setTimeout(function() {
macro10.loadNextResource();
}, 0);
});
}
/**
* getImage()
*
* Service for the Debugger to obtain the assembled data after a (hopefully) successful assembly process.
*
* @this {Macro10}
* @return {Array.<number>}
*/
getImage()
{
return this.aWords;
}
/**
* getStart()
*
* Service for the Debugger to obtain the starting address after a (hopefully) successful assembly process.
*
* @this {Macro10}
* @return {number|null|undefined}
*/
getStart()
{
return this.addrStart;
}
/**
* parseResources()
*
* Begin the assembly process.
*
* @this {Macro10}
* @return {number}
*/
parseResources()
{
var macro10 = this;
/*
* If the "preprocess" option is set, then print everything without assembling.
*/
if (this.sOptions.indexOf('p') >= 0) {
this.println(this.asLines.join(""));
return 0;
}
var a = this.dbg.resetVariables();
/*
* Add predefined device codes
*/
this.addSymbol("APR", 0); // Priority Interrupt
this.addSymbol("PI", 4); // Arithmetic Processor
try {
for (let i = 0; i < this.asLines.length; i += 2) {
this.nLine++;
/*
* If the "line" option is set, then print all the lines as they are parsed.
*/
if (this.sOptions.indexOf('l') >= 0) {
this.println(this.getLineRef() + ": " + this.asLines[i]);
}
/*
* Since, at this early stage, I'm not sure whether all the resources I'm interested in
* assembling have had their original CR/LF line endings preserved (eg, some files may have
* been converted to LF-only line endings), I'm going to skip over whatever line endings
* are in the array (stored in every OTHER entry) and insert my own uniform CR/LF sequences.
*/
if (!this.parseLine(this.asLines[i] + '\r\n')) break;
/*
* When an END statement is encountered, addrStart will change from null to either undefined
* or a starting address.
*/
if (this.addrStart !== null) break;
}
if (this.nMacroDef) {
this.error("open block", this.tblMacros[this.sMacroDef].nLine);
}
if (this.stackScopes.length) {
this.error("open scope", this.stackScopes[0].nLine);
}
/*
* Process all literals next.
*/
this.doLiterals();
/*
* Process all variables next.
*/
this.doVariables();
/*
* And last but not least, perform all fixups.
*/
this.aFixups.forEach(function processFixup(sValue, nLocation) {
let value = macro10.parseExpression(sValue, undefined, nLocation, macro10.aLineRefs[nLocation]);
if (value === undefined) return;
value += macro10.aWords[nLocation];
macro10.aWords[nLocation] = macro10.truncate(value, nLocation);
});
} catch(err) {
this.println(err.message);
this.nError = -1;
}
if (this.sOptions.indexOf('d') < 0) this.dbg.restoreVariables(a);
return this.nError;
}
/**
* parseLine(sLine, aParms, aValues, aDefaults)
*
* @this {Macro10}
* @param {string} sLine (line contents)
* @param {Array.<string>} [aParms]
* @param {Array.<string>} [aValues]
* @param {Array.<string>} [aDefaults]
* @return {boolean}
*/
parseLine(sLine, aParms, aValues, aDefaults)
{
var i, matchLine;
if (this.chASCII != null) {
sLine = this.defASCII(sLine);
}
var fParse = true;
var sLabel, sOperator = "", sSeparator, sOperands, sRemainder;
while (fParse) {
matchLine = sLine.match(this.reLine);
if (!matchLine || matchLine[5] && matchLine[5].slice(0, 1) != ';') {
this.error("failed to parse line '" + sLine + "'");
return false;
}
fParse = false;
sOperator = matchLine[2].toUpperCase();
/*
* TODO: The following kludge needs to be fixed at some point. The goal here is to prevent any
* of the caller's parameters from replacing any IRP/IRPC call parameters, but the goal is actually
* much bigger than that, because it's not just IRP/IRPC call parameters that must be left intact,
* but ANY macro call parameters; IRP/IRPC macros are just easier to pick out. Unfortunately, as
* the code is currently structured, we won't know if we're dealing with any macro call parameters
* on this line until parseMacro() is called, below.
*/
if (sOperator == Macro10.PSEUDO_OP.IRP || sOperator == Macro10.PSEUDO_OP.IRPC) {
aParms = null;
}
if (aParms) {
for (var iParm = 0; iParm < aParms.length; iParm++) {
var sParm = aParms[iParm];
var macroDef = this.tblMacros[this.sMacroDef];
if (macroDef && macroDef.aParms.indexOf(sParm) >= 0) continue;
var sReplace = aValues[iParm] || aDefaults[iParm] || "";
var iSearch = 0;
var iLimit = sLine.length - matchLine[5].length; // set the limit at the start of the comment, if any
while (iSearch < iLimit) {
var iMatch = sLine.indexOf(sParm, iSearch);
if (iMatch < 0) break;
iSearch = iMatch + 1;
var iMatchEnd = iMatch + sParm.length;
var chPre = '', chPost = '';
if ((!iMatch || !this.isSymbolChar(chPre = sLine[iMatch - 1])) && (iMatchEnd >= sLine.length || !this.isSymbolChar(chPost = sLine[iMatchEnd]))) {
/*
* If the "concatenation character" (') appears before (or after) the symbol being replaced, remove it.
*/
if (chPre == "'") iMatch--;
if (chPost == "'") iMatchEnd++;
sLine = sLine.substr(0, iMatch) + sReplace + sLine.substr(iMatchEnd);
iSearch = iMatch + sReplace.length;
fParse = true;
}
}
}
aParms = null;
}
if (this.nMacroDef) {
if (this.nMacroDef == 1) {
i = sLine.indexOf(this.chMacroOpen);
if (i >= 0) {
this.nMacroDef++;
sLine = sLine.substr(i+1);
} else {
this.error("expected " + this.sOperator + " definition in '" + sLine + "'");
}
}
if (this.nMacroDef > 1) {
sLine = this.appendMacro(sLine);
fParse = true;
}
if (this.nMacroDef) return true;
}
}
sLabel = matchLine[1];
sSeparator = matchLine[3];
sOperands = matchLine[4].trim();
sRemainder = matchLine[4] + matchLine[5];
if (sLabel) {
sLabel = sLabel.slice(0, -1);
this.addSymbol(sLabel, this.nLocation, Macro10.SYMTYPE.LABEL);
}
var matchOp;
if (sOperator && (matchOp = sOperands.match(/^([=:]+)(.*)/))) {
var nType = 0;
sLabel = sOperator;
sOperator = matchOp[1];
sOperands = matchOp[2];
if (sOperator == '==') {
nType |= Macro10.SYMTYPE.PRIVATE;
}
else if (sOperator == '=:') {
nType |= Macro10.SYMTYPE.INTERNAL;
}
this.addSymbol(sLabel, sOperands, nType);
sOperator = sOperands = "";
}
if (!sOperator && !sOperands) return true;
this.sOperator = sOperator;
/*
* Check the operands for a literal. If the line contains and/or ends with a literal
* we record it and replace it with an internal symbol. We assume only one literal per line,
* especially since they can be open-ended (ie, continue for multiple lines).
*/
var sLiteral = this.getLiteral(sOperands);
if (sLiteral) {
sOperands = sOperands.replace(sLiteral, this.defMacro(Macro10.PSEUDO_OP.LITERAL, this.getLiteral(sRemainder)));
if (!sSeparator) sSeparator = "\t";
}
/*
* Check the operands for any reserved symbols (ie, symbols with a trailing '#', such as "USER#").
*/
var sSymbol;
while ((sSymbol = this.getReserved(sOperands))) {
sOperands = sOperands.replace(sSymbol, sSymbol.slice(0, -1));
}
if (!this.parseMacro(sOperator, sOperands)) {
switch (sOperator) {
case Macro10.PSEUDO_OP.ASCII:
case Macro10.PSEUDO_OP.ASCIZ:
case Macro10.PSEUDO_OP.SIXBIT:
this.defASCII(sRemainder);
break;
case Macro10.PSEUDO_OP.BLOCK:
this.defBLOCK(sOperands);
break;
case Macro10.PSEUDO_OP.BYTE:
this.defBYTE(sOperands);
break;
case Macro10.PSEUDO_OP.END:
this.defEND(sOperands);
break;
case Macro10.PSEUDO_OP.EXP:
this.defWord(sOperands);
break;
case Macro10.PSEUDO_OP.LIT:
this.doLiterals();
break;
case Macro10.PSEUDO_OP.LOC:
this.defLocation(sOperands);
break;
case Macro10.PSEUDO_OP.VAR:
this.doVariables();
break;
case Macro10.PSEUDO_OP.XWD:
this.defXWD(sOperands);
break;
case Macro10.PSEUDO_OP.DEFINE:
case Macro10.PSEUDO_OP.IF1:
case Macro10.PSEUDO_OP.IFDEF:
case Macro10.PSEUDO_OP.IFDIF:
case Macro10.PSEUDO_OP.IFE:
case Macro10.PSEUDO_OP.IFG:
case Macro10.PSEUDO_OP.IFGE:
case Macro10.PSEUDO_OP.IFIDN:
case Macro10.PSEUDO_OP.IFL:
case Macro10.PSEUDO_OP.IFLE:
case Macro10.PSEUDO_OP.IFN:
case Macro10.PSEUDO_OP.IFNDEF:
case Macro10.PSEUDO_OP.IRP:
case Macro10.PSEUDO_OP.IRPC:
case Macro10.PSEUDO_OP.OPDEF:
case Macro10.PSEUDO_OP.REPEAT:
this.defMacro(sOperator, sRemainder);
break;
case Macro10.PSEUDO_OP.PURGE:
this.delSymbols(sOperands);
break;
case Macro10.PSEUDO_OP.LALL: // TODO
case Macro10.PSEUDO_OP.LIST: // TODO
case Macro10.PSEUDO_OP.NOSYM: // TODO
case Macro10.PSEUDO_OP.PAGE: // TODO
case Macro10.PSEUDO_OP.SUBTTL: // TODO
case Macro10.PSEUDO_OP.TITLE: // TODO
case Macro10.PSEUDO_OP.XALL: // TODO
case Macro10.PSEUDO_OP.XLIST: // TODO
break;
default:
this.defWord(sOperator, sSeparator, sOperands);
break;
}
}
return true;
}
/**
* parseLiteral(name, sText)
*
* This is like parseText() except that we set up a new scope, including new aWords and aFixups arrays.
*
* @this {Macro10}
* @param {string} name
* @param {string} sText
*/
parseLiteral(name, sText)
{
this.pushScope(name);
this.parseText(sText);
this.popScope();
}
/**
* parseMacro(name, sOperands)
*
* For OPDEF macros, the operands are opcode values rather than conventional macro parameter values.
* As the MACRO-10 manual explains:
*
* Defines the symbol as an operator equivalent to expression, giving the symbol a fullword value.
* When the operator is later used with operands, the accumulator fields are added, the indirect bits
* are ORed, the memory addresses are added, and the index register addresses are added.
*
* EXAMPLE: OPDEF CAL [MOVE 1,@SYM(2)]
* CAL 1,BOL(2)
*
* RESULT: MOVE 2,@SYM+BOL(4)
*
* The easiest thing to do is parse the OPDEF text, allowing it to generate its default "fullword value",
* then parse the operands in their own scope, extract the bits generated from the operands, and merge them
* into the generated OPDEF value.
*
* @this {Macro10}
* @param {string} name
* @param {string} [sOperands]
* @return {boolean}
*/
parseMacro(name, sOperands)
{
var macro = this.tblMacros[name];
if (!macro) return false;
if (sOperands != null) {
/*
* If this is an OPDEF, then a two-step process is required: generate the OPDEF's value, then parse
* the operands and merge their bits with the OPDEF's bits.
*/
if (macro.nOperand == Macro10.MACRO_OP.OPDEF) {
var nLocation = this.nLocation;
this.parseText(macro.sText);
if (nLocation < this.nLocation) {
/*
* An OPDEF invocation *may* have operands, but it's not required to.
*/
if (!sOperands) return true;
this.pushScope();
this.parseText(sOperands);
var w = this.aWords[0];
var sFixup = this.aFixups[0];
this.popScope();
if (w !== undefined) {
this.aWords[nLocation] += (w & (PDP10.OPCODE.A_FIELD | PDP10.OPCODE.X_FIELD | PDP10.OPCODE.Y_FIELD));
/*
* We can't "OR" (|=) the I_FIELD bit into the target word, because it's a 36-bit value and bitwise
* operators truncate to 32 bits, so we'll use addition, trusting that the field was initially zero.
*/
this.dbg.assert(!(this.aWords[nLocation] & PDP10.OPCODE.I_FIELD));
this.aWords[nLocation] += (w & PDP10.OPCODE.I_FIELD);
if (sFixup) {
if (!this.aFixups[nLocation]) {
this.aFixups[nLocation] = sFixup;
} else {
this.aFixups[nLocation] += '+' + sFixup;
}
}
return true;
}
}
/*
* Either the OPDEF didn't generate any data OR the OPDEF's operands failed to evaluate.
*/
this.error("OPDEF '" + name + "' (" + sOperands + ") failed");
return false;
}
var macroPrev = this.macroCall;
this.macroCall = macro;
macro.aValues = this.getValues(sOperands, true);
this.parseText(macro.sText, macro.aParms, macro.aValues, macro.aDefaults);
/*
* WARNING: Our simplistic approach to macro expansion and processing means that recursive macros
* (such as the SHIFT macro in /apps/pdp10/tests/macro10/TEXT.MAC) could blow the stack. Nothing bad
* should happen (other than a JavaScript stack limit exception aborting the assembly), but it begs
* the question: did MACRO-10 perform any tail recursion optimizations or other tricks to prevent macros
* from gobbling stack, or could they blow MACRO-10's stack just as easily?
*/
this.macroCall = macroPrev;
return true;
}
if (name[0] != '?') return false;
var sOperator = name.substr(1);
switch(sOperator) {
case Macro10.PSEUDO_OP.IFE:
case Macro10.PSEUDO_OP.IFDIF:
case Macro10.PSEUDO_OP.IFNDEF:
if (!macro.nOperand) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IFG:
if (macro.nOperand > 0) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IFGE:
if (macro.nOperand >= 0) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IFL:
if (macro.nOperand < 0) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IFLE:
if (macro.nOperand <= 0) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IF1:
case Macro10.PSEUDO_OP.IFN:
case Macro10.PSEUDO_OP.IFDEF:
case Macro10.PSEUDO_OP.IFIDN:
if (macro.nOperand) {
this.parseText(macro.sText);
}
break;
case Macro10.PSEUDO_OP.IRP:
case Macro10.PSEUDO_OP.IRPC:
for (let i = 0; i < macro.aValues.length; i++) {
this.parseText(macro.sText, macro.aParms, [macro.aValues[i]], []);
}
break;
case Macro10.PSEUDO_OP.REPEAT:
while (macro.nOperand-- > 0) {
this.parseText(macro.sText);
}
break;
default:
this.parseLiteral(name, macro.sText);
break;
}
return true;
}
/**
* parseText(sText, aParms, aValues, aDefaults)
*
* @this {Macro10}
* @param {string} sText
* @param {Array.<string>} [aParms]
* @param {Array.<string>} [aValues]
* @param {Array.<string>} [aDefaults]
*/
parseText(sText, aParms, aValues, aDefaults)
{
/*
* Unlike the caller of parseResources(), we don't split the text using a capture group,
* so all line separators are tossed, and just like parseResources(), we always include a
* uniform CR/LF sequence at the end of each line.
*
* TODO: Consider whether callers should always store their text snippets as line arrays, too,
* to avoid this re-splitting.
*/
var asLines = sText.split(/\r?\n/);
for (var iLine = 0; iLine < asLines.length; iLine++) {
var sLine = asLines[iLine] + '\r\n';
if (!this.parseLine(sLine, aParms, aValues, aDefaults)) break;
}
}
/**
* pushScope(name)
*
* @this {Macro10}
* @param {string} [name] (must be defined for literals only)
*/
pushScope(name)
{
this.stackScopes.push({
name,
aWords: this.aWords,
aFixups: this.aFixups,
nLocation: this.nLocation,
nLocationScope: this.nLocationScope,
nLine: this.nLine
});
this.aWords = [];
this.aFixups = [];
if (this.nLocationScope < 0) this.nLocationScope = this.nLocation;
this.nLocation = 0;
}
/**
* popScope()
*
* @this {Macro10}
*/
popScope()
{
if (!this.stackScopes.length) {
this.error("scope nesting error");
return;
}
var name = this.stackScopes[this.stackScopes.length - 1].name;
if (name) this.aLiterals.push({name, aWords: this.aWords, aFixups: this.aFixups});
var scope = this.stackScopes.pop();
this.aWords = scope.aWords;
this.aFixups = scope.aFixups;