@exercism/codemirror-lang-assembly 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/LICENSE +21 -0
- package/README.md +332 -2
- package/dist/index.cjs +1938 -0
- package/dist/index.d.cts +595 -0
- package/dist/index.d.ts +595 -0
- package/dist/index.js +1914 -0
- package/package.json +62 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1938 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lr = require('@lezer/lr');
|
|
4
|
+
var language = require('@codemirror/language');
|
|
5
|
+
var highlight = require('@lezer/highlight');
|
|
6
|
+
var autocomplete = require('@codemirror/autocomplete');
|
|
7
|
+
|
|
8
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
9
|
+
const RawString = 1,
|
|
10
|
+
BlockOpen = 2,
|
|
11
|
+
BraceOpen = 3,
|
|
12
|
+
Mnemonic = 4,
|
|
13
|
+
DirectiveName = 5,
|
|
14
|
+
MacroName = 6,
|
|
15
|
+
Label = 7,
|
|
16
|
+
Register = 8,
|
|
17
|
+
SizeKeyword = 9,
|
|
18
|
+
Identifier = 10,
|
|
19
|
+
Number = 11,
|
|
20
|
+
ImmediatePrefix = 12,
|
|
21
|
+
Here = 13,
|
|
22
|
+
Star = 14,
|
|
23
|
+
Slash = 15,
|
|
24
|
+
Percent = 16,
|
|
25
|
+
StatementComment = 17,
|
|
26
|
+
AssignOp = 18,
|
|
27
|
+
Equals = 19,
|
|
28
|
+
OperandDirective = 20,
|
|
29
|
+
MacroParameter = 21,
|
|
30
|
+
Dialect_x86 = 0,
|
|
31
|
+
Dialect_arm64 = 1,
|
|
32
|
+
Dialect_mips = 2;
|
|
33
|
+
|
|
34
|
+
function wordSet(from) {
|
|
35
|
+
return new Set(from.split(/\s+/).filter(Boolean));
|
|
36
|
+
}
|
|
37
|
+
function numbered(prefixes, from, to) {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const prefix of prefixes.split(" ")) {
|
|
40
|
+
for (let index = from; index <= to; index++)
|
|
41
|
+
out.push(prefix + index);
|
|
42
|
+
}
|
|
43
|
+
return out.join(" ");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The data directives, which NASM also accepts after `times` and `at`. */
|
|
47
|
+
const DATA_DIRECTIVES = wordSet(`
|
|
48
|
+
db dw dd dq dt do dy dz
|
|
49
|
+
resb resw resd resq rest reso resy resz
|
|
50
|
+
`);
|
|
51
|
+
/** NASM's own directives, written without a `%` in front. */
|
|
52
|
+
const DIRECTIVES$3 = wordSet(`
|
|
53
|
+
${[...DATA_DIRECTIVES].join(" ")}
|
|
54
|
+
equ times incbin
|
|
55
|
+
section segment global extern common static required
|
|
56
|
+
default bits use16 use32 use64 cpu float org absolute
|
|
57
|
+
align alignb sectalign
|
|
58
|
+
struc endstruc istruc at iend
|
|
59
|
+
`);
|
|
60
|
+
/** The section names that a Linux program uses. */
|
|
61
|
+
const SECTIONS$1 = wordSet(`.text .data .bss .rodata .note.GNU-stack`);
|
|
62
|
+
/** NASM's preprocessor directives. */
|
|
63
|
+
const PREPROCESSOR = wordSet(`
|
|
64
|
+
%define %xdefine %idefine %ixdefine %undef %defstr %deftok %defalias
|
|
65
|
+
%assign %iassign %strcat %strlen %substr
|
|
66
|
+
%macro %imacro %endmacro %unmacro %rotate %exitmacro
|
|
67
|
+
%rep %endrep %exitrep
|
|
68
|
+
%if %ifn %elif %elifn %else %endif
|
|
69
|
+
%ifdef %ifndef %elifdef %elifndef
|
|
70
|
+
%ifidn %ifidni %ifnidn %ifnidni %elifidn %elifidni
|
|
71
|
+
%ifmacro %ifnmacro %ifnum %ifstr %ifid %iftoken %ifempty %ifenv %ifctx
|
|
72
|
+
%include %pathsearch %depend %use
|
|
73
|
+
%push %pop %repl %arg %stacksize %local
|
|
74
|
+
%error %warning %fatal %line %pragma %clear
|
|
75
|
+
`);
|
|
76
|
+
const REGISTERS$3 = wordSet(`
|
|
77
|
+
rax rbx rcx rdx rsi rdi rbp rsp ${numbered("r", 8, 15)} rip
|
|
78
|
+
eax ebx ecx edx esi edi ebp esp ${numbered("r", 8, 15).replace(/(\d+)/g, "$1d")} eip
|
|
79
|
+
ax bx cx dx si di bp sp ${numbered("r", 8, 15).replace(/(\d+)/g, "$1w")} ip
|
|
80
|
+
al ah bl bh cl ch dl dh sil dil spl bpl
|
|
81
|
+
${numbered("r", 8, 15).replace(/(\d+)/g, "$1b")}
|
|
82
|
+
${numbered("r", 8, 15).replace(/(\d+)/g, "$1l")}
|
|
83
|
+
cs ds es fs gs ss
|
|
84
|
+
${numbered("cr dr", 0, 15)}
|
|
85
|
+
st ${numbered("st mm", 0, 7)}
|
|
86
|
+
${numbered("xmm ymm zmm", 0, 31)}
|
|
87
|
+
${numbered("k tmm", 0, 7)} ${numbered("bnd", 0, 3)}
|
|
88
|
+
`);
|
|
89
|
+
/** The words NASM writes in front of an operand to qualify it. */
|
|
90
|
+
const SIZE_KEYWORDS$1 = wordSet(`
|
|
91
|
+
byte word dword qword tword oword yword zword
|
|
92
|
+
rel abs strict near far short wrt seg nosplit
|
|
93
|
+
`);
|
|
94
|
+
const PREFIXES = wordSet(`
|
|
95
|
+
rep repe repz repne repnz lock xacquire xrelease bnd notrack
|
|
96
|
+
`);
|
|
97
|
+
const x86Vocabulary = {
|
|
98
|
+
directives: DIRECTIVES$3,
|
|
99
|
+
dotDirectives: null,
|
|
100
|
+
definitionDirectives: wordSet(`${[...DATA_DIRECTIVES].join(" ")} equ times istruc incbin`),
|
|
101
|
+
qualifiesAnOperand: new Set(),
|
|
102
|
+
sizeKeywords: SIZE_KEYWORDS$1,
|
|
103
|
+
registers: REGISTERS$3,
|
|
104
|
+
operandDirectives: wordSet(`${[...DATA_DIRECTIVES].join(" ")} times`),
|
|
105
|
+
prefixes: PREFIXES,
|
|
106
|
+
nasmMacros: true
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** The GNU assembler's directives, as AArch64 code uses them. */
|
|
110
|
+
const DIRECTIVES$2 = wordSet(`
|
|
111
|
+
.text .data .bss .section .pushsection .popsection .previous .subsection
|
|
112
|
+
.global .globl .local .weak .weakref .hidden .protected .internal
|
|
113
|
+
.type .size .extern .ident .file .loc .end
|
|
114
|
+
.equ .set .equiv .eqv .req .unreq
|
|
115
|
+
.byte .hword .short .2byte .word .int .long .4byte .quad .xword .dword .8byte
|
|
116
|
+
.octa .ascii .asciz .string .float .single .double
|
|
117
|
+
.zero .space .skip .fill .lcomm .comm .sleb128 .uleb128
|
|
118
|
+
.align .balign .balignw .balignl .p2align .p2alignw .p2alignl .org
|
|
119
|
+
.macro .endm .exitm .purgem .altmacro .noaltmacro
|
|
120
|
+
.rept .irp .irpc .endr
|
|
121
|
+
.if .ifdef .ifndef .ifeq .ifne .ifgt .ifge .iflt .ifle .ifb .ifnb .ifc .ifnc
|
|
122
|
+
.else .elseif .endif
|
|
123
|
+
.include .incbin .err .error .warning .print .abort
|
|
124
|
+
.arch .arch_extension .cpu .inst .ltorg .pool .variant_pcs .tlsdesccall
|
|
125
|
+
.func .endfunc .struct
|
|
126
|
+
`);
|
|
127
|
+
/** The directives that write data, which make the label in front a variable. */
|
|
128
|
+
wordSet(`
|
|
129
|
+
.byte .hword .short .2byte .word .int .long .4byte .quad .xword .dword .8byte
|
|
130
|
+
.octa .ascii .asciz .string .float .single .double .zero .space .skip .fill
|
|
131
|
+
`);
|
|
132
|
+
/** The section names that a Linux program uses. */
|
|
133
|
+
const SECTIONS = wordSet(`.text .data .bss .rodata .note.GNU-stack`);
|
|
134
|
+
/**
|
|
135
|
+
* The general purpose registers and their aliases, the floating point and
|
|
136
|
+
* vector registers, the SVE registers, and the system registers that a user
|
|
137
|
+
* program can read.
|
|
138
|
+
*/
|
|
139
|
+
const REGISTERS$2 = wordSet(`
|
|
140
|
+
${numbered("x w", 0, 30)} sp wsp xzr wzr lr fp ip0 ip1
|
|
141
|
+
${numbered("v b h s d q z", 0, 31)} ${numbered("p", 0, 15)}
|
|
142
|
+
nzcv fpcr fpsr tpidr_el0 tpidrro_el0 cntvct_el0 cntfrq_el0 dczid_el0 ctr_el0
|
|
143
|
+
`);
|
|
144
|
+
/** Shifts and extensions that follow an operand: `x1, lsl #3`. */
|
|
145
|
+
const SHIFTS = wordSet(`
|
|
146
|
+
lsl lsr asr ror msl
|
|
147
|
+
uxtb uxth uxtw uxtx sxtb sxth sxtw sxtx
|
|
148
|
+
`);
|
|
149
|
+
/** The relocation operators written between colons: `:lo12:msg`. */
|
|
150
|
+
const RELOCATIONS = wordSet(`
|
|
151
|
+
lo12 got got_lo12 pg_hi21 pg_hi21_nc
|
|
152
|
+
abs_g0 abs_g0_nc abs_g0_s abs_g1 abs_g1_nc abs_g1_s
|
|
153
|
+
abs_g2 abs_g2_nc abs_g2_s abs_g3
|
|
154
|
+
tlsdesc tlsdesc_lo12 gottprel gottprel_lo12
|
|
155
|
+
tprel_hi12 tprel_lo12 tprel_lo12_nc tprel_g0 tprel_g0_nc tprel_g1 tprel_g1_nc tprel_g2
|
|
156
|
+
dtprel_hi12 dtprel_lo12 dtprel_lo12_nc dtprel_g0 dtprel_g0_nc dtprel_g1 dtprel_g1_nc dtprel_g2
|
|
157
|
+
`);
|
|
158
|
+
/** The condition codes, written after `b.` and as the last operand of `csel`. */
|
|
159
|
+
const CONDITIONS$1 = wordSet(`eq ne cs hs cc lo mi pl vs vc hi ls ge lt gt le al nv`);
|
|
160
|
+
const arm64Vocabulary = {
|
|
161
|
+
// The GNU assembler spells every directive with a dot.
|
|
162
|
+
directives: new Set(),
|
|
163
|
+
dotDirectives: DIRECTIVES$2,
|
|
164
|
+
// `count .req x19` names `count` as an alias for a register.
|
|
165
|
+
definitionDirectives: wordSet(".req"),
|
|
166
|
+
qualifiesAnOperand: new Set(),
|
|
167
|
+
sizeKeywords: new Set([...SHIFTS, ...RELOCATIONS]),
|
|
168
|
+
registers: REGISTERS$2,
|
|
169
|
+
operandDirectives: new Set(),
|
|
170
|
+
prefixes: new Set(),
|
|
171
|
+
nasmMacros: false,
|
|
172
|
+
// `v0.16b`, `v1.4s` and `v2.d` are `v0`, `v1` and `v2`.
|
|
173
|
+
registerSuffix: /\.\d*[bhsdq]$/,
|
|
174
|
+
lineEndKeywords: CONDITIONS$1,
|
|
175
|
+
numericLabelReferences: true
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** The directives of MARS, the MIPS simulator used by Exercism's test runner. */
|
|
179
|
+
const DIRECTIVES$1 = wordSet(`
|
|
180
|
+
.text .data .ktext .kdata .globl .extern
|
|
181
|
+
.word .half .byte .float .double .ascii .asciiz .space .align
|
|
182
|
+
.eqv .macro .end_macro .include .set
|
|
183
|
+
`);
|
|
184
|
+
/** The directives that write data, which make the label in front a variable. */
|
|
185
|
+
wordSet(`.word .half .byte .float .double .ascii .asciiz .space`);
|
|
186
|
+
/**
|
|
187
|
+
* The registers by number and by name, and the floating point registers. They
|
|
188
|
+
* are listed without the `$` that MIPS writes in front of every register.
|
|
189
|
+
*/
|
|
190
|
+
const REGISTER_NAMES = wordSet(`
|
|
191
|
+
zero at v0 v1 ${numbered("a", 0, 3)} ${numbered("t", 0, 9)} ${numbered("s", 0, 7)}
|
|
192
|
+
k0 k1 gp sp fp ra
|
|
193
|
+
`);
|
|
194
|
+
const REGISTERS$1 = new Set([
|
|
195
|
+
...REGISTER_NAMES,
|
|
196
|
+
...wordSet(numbered("", 0, 31)),
|
|
197
|
+
...wordSet(numbered("f", 0, 31))
|
|
198
|
+
]);
|
|
199
|
+
/** The registers as they are written, with the `$`, for completion. */
|
|
200
|
+
const WRITTEN_REGISTERS = [
|
|
201
|
+
...[...REGISTER_NAMES].map((name) => "$" + name),
|
|
202
|
+
...numbered("$f", 0, 31).split(" "),
|
|
203
|
+
...numbered("$", 0, 31).split(" ")
|
|
204
|
+
];
|
|
205
|
+
const mipsVocabulary = {
|
|
206
|
+
// MARS spells every directive with a dot, and every label has a colon.
|
|
207
|
+
directives: new Set(),
|
|
208
|
+
dotDirectives: DIRECTIVES$1,
|
|
209
|
+
definitionDirectives: new Set(),
|
|
210
|
+
qualifiesAnOperand: new Set(),
|
|
211
|
+
sizeKeywords: new Set(),
|
|
212
|
+
registers: REGISTERS$1,
|
|
213
|
+
operandDirectives: new Set(),
|
|
214
|
+
prefixes: new Set(),
|
|
215
|
+
nasmMacros: false,
|
|
216
|
+
dollarRegisters: true,
|
|
217
|
+
hashComments: true,
|
|
218
|
+
percentParameters: true
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Each dialect of the grammar, with the vocabulary its tokenizer reads. To add
|
|
223
|
+
* an instruction set, name a dialect in `@dialects` in assembly.grammar, write
|
|
224
|
+
* its vocabulary in a file of its own, and list it here.
|
|
225
|
+
*/
|
|
226
|
+
const dialectVocabularies = [
|
|
227
|
+
[Dialect_x86, x86Vocabulary],
|
|
228
|
+
[Dialect_arm64, arm64Vocabulary],
|
|
229
|
+
[Dialect_mips, mipsVocabulary]
|
|
230
|
+
];
|
|
231
|
+
|
|
232
|
+
function isDigit(ch) {
|
|
233
|
+
return ch >= 48 /* Ch.Zero */ && ch <= 57 /* Ch.Nine */;
|
|
234
|
+
}
|
|
235
|
+
function isHexDigit(ch) {
|
|
236
|
+
return (isDigit(ch) ||
|
|
237
|
+
(ch >= 65 /* Ch.UpperA */ && ch <= 70 /* Ch.UpperF */) ||
|
|
238
|
+
(ch >= 97 /* Ch.LowerA */ && ch <= 102 /* Ch.LowerF */));
|
|
239
|
+
}
|
|
240
|
+
// Anything above ASCII counts as a letter. Assembly has no character set worth
|
|
241
|
+
// the name, and a symbol with an accent in it, or a zero-width space that a
|
|
242
|
+
// listing dragged in, is still part of the word it stands inside.
|
|
243
|
+
function isLetter(ch) {
|
|
244
|
+
return ((ch >= 65 /* Ch.UpperA */ && ch <= 90 /* Ch.UpperZ */) || (ch >= 97 /* Ch.LowerA */ && ch <= 122 /* Ch.LowerZ */) || ch > 127);
|
|
245
|
+
}
|
|
246
|
+
// A word may open with a dot, because that is how gas spells a directive and
|
|
247
|
+
// how NASM spells a local label; with an `@` or a `?`, because that is how MASM
|
|
248
|
+
// spells a name the programmer did not choose; and with a backslash, because
|
|
249
|
+
// that is how a gas macro spells its own arguments (`\1`, `\@`).
|
|
250
|
+
function isWordStart(ch) {
|
|
251
|
+
return (isLetter(ch) ||
|
|
252
|
+
ch == 95 /* Ch.Underscore */ ||
|
|
253
|
+
ch == 46 /* Ch.Dot */ ||
|
|
254
|
+
ch == 63 /* Ch.Question */ ||
|
|
255
|
+
ch == 64 /* Ch.At */ ||
|
|
256
|
+
ch == 92 /* Ch.Backslash */);
|
|
257
|
+
}
|
|
258
|
+
// `@` closes a word as well as opening one: `printf@PLT` is one symbol, and the
|
|
259
|
+
// relocation on the end of it is not a comment.
|
|
260
|
+
function isWordChar(ch) {
|
|
261
|
+
return (isLetter(ch) ||
|
|
262
|
+
isDigit(ch) ||
|
|
263
|
+
ch == 95 /* Ch.Underscore */ ||
|
|
264
|
+
ch == 46 /* Ch.Dot */ ||
|
|
265
|
+
ch == 63 /* Ch.Question */ ||
|
|
266
|
+
ch == 36 /* Ch.Dollar */ ||
|
|
267
|
+
ch == 64 /* Ch.At */);
|
|
268
|
+
}
|
|
269
|
+
function pastesAName(ch) {
|
|
270
|
+
return isLetter(ch) || ch == 95 /* Ch.Underscore */ || ch == 64 /* Ch.At */;
|
|
271
|
+
}
|
|
272
|
+
function isSpace(ch) {
|
|
273
|
+
return ch == 32 /* Ch.Space */ || ch == 9 /* Ch.Tab */ || ch == 11 /* Ch.VerticalTab */ || ch == 12 /* Ch.FormFeed */;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* What an assembler does with a word that stands where a statement stands, and
|
|
277
|
+
* which no instruction set can claim. Everything here is the assembler's own
|
|
278
|
+
* vocabulary: reserving storage, naming a section, opening a macro.
|
|
279
|
+
*
|
|
280
|
+
* `set` and `res` are missing on purpose. Both are directives to some
|
|
281
|
+
* assemblers and instructions to the Z80, and an instruction is the commoner
|
|
282
|
+
* reading; a directive spelt `set` is nearly always preceded by the name it
|
|
283
|
+
* sets, which the label rule catches first.
|
|
284
|
+
*/
|
|
285
|
+
const DIRECTIVES = wordSet(`
|
|
286
|
+
db dw dd dq dt do ddq dqword dz
|
|
287
|
+
defb defw defl defs defm dm dc ds dcb
|
|
288
|
+
resb resw resd resq rest reso resy resz rb rw rd rq rt
|
|
289
|
+
byte sbyte word sword dword sdword fword qword tbyte oword
|
|
290
|
+
real4 real8 real10
|
|
291
|
+
equ label textequ catstr
|
|
292
|
+
struc endstruc struct ends union record
|
|
293
|
+
macro endmacro endm rept endr irp irpc times purge
|
|
294
|
+
proc endp local
|
|
295
|
+
section segment org end entry format model stack heap assume invoke
|
|
296
|
+
public global globl extern extrn common comm static
|
|
297
|
+
include includelib incbin
|
|
298
|
+
bits use16 use32 use64 cpu default
|
|
299
|
+
align alignb even cnop
|
|
300
|
+
if else elseif endif ifdef ifndef ife
|
|
301
|
+
list nolist page title subttl xdef xref opt
|
|
302
|
+
`);
|
|
303
|
+
/**
|
|
304
|
+
* The dot-prefixed vocabulary: the GNU assembler's, and the MASM, LC-3 and VAX
|
|
305
|
+
* words that borrowed the spelling. It is a closed list because a dot-prefixed
|
|
306
|
+
* word that is not on it is a label: NASM spells a local label `.loop` and
|
|
307
|
+
* writes no colon after it, and every assembler that took the convention did
|
|
308
|
+
* the same.
|
|
309
|
+
*/
|
|
310
|
+
const DOT_DIRECTIVES = wordSet(`
|
|
311
|
+
.model .stack .code .const .startup .exit .dosseg .fardata .radix .seq
|
|
312
|
+
.alpha .cref .nocref .lall .sall .xall .listall .repeat .until .untilcxz
|
|
313
|
+
.while .endw .break .continue .186 .286 .386 .386p .486 .586 .686 .8086
|
|
314
|
+
.8087 .mmx .xmm .k3d .no87
|
|
315
|
+
.orig .stringz .blkw .blkb .blkl .blkd .entry .rsset .rsreset .definelabel
|
|
316
|
+
.align16 .asg .eval .newblock .usect .sect .label .field .bes
|
|
317
|
+
|
|
318
|
+
.abort .align .altmacro .arch .arch_extension .arm .ascii .asciz .asciiz
|
|
319
|
+
.att_syntax .balign .balignw .balignl .bss .byte .code .code16 .code32
|
|
320
|
+
.code64 .comm .common .cpu .data .dc .dcb .double .ds .eabi_attribute .eject
|
|
321
|
+
.else .elseif .end .endef .endfunc .endif .endm .endmacro .endr .equ .equiv
|
|
322
|
+
.eqv .err .error .exitm .extern .fail .file .fill .float .fnend .fnstart
|
|
323
|
+
.fpu .func .global .globl .gnu_attribute .handlerdata .hidden .hword .ident
|
|
324
|
+
.if .ifb .ifc .ifdef .ifeq .ifge .ifgt .ifle .iflt .ifnb .ifnc .ifndef
|
|
325
|
+
.ifne .incbin .include .inst .int .intel_syntax .internal .irp .irpc .lcomm
|
|
326
|
+
.line .linkonce .list .llong .loc .local .long .ltorg .macro .movsp .mri
|
|
327
|
+
.noaltmacro .nolist .octa .org .p2align .p2alignl .p2alignw .pad
|
|
328
|
+
.personality .personalityindex .pool .popsection .previous .print
|
|
329
|
+
.protected .psize .purgem .pushsection .quad .rept .req .rodata .save
|
|
330
|
+
.sbttl .scl .section .seh_endprologue .seh_endproc .seh_proc .seh_pushreg
|
|
331
|
+
.seh_stackalloc .set .setfp .short .single .size .skip .sleb128 .space
|
|
332
|
+
.stabd .stabn .stabs .string .string8 .string16 .struct .subsection .symver
|
|
333
|
+
.syntax .tag .text .thumb .thumb_func .thumb_set .title .type .uleb128
|
|
334
|
+
.unreq .val .version .vsave .warning .weak .weakref .word .zero
|
|
335
|
+
`);
|
|
336
|
+
/**
|
|
337
|
+
* A directive that can only follow the name it is about. This is what tells a
|
|
338
|
+
* label from a mnemonic on a line that writes no colon: `bdos equ 5h` names
|
|
339
|
+
* `bdos`, and `message db 'x'` names `message`, because neither `equ` nor `db`
|
|
340
|
+
* can open a statement of its own once a word already has.
|
|
341
|
+
*/
|
|
342
|
+
const DEFINITION_DIRECTIVES = wordSet(`
|
|
343
|
+
db dw dd dq dt do ddq dz defb defw defl defs defm dm
|
|
344
|
+
dc dcb dc.b dc.w dc.l dc.s dc.d ds.b ds.w ds.l ds.s ds.d dcb.b dcb.w dcb.l
|
|
345
|
+
resb resw resd resq rest reso resy resz rb rw rq
|
|
346
|
+
byte sbyte word sword dword sdword fword qword tbyte oword
|
|
347
|
+
real4 real8 real10
|
|
348
|
+
equ set assign label textequ catstr
|
|
349
|
+
proc macro struc struct segment section union record ends endp endm
|
|
350
|
+
.equ .set .equiv .byte .word .long .quad .short .hword .int .octa .single
|
|
351
|
+
.double .float .ascii .asciz .asciiz .string .space .skip .zero .fill
|
|
352
|
+
.comm .lcomm .globl .global .type .size .req .struct
|
|
353
|
+
`);
|
|
354
|
+
/**
|
|
355
|
+
* The subset of the above that is also written in front of an operand rather
|
|
356
|
+
* than in front of a name. `msg BYTE "hi"` declares `msg`, and
|
|
357
|
+
* `mov byte ptr [x], 1` does not declare `mov`; the `ptr` behind it is what
|
|
358
|
+
* says so.
|
|
359
|
+
*/
|
|
360
|
+
const QUALIFIES_AN_OPERAND = wordSet(`
|
|
361
|
+
byte sbyte word sword dword sdword fword qword tbyte oword
|
|
362
|
+
real4 real8 real10
|
|
363
|
+
`);
|
|
364
|
+
/**
|
|
365
|
+
* A word that qualifies an operand rather than being one. Intel syntax writes
|
|
366
|
+
* the width of a memory reference in front of it, and MASM writes several more
|
|
367
|
+
* things the same way.
|
|
368
|
+
*/
|
|
369
|
+
const SIZE_KEYWORDS = wordSet(`
|
|
370
|
+
byte sbyte word sword dword sdword fword qword tbyte oword yword zword
|
|
371
|
+
xmmword ymmword zmmword real4 real8 real10
|
|
372
|
+
ptr offset short near far dup rel seg wrt strict abs sizeof lengthof
|
|
373
|
+
`);
|
|
374
|
+
/**
|
|
375
|
+
* Registers, from every instruction set the corpus this was measured against
|
|
376
|
+
* carries. A list is the only way to know one: a register is spelt like any
|
|
377
|
+
* other word, and the assembler that reads it is the only thing that knows
|
|
378
|
+
* `d0` names one and `msg` does not.
|
|
379
|
+
*
|
|
380
|
+
* A prefix is stripped before the lookup, so `%eax`, `$t0` and `eax` all land
|
|
381
|
+
* on the same entry, and an assembler that writes no prefix is read as well as
|
|
382
|
+
* one that insists on it. The cost is a label named `a` or `x` read as a
|
|
383
|
+
* register, which is the trade every assembly highlighter makes.
|
|
384
|
+
*/
|
|
385
|
+
const REGISTERS = wordSet(`
|
|
386
|
+
${numbered("r", 0, 31)} ${numbered("x w", 0, 31)} ${numbered("f", 0, 31)}
|
|
387
|
+
${numbered("d", 0, 31)} ${numbered("s", 0, 31)} ${numbered("q", 0, 31)}
|
|
388
|
+
${numbered("b h v", 0, 31)} ${numbered("t", 0, 9)} ${numbered("a", 0, 7)}
|
|
389
|
+
${numbered("v", 0, 1)} ${numbered("k", 0, 7)} ${numbered("cr", 0, 8)}
|
|
390
|
+
${numbered("dr", 0, 7)} ${numbered("st mm", 0, 7)}
|
|
391
|
+
${numbered("xmm ymm zmm", 0, 31)} ${numbered("ft fs fa", 0, 11)}
|
|
392
|
+
al ah bl bh cl ch dl dh sil dil spl bpl
|
|
393
|
+
ax bx cx dx si di sp bp ip
|
|
394
|
+
eax ebx ecx edx esi edi esp ebp eip eflags
|
|
395
|
+
rax rbx rcx rdx rsi rdi rsp rbp rip rflags flags
|
|
396
|
+
r8b r9b r10b r11b r12b r13b r14b r15b
|
|
397
|
+
r8w r9w r10w r11w r12w r13w r14w r15w
|
|
398
|
+
r8d r9d r10d r11d r12d r13d r14d r15d
|
|
399
|
+
cs ds es fs gs ss st
|
|
400
|
+
lr pc fp sb sl cpsr spsr apsr xzr wzr wsp
|
|
401
|
+
a1 a2 a3 a4 v1 v2 v3 v4 v5 v6 v7 v8
|
|
402
|
+
sr ccr usp ssp vbr sfc dfc
|
|
403
|
+
a b c d e f h l i m p r s x y z
|
|
404
|
+
af bc de hl ix iy ixh ixl iyh iyl psw acc dptr
|
|
405
|
+
zero at gp tp ra hi lo ctr xer msr
|
|
406
|
+
xl xh yl yh zl zh
|
|
407
|
+
`);
|
|
408
|
+
/** The shared vocabulary, which reads every assembler at once. */
|
|
409
|
+
const generic = {
|
|
410
|
+
directives: DIRECTIVES,
|
|
411
|
+
dotDirectives: DOT_DIRECTIVES,
|
|
412
|
+
definitionDirectives: DEFINITION_DIRECTIVES,
|
|
413
|
+
qualifiesAnOperand: QUALIFIES_AN_OPERAND,
|
|
414
|
+
sizeKeywords: SIZE_KEYWORDS,
|
|
415
|
+
registers: REGISTERS,
|
|
416
|
+
operandDirectives: new Set(),
|
|
417
|
+
prefixes: new Set(),
|
|
418
|
+
nasmMacros: false
|
|
419
|
+
};
|
|
420
|
+
function vocabularyFor(stack) {
|
|
421
|
+
for (const [dialect, vocabulary] of dialectVocabularies) {
|
|
422
|
+
if (stack.dialectEnabled(dialect))
|
|
423
|
+
return vocabulary;
|
|
424
|
+
}
|
|
425
|
+
return generic;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Every word, number and sigil, read against the slot it stands in. The parser
|
|
429
|
+
* is asked which slot that is rather than told: a mnemonic can only stand where
|
|
430
|
+
* a statement can begin, so `stack.canShift(Mnemonic)` is the question, and the
|
|
431
|
+
* answer separates `byte` the directive from `byte` the size, and `a` the
|
|
432
|
+
* mnemonic-shaped word from `a` the accumulator.
|
|
433
|
+
*/
|
|
434
|
+
const words = new lr.ExternalTokenizer((input, stack) => {
|
|
435
|
+
const start = input.next;
|
|
436
|
+
if (start == -1 /* Ch.EOF */)
|
|
437
|
+
return;
|
|
438
|
+
const statement = stack.canShift(Mnemonic);
|
|
439
|
+
const vocabulary = vocabularyFor(stack);
|
|
440
|
+
if (isDigit(start)) {
|
|
441
|
+
// `1:` and `2:` are how gas writes a label it does not want to name.
|
|
442
|
+
if (statement && digitsThenColon(input))
|
|
443
|
+
return acceptLabel(input);
|
|
444
|
+
// `1b` and `1f` jump back and forward to the nearest `1:`.
|
|
445
|
+
if (!statement && vocabulary.numericLabelReferences && numericLabelReference(input)) {
|
|
446
|
+
return input.acceptToken(Identifier);
|
|
447
|
+
}
|
|
448
|
+
scanNumber(input);
|
|
449
|
+
return input.acceptToken(Number);
|
|
450
|
+
}
|
|
451
|
+
if (start == 36 /* Ch.Dollar */)
|
|
452
|
+
return dollar(input, statement, vocabulary);
|
|
453
|
+
if (start == 37 /* Ch.Percent */)
|
|
454
|
+
return percent(input, statement, vocabulary);
|
|
455
|
+
if (start == 35 /* Ch.Hash */) {
|
|
456
|
+
if (vocabulary.hashComments)
|
|
457
|
+
return operatorOrComment(input, true, StatementComment);
|
|
458
|
+
return hash(input, statement);
|
|
459
|
+
}
|
|
460
|
+
// A star in column one is a comment to the 68000 assemblers and to MASM, and
|
|
461
|
+
// a slash in column one is one to the PDP-8's. Anywhere else the one
|
|
462
|
+
// multiplies and the other divides, and anywhere else is where the parser has
|
|
463
|
+
// already begun reading operands.
|
|
464
|
+
if (start == 42 /* Ch.Star */)
|
|
465
|
+
return operatorOrComment(input, statement, Star);
|
|
466
|
+
if (start == 47 /* Ch.Slash */)
|
|
467
|
+
return operatorOrComment(input, statement, Slash);
|
|
468
|
+
// A capital C opening a statement is GNU MP's comment character, and GNU MP
|
|
469
|
+
// is the assembly that ships inside everybody else's tree. It has to be the
|
|
470
|
+
// whole of the word: `CALL` is a mnemonic and `C:` is a label.
|
|
471
|
+
if (start == 67 /* Ch.UpperC */ && statement && isSpace(input.peek(1))) {
|
|
472
|
+
input.advance();
|
|
473
|
+
return operatorOrComment(input, true, StatementComment);
|
|
474
|
+
}
|
|
475
|
+
// `=` opens a statement or stands inside an operand, and the two are told
|
|
476
|
+
// apart here rather than in the grammar, where one rule would have to guess.
|
|
477
|
+
if (start == 61 /* Ch.Equals */) {
|
|
478
|
+
input.advance();
|
|
479
|
+
return input.acceptToken(statement ? AssignOp : Equals);
|
|
480
|
+
}
|
|
481
|
+
// gas writes the location counter as a bare dot, which is a word start
|
|
482
|
+
// everywhere else.
|
|
483
|
+
if (start == 46 /* Ch.Dot */ && !isWordChar(input.peek(1))) {
|
|
484
|
+
input.advance();
|
|
485
|
+
return input.acceptToken(Here);
|
|
486
|
+
}
|
|
487
|
+
if (isWordStart(start)) {
|
|
488
|
+
const word = scanWord(input);
|
|
489
|
+
return statement
|
|
490
|
+
? acceptStatementWord(input, word, vocabulary)
|
|
491
|
+
: acceptOperandWord(input, word, vocabulary);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
/** `$1F` is hexadecimal, `$t0` is a register and `$` alone is here. */
|
|
495
|
+
function dollar(input, statement, vocabulary) {
|
|
496
|
+
if (input.peek(1) == 36 /* Ch.Dollar */) {
|
|
497
|
+
input.advance();
|
|
498
|
+
input.advance();
|
|
499
|
+
return input.acceptToken(Here);
|
|
500
|
+
}
|
|
501
|
+
// To MIPS, `$a0` and `$31` are registers, and `$` opens nothing else.
|
|
502
|
+
if (vocabulary.dollarRegisters && isWordChar(input.peek(1))) {
|
|
503
|
+
input.advance();
|
|
504
|
+
const word = scanWord(input);
|
|
505
|
+
return input.acceptToken(vocabulary.registers.has(word.toLowerCase()) ? Register : Identifier);
|
|
506
|
+
}
|
|
507
|
+
if (isHexDigit(input.peek(1))) {
|
|
508
|
+
let offset = 1;
|
|
509
|
+
while (isHexDigit(input.peek(offset)))
|
|
510
|
+
offset++;
|
|
511
|
+
// `$a0` is a MIPS register and `$a0` is a 6502 address, and only a word
|
|
512
|
+
// that cannot be read as hexadecimal settles it. Hexadecimal wins the tie,
|
|
513
|
+
// because an assembler that prefixes its registers writes far fewer of
|
|
514
|
+
// them than one that prefixes its addresses writes addresses.
|
|
515
|
+
if (!isWordChar(input.peek(offset))) {
|
|
516
|
+
while (offset-- > 0)
|
|
517
|
+
input.advance();
|
|
518
|
+
return input.acceptToken(Number);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (isWordStart(input.peek(1)) || isHexDigit(input.peek(1))) {
|
|
522
|
+
input.advance();
|
|
523
|
+
const word = scanWord(input);
|
|
524
|
+
return statement
|
|
525
|
+
? acceptStatementWord(input, word, vocabulary)
|
|
526
|
+
: acceptOperandWord(input, word, vocabulary);
|
|
527
|
+
}
|
|
528
|
+
input.advance();
|
|
529
|
+
input.acceptToken(Here);
|
|
530
|
+
}
|
|
531
|
+
/** `%macro` opens a statement, `%eax` is a register and `%1010` is binary. */
|
|
532
|
+
function percent(input, statement, vocabulary) {
|
|
533
|
+
const after = input.peek(1);
|
|
534
|
+
if (vocabulary.nasmMacros && nasmMacroWord(input, statement))
|
|
535
|
+
return;
|
|
536
|
+
if (statement && (isWordStart(after) || after == 37 /* Ch.Percent */)) {
|
|
537
|
+
input.advance();
|
|
538
|
+
if (input.next == 37 /* Ch.Percent */)
|
|
539
|
+
input.advance();
|
|
540
|
+
scanWord(input);
|
|
541
|
+
return input.acceptToken(MacroName);
|
|
542
|
+
}
|
|
543
|
+
if (isWordStart(after)) {
|
|
544
|
+
input.advance();
|
|
545
|
+
const word = scanWord(input);
|
|
546
|
+
if (vocabulary.percentParameters)
|
|
547
|
+
return input.acceptToken(MacroParameter);
|
|
548
|
+
return acceptOperandWord(input, word, vocabulary);
|
|
549
|
+
}
|
|
550
|
+
// `%1010` is binary to a Motorola assembler and `7%3` is a remainder to
|
|
551
|
+
// everyone else, and what stands in front of the sign is what tells them
|
|
552
|
+
// apart: a literal opens an operand and a remainder joins two.
|
|
553
|
+
if (isDigit(after) && !followsAValue(input)) {
|
|
554
|
+
input.advance();
|
|
555
|
+
scanNumber(input);
|
|
556
|
+
return input.acceptToken(Number);
|
|
557
|
+
}
|
|
558
|
+
input.advance();
|
|
559
|
+
input.acceptToken(Percent);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* NASM gives `%` three more jobs inside a macro. `%1` is a parameter, `%%end`
|
|
563
|
+
* is a label local to one expansion of the macro, and `%$x` is local to a
|
|
564
|
+
* context. A label of either kind may open a statement: `%%end:`.
|
|
565
|
+
*/
|
|
566
|
+
function nasmMacroWord(input, statement) {
|
|
567
|
+
const after = input.peek(1);
|
|
568
|
+
if (isDigit(after) || ((after == 43 /* Ch.Plus */ || after == 45 /* Ch.Minus */) && isDigit(input.peek(2)))) {
|
|
569
|
+
if (followsAValue(input))
|
|
570
|
+
return false;
|
|
571
|
+
input.advance();
|
|
572
|
+
if (!isDigit(input.next))
|
|
573
|
+
input.advance();
|
|
574
|
+
while (isDigit(input.next))
|
|
575
|
+
input.advance();
|
|
576
|
+
input.acceptToken(MacroParameter);
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
let offset = 1;
|
|
580
|
+
if (after == 37 /* Ch.Percent */)
|
|
581
|
+
offset = 2;
|
|
582
|
+
else if (after == 36 /* Ch.Dollar */) {
|
|
583
|
+
offset = 2;
|
|
584
|
+
while (input.peek(offset) == 36 /* Ch.Dollar */)
|
|
585
|
+
offset++;
|
|
586
|
+
}
|
|
587
|
+
else
|
|
588
|
+
return false;
|
|
589
|
+
if (!isWordStart(input.peek(offset)))
|
|
590
|
+
return false;
|
|
591
|
+
for (let i = 0; i < offset; i++)
|
|
592
|
+
input.advance();
|
|
593
|
+
scanWord(input);
|
|
594
|
+
if (statement) {
|
|
595
|
+
if (input.next == 58 /* Ch.Colon */)
|
|
596
|
+
input.advance();
|
|
597
|
+
input.acceptToken(Label);
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
input.acceptToken(Identifier);
|
|
601
|
+
}
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
function followsAValue(input) {
|
|
605
|
+
const behind = input.peek(-1);
|
|
606
|
+
return isWordChar(behind) || behind == 41 /* Ch.ParenR */ || behind == 93 /* Ch.BracketR */;
|
|
607
|
+
}
|
|
608
|
+
function operatorOrComment(input, statement, operator) {
|
|
609
|
+
input.advance();
|
|
610
|
+
if (!statement)
|
|
611
|
+
return input.acceptToken(operator);
|
|
612
|
+
while (input.next != 10 /* Ch.Newline */ && input.next != 13 /* Ch.Return */ && input.next != -1 /* Ch.EOF */) {
|
|
613
|
+
input.advance();
|
|
614
|
+
}
|
|
615
|
+
input.acceptToken(StatementComment);
|
|
616
|
+
}
|
|
617
|
+
/** `#define` opens a statement; `#42` prefixes an immediate. */
|
|
618
|
+
function hash(input, statement) {
|
|
619
|
+
if (statement && isWordStart(input.peek(1))) {
|
|
620
|
+
input.advance();
|
|
621
|
+
scanWord(input);
|
|
622
|
+
return input.acceptToken(MacroName);
|
|
623
|
+
}
|
|
624
|
+
input.advance();
|
|
625
|
+
input.acceptToken(ImmediatePrefix);
|
|
626
|
+
}
|
|
627
|
+
function scanWord(input) {
|
|
628
|
+
let word = String.fromCharCode(input.next);
|
|
629
|
+
input.advance();
|
|
630
|
+
// A gas macro pastes its arguments into a name: `.copy_\str:` and
|
|
631
|
+
// `.loop\@:` are one label each.
|
|
632
|
+
while (isWordChar(input.next) || (input.next == 92 /* Ch.Backslash */ && pastesAName(input.peek(1)))) {
|
|
633
|
+
word += String.fromCharCode(input.next);
|
|
634
|
+
input.advance();
|
|
635
|
+
}
|
|
636
|
+
return word;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* A number is read to the end of what it is spelt with rather than by radix,
|
|
640
|
+
* because the radix is written in four places: in front (`0x1F`), behind
|
|
641
|
+
* (`1Fh`), as a sigil (`$1F`) and nowhere at all. Nothing that opens with a
|
|
642
|
+
* digit is a symbol in any assembler, so reading the whole run costs nothing
|
|
643
|
+
* and spares the tokenizer four spellings of the same thing.
|
|
644
|
+
*/
|
|
645
|
+
function scanNumber(input) {
|
|
646
|
+
input.advance();
|
|
647
|
+
for (;;) {
|
|
648
|
+
const ch = input.next;
|
|
649
|
+
if (isLetter(ch) || isDigit(ch) || ch == 95 /* Ch.Underscore */)
|
|
650
|
+
input.advance();
|
|
651
|
+
else if (ch == 46 /* Ch.Dot */ && isDigit(input.peek(1)))
|
|
652
|
+
input.advance();
|
|
653
|
+
else
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
function numericLabelReference(input) {
|
|
658
|
+
let offset = 0;
|
|
659
|
+
while (isDigit(input.peek(offset)))
|
|
660
|
+
offset++;
|
|
661
|
+
const suffix = input.peek(offset);
|
|
662
|
+
if ((suffix != 98 /* Ch.LowerB */ && suffix != 102 /* Ch.LowerF */) || isWordChar(input.peek(offset + 1))) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
for (let i = 0; i <= offset; i++)
|
|
666
|
+
input.advance();
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
function digitsThenColon(input) {
|
|
670
|
+
let offset = 0;
|
|
671
|
+
while (isDigit(input.peek(offset)))
|
|
672
|
+
offset++;
|
|
673
|
+
return input.peek(offset) == 58 /* Ch.Colon */;
|
|
674
|
+
}
|
|
675
|
+
function acceptLabel(input) {
|
|
676
|
+
while (isDigit(input.next))
|
|
677
|
+
input.advance();
|
|
678
|
+
input.advance();
|
|
679
|
+
input.acceptToken(Label);
|
|
680
|
+
}
|
|
681
|
+
function acceptStatementWord(input, word, vocabulary) {
|
|
682
|
+
// A colon behind it settles the question and no assembler disagrees.
|
|
683
|
+
if (input.next == 58 /* Ch.Colon */) {
|
|
684
|
+
input.advance();
|
|
685
|
+
if (input.next == 58 /* Ch.Colon */)
|
|
686
|
+
input.advance();
|
|
687
|
+
return input.acceptToken(Label);
|
|
688
|
+
}
|
|
689
|
+
const lower = word.toLowerCase();
|
|
690
|
+
if (lower.charCodeAt(0) == 46 /* Ch.Dot */) {
|
|
691
|
+
const dots = vocabulary.dotDirectives;
|
|
692
|
+
const directive = dots != null && (dots.has(lower) || lower.startsWith(".cfi_"));
|
|
693
|
+
return input.acceptToken(directive ? DirectiveName : Label);
|
|
694
|
+
}
|
|
695
|
+
if (vocabulary.directives.has(lower))
|
|
696
|
+
return input.acceptToken(DirectiveName);
|
|
697
|
+
// Nothing behind it but a directive that needs a name in front: the word is
|
|
698
|
+
// that name.
|
|
699
|
+
if (definitionFollows(input, vocabulary))
|
|
700
|
+
return input.acceptToken(Label);
|
|
701
|
+
// `rep movsb` is one instruction, so the prefix takes the mnemonic behind it.
|
|
702
|
+
if (vocabulary.prefixes.has(lower)) {
|
|
703
|
+
const offset = skipSpace(input, 0);
|
|
704
|
+
const next = peekWord(input, offset);
|
|
705
|
+
if (offset > 0 && next && !vocabulary.registers.has(next.toLowerCase())) {
|
|
706
|
+
for (let i = 0; i < offset + next.length; i++)
|
|
707
|
+
input.advance();
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
input.acceptToken(Mnemonic);
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* The IBM assemblers ask a symbol about itself with a letter and an apostrophe
|
|
714
|
+
* that never closes: `L'TBL` is the length of `TBL`, and `T'`, `S'`, `I'`, `K'`,
|
|
715
|
+
* `N'`, `D'` and `O'` ask the other questions. Left to the character literal
|
|
716
|
+
* rule, that apostrophe opens a literal that runs to the end of the line and
|
|
717
|
+
* takes the rest of the statement with it.
|
|
718
|
+
*/
|
|
719
|
+
const ATTRIBUTE_LETTERS = wordSet("l t s i k n d o");
|
|
720
|
+
function acceptOperandWord(input, word, vocabulary) {
|
|
721
|
+
var _a;
|
|
722
|
+
const lower = word.toLowerCase();
|
|
723
|
+
if (input.next == 39 /* Ch.Apostrophe */ &&
|
|
724
|
+
ATTRIBUTE_LETTERS.has(lower) &&
|
|
725
|
+
isWordStart(input.peek(1))) {
|
|
726
|
+
input.advance();
|
|
727
|
+
scanWord(input);
|
|
728
|
+
return input.acceptToken(Identifier);
|
|
729
|
+
}
|
|
730
|
+
if (vocabulary.sizeKeywords.has(lower))
|
|
731
|
+
return input.acceptToken(SizeKeyword);
|
|
732
|
+
if (((_a = vocabulary.lineEndKeywords) === null || _a === void 0 ? void 0 : _a.has(lower)) && endsTheStatement(input)) {
|
|
733
|
+
return input.acceptToken(SizeKeyword);
|
|
734
|
+
}
|
|
735
|
+
let register = strip(lower);
|
|
736
|
+
if (vocabulary.registerSuffix)
|
|
737
|
+
register = register.replace(vocabulary.registerSuffix, "");
|
|
738
|
+
if (!vocabulary.dollarRegisters && vocabulary.registers.has(register)) {
|
|
739
|
+
return input.acceptToken(Register);
|
|
740
|
+
}
|
|
741
|
+
if (vocabulary.operandDirectives.has(lower))
|
|
742
|
+
return input.acceptToken(OperandDirective);
|
|
743
|
+
input.acceptToken(Identifier);
|
|
744
|
+
}
|
|
745
|
+
/** A register prefix is decoration; `%eax`, `$t0` and `eax` are one register. */
|
|
746
|
+
function strip(word) {
|
|
747
|
+
const first = word.charCodeAt(0);
|
|
748
|
+
return first == 37 /* Ch.Percent */ || first == 36 /* Ch.Dollar */ ? word.slice(1) : word;
|
|
749
|
+
}
|
|
750
|
+
function definitionFollows(input, vocabulary) {
|
|
751
|
+
let offset = skipSpace(input, 0);
|
|
752
|
+
// `message_len = $ - message` declares as surely as `equ` does.
|
|
753
|
+
if (input.peek(offset) == 61 /* Ch.Equals */)
|
|
754
|
+
return input.peek(offset + 1) != 61 /* Ch.Equals */;
|
|
755
|
+
const word = peekWord(input, offset);
|
|
756
|
+
if (!vocabulary.definitionDirectives.has(word.toLowerCase()))
|
|
757
|
+
return false;
|
|
758
|
+
if (!vocabulary.qualifiesAnOperand.has(word.toLowerCase()))
|
|
759
|
+
return true;
|
|
760
|
+
// `mov byte ptr [x], 1` in MASM and `mov byte [x], 1` in NASM both qualify
|
|
761
|
+
// an operand, so the word in front is a mnemonic.
|
|
762
|
+
offset = skipSpace(input, offset + word.length);
|
|
763
|
+
if (input.peek(offset) == 91 /* Ch.BracketL */)
|
|
764
|
+
return false;
|
|
765
|
+
return peekWord(input, offset).toLowerCase() != "ptr";
|
|
766
|
+
}
|
|
767
|
+
/** Whether only space and a comment stand between the cursor and the line's end. */
|
|
768
|
+
function endsTheStatement(input) {
|
|
769
|
+
const offset = skipSpace(input, 0);
|
|
770
|
+
const opensABlockComment = input.peek(offset) == 47 /* Ch.Slash */ && input.peek(offset + 1) == 42 /* Ch.Star */;
|
|
771
|
+
return endsTheLine(input.peek(offset)) || opensAComment(input, offset) || opensABlockComment;
|
|
772
|
+
}
|
|
773
|
+
function skipSpace(input, offset) {
|
|
774
|
+
while (isSpace(input.peek(offset)))
|
|
775
|
+
offset++;
|
|
776
|
+
return offset;
|
|
777
|
+
}
|
|
778
|
+
function peekWord(input, offset) {
|
|
779
|
+
if (!isWordStart(input.peek(offset)))
|
|
780
|
+
return "";
|
|
781
|
+
let word = "";
|
|
782
|
+
while (isWordChar(input.peek(offset))) {
|
|
783
|
+
word += String.fromCharCode(input.peek(offset));
|
|
784
|
+
offset++;
|
|
785
|
+
}
|
|
786
|
+
return word;
|
|
787
|
+
}
|
|
788
|
+
// bass and armips brace a run of lines, `scope AA {` and `if cond {`, where
|
|
789
|
+
// A backtick opens a quote, and what closes it depends on who is reading. An
|
|
790
|
+
// assembler closes it with another backtick and never crosses a line. m4
|
|
791
|
+
// closes it with an apostrophe, counts the pairs on the way, and thinks
|
|
792
|
+
// nothing of running over twenty lines: `ifdef(`OPERATION_addlsh2_n',`` opens a
|
|
793
|
+
// block of assembly that ends in `')` further down, and GNU MP's `.asm` files
|
|
794
|
+
// are written that way from top to bottom.
|
|
795
|
+
//
|
|
796
|
+
// So the quote is read as m4's when an apostrophe closes it and as the
|
|
797
|
+
// assembler's otherwise, and an unbalanced one ends at its line rather than
|
|
798
|
+
// taking the rest of the file with it.
|
|
799
|
+
const rawString = new lr.ExternalTokenizer((input) => {
|
|
800
|
+
if (input.next != 96 /* Ch.Backtick */)
|
|
801
|
+
return;
|
|
802
|
+
const end = quoteEnd(input);
|
|
803
|
+
for (let i = 0; i < end; i++)
|
|
804
|
+
input.advance();
|
|
805
|
+
input.acceptToken(RawString);
|
|
806
|
+
});
|
|
807
|
+
/**
|
|
808
|
+
How far a quote may be looked ahead for its close. The whole file would be
|
|
809
|
+
the honest answer and a quadratic one, since a file that leaves a backtick
|
|
810
|
+
unbalanced usually leaves several.
|
|
811
|
+
*/
|
|
812
|
+
const quoteReach = 65536;
|
|
813
|
+
/**
|
|
814
|
+
How far the quote opening at the cursor runs. m4's balanced apostrophe wins
|
|
815
|
+
where there is one, the assembler's closing backtick on the same line comes
|
|
816
|
+
next, and a quote that neither closes ends where its line does.
|
|
817
|
+
*/
|
|
818
|
+
function quoteEnd(input) {
|
|
819
|
+
let depth = 0;
|
|
820
|
+
let lineEnd = -1;
|
|
821
|
+
let sameLineBacktick = -1;
|
|
822
|
+
for (let ahead = 0;; ahead++) {
|
|
823
|
+
const ch = input.peek(ahead);
|
|
824
|
+
if (ch == -1 /* Ch.EOF */ || ahead > quoteReach) {
|
|
825
|
+
return sameLineBacktick > 0 ? sameLineBacktick : lineEnd < 0 ? ahead : lineEnd;
|
|
826
|
+
}
|
|
827
|
+
if (lineEnd < 0 && endsTheLine(ch))
|
|
828
|
+
lineEnd = ahead;
|
|
829
|
+
if (ch == 92 /* Ch.Backslash */ && input.peek(ahead + 1) != -1 /* Ch.EOF */) {
|
|
830
|
+
ahead++;
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
if (ch == 96 /* Ch.Backtick */) {
|
|
834
|
+
depth++;
|
|
835
|
+
if (depth == 2 && lineEnd < 0 && sameLineBacktick < 0)
|
|
836
|
+
sameLineBacktick = ahead + 1;
|
|
837
|
+
}
|
|
838
|
+
else if (ch == 39 /* Ch.Apostrophe */ && !--depth) {
|
|
839
|
+
return ahead + 1;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
// every other assembler's brace holds operands: a register list to ARM, a mask
|
|
844
|
+
// to AVX. The two are the same character and only what follows it says which.
|
|
845
|
+
// A brace that ends its own line opens the block, because a list has its
|
|
846
|
+
// contents behind it and a block has them underneath.
|
|
847
|
+
const braces = new lr.ExternalTokenizer((input, stack) => {
|
|
848
|
+
if (input.next != 123 /* Ch.BraceL */)
|
|
849
|
+
return;
|
|
850
|
+
input.advance();
|
|
851
|
+
input.acceptToken(opensABlock(input) ? BlockOpen : BraceOpen);
|
|
852
|
+
});
|
|
853
|
+
function opensABlock(input) {
|
|
854
|
+
let ahead = 0;
|
|
855
|
+
while (input.peek(ahead) == 32 /* Ch.Space */ || input.peek(ahead) == 9 /* Ch.Tab */)
|
|
856
|
+
ahead++;
|
|
857
|
+
if (opensAComment(input, ahead)) {
|
|
858
|
+
while (!endsTheLine(input.peek(ahead)))
|
|
859
|
+
ahead++;
|
|
860
|
+
}
|
|
861
|
+
return endsTheLine(input.peek(ahead));
|
|
862
|
+
}
|
|
863
|
+
function endsTheLine(ch) {
|
|
864
|
+
return ch == 10 /* Ch.Newline */ || ch == 13 /* Ch.Return */ || ch == -1 /* Ch.EOF */;
|
|
865
|
+
}
|
|
866
|
+
function opensAComment(input, at) {
|
|
867
|
+
const ch = input.peek(at);
|
|
868
|
+
if (ch == 59 /* Ch.Semicolon */)
|
|
869
|
+
return true;
|
|
870
|
+
return ch == 47 /* Ch.Slash */ && input.peek(at + 1) == 47 /* Ch.Slash */;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
874
|
+
const parser = lr.LRParser.deserialize({
|
|
875
|
+
version: 14,
|
|
876
|
+
states: "(QOfQ_OOOOQ^'#Db'#DbO%lQ_O'#DiO'dQ_O'#DTO'kQ_O'#DWO'rQ_O'#DYOOQ^'#Dk'#DkOOQ^'#Dc'#DcO)dQ_O'#CyO)}Q_O'#DdO*hQ]O'#CxO*hQ]O'#CxO+PQ_O'#CwO+^Q_O'#D[O+kQ_O'#D]O+xQ_O'#D^OOQ['#D_'#D_OOQ['#Dj'#DjO,VQXO'#D`OOQW'#Di'#DiO,[Q]O'#DiQ,gQXO'#DaOfQ_OOOOQ^-E7`-E7`OOQW,5:T,5:TO,lQ]O,5:TOOQ^,59o,59oO,wQXO,59oOOQ^,59r,59rO,|QXO,59rOOQ^,59t,59tO-RQXO,59tOOQ^-E7a-E7aOOQ[,5:O,5:OOOQ[-E7b-E7bO-WQ]O,59dOOQ[,59c,59cOOQ[,59v,59vOOQ[,59w,59wOOQ[,59x,59xO-oQ_O,59zO-yQXO,59zOOQ^,59{,59{OOQ^-E7_-E7_OOQW1G/o1G/oOOQ^1G/Z1G/ZOOQ^1G/^1G/^OOQ^1G/`1G/`OOQW,5:P,5:POOQW1G/f1G/fO.OQ_O1G/fOOQW-E7c-E7cOOQW7+%Q7+%QP-rQ_O'#De",
|
|
877
|
+
stateData: ".a~O![OSfOSgOShOSaOSiOS~OPUOQbORTOS[OT]OU^OVPOWUOXUOYUOZUO[UO]UO^UO_UO`UOb_OcUOdUOeUOnUOoUOpUOqUOrUOsUOtUOvROySO}XO!Y!]P!`!]P~OPUOQbORTOS[OT]OU^OVPOWUOXUOYUOZUO[UO]UO^UO_UO`UOb_OcUOdUOeUOnUOoUOpUOqUOrUOsUOtUOvROySO}XO~O!Y!]X!`!]X~P#lOPUORTOWUOXUOYUOZUO[UO]UO^UO_UO`UOcUOdUOeUOnUOoUOpUOqUOrUOsUOtUOvROySO}XO~OujO~P%vOxlO~P%vO{nO~P%vOPUORTOWUOXUOYUOZUO[UO]UO^UO_UO`UOcUOdUOeUOnUOoUOpUOqUOrUOsUOtUOvROySO~OQmX}mX!YmX!`mXumXxmX{mX~P'yOQ!WX}!WX!Y!WX!`!WXu!WXx!WX{!WX~P'yO}XOQlX!YlX!`lXulXxlX{lX~OQkX!YkX!`kX~P%vOQ!OX!Y!OX!`!OX~P%vOQ!PX!Y!PX!`!PX~P%vOQ!QX!Y!QX!`!QX~P%vO!`xO~OQbO!Y!]X!`!]X~O!`zO~OQbO!Y!]a!`!]a~Ou}O~Ox!OO~O{!PO~O}XOQla!Yla!`laulaxla{la~O{!RO!`!]P~P#lO!`!SO~O{!UO!`!]P~P#lOghqpqs~",
|
|
878
|
+
goto: "&b!`PPPPPPPPPPPPPPPPPPPPPPPPPPP!a!i#WPPPPPPPPP#jPP#jP#jP!a!a!a!a#{$Z$a$k%O%fPPP%l%u&P]aOQfx!S!V[`OQfx!S!VQkRQmSQoTQt[Qu]Qv^Rw_jZOQRST[]^_fx!S!VRqXoUOQRSTWX[]^_fx!S!VYcOfx!S!VShQdR|iQfOR{fYQOfx!S!VRgQlWOQRSTX[]^_fx!S!VRpWjYOQRST[]^_fx!S!VSrYsRsZQybR!TySeOfV!Qx!S!VYdOfx!S!VRiQoVOQRSTWX[]^_fx!S!V",
|
|
879
|
+
nodeNames: "⚠ RawString { { Mnemonic DirectiveName MacroName Label Register SizeKeyword Identifier Number ImmediatePrefix Here Star Slash Percent StatementComment AssignOp Equals DirectiveName MacroParameter Continuation BlockComment LineComment EndMarker Program Instruction Operands Operand String Char ArithOp CompareOp : Bang ? ) ( Parens ] [ Brackets } Braces , Directive Preprocessor Assignment Data Block",
|
|
880
|
+
maxTerm: 62,
|
|
881
|
+
nodeProps: [
|
|
882
|
+
["openedBy", 37,"(",40,"["],
|
|
883
|
+
["closedBy", 38,")",41,"]"]
|
|
884
|
+
],
|
|
885
|
+
skippedNodes: [0,17,22,23,24,25],
|
|
886
|
+
repeatNodeCount: 5,
|
|
887
|
+
tokenData: ".T~RqXY#YYZ#kZ[#Y[]#Y]^#pjk#xpq#Yqr$irs$vst&Pvw'mwx'rxy({yz)Q{|'m|})V}!O'm!P!Q)[![!]*l!]!^'U!^!_*q!_!`*|!`!a+S!a!b+_!b!c+d!}#O,Y#O#P,_#P#Q,{#Q#R'm#W#X-Q#p#q'm#q#r.O#r#s'm~#_S![~XY#YZ[#Y[]#Ypq#Y~#pO!`~~#uP!`~YZ#k~#}Ri~O;'S#x;'S;=`$W;=`O#x~$]Si~O;'S#x;'S;=`$W;=`<%l#x<%lO#x~$nPs~!_!`$q~$vOq~~${Wn~OY$vZr$vrs%es#O$v#O#P%j#P;'S$v;'S;=`%y<%lO$v~%jOn~~%mSOY$vZ;'S$v;'S;=`%y<%lO$v~%|P;=`<%l$v~&S`OY'UZ]'U^r'Ust'Uvw'Uy{'U|}'U!O!Q'U!]!c'U!}#O'U#P#R'U#S#T'U#o#r'U#s;'S'U;'S;=`'g<%lO'U~'ZSh~OY'UZ;'S'U;'S;=`'g<%lO'U~'jP;=`<%l'U~'rOp~~'wWo~OY'rZw'rwx(ax#O'r#O#P(f#P;'S'r;'S;=`(u<%lO'r~(fOo~~(iSOY'rZ;'S'r;'S;=`(u<%lO'r~(xP;=`<%l'r~)QOv~~)VOu~~)[O}~~)_Qz{)e!P!Q'U~)hTOz)ez{)w{;'S)e;'S;=`*f<%lO)e~)zVOz)ez{)w{!P)e!P!Q*a!Q;'S)e;'S;=`*f<%lO)e~*fOg~~*iP;=`<%l)e~*qOr~~*vQq~!^!_$q!_!`$q~+PP!_!`$q~+XQq~!_!`$q!`!a$q~+dOt~~+gZOY'UZ]'U^t'Uu!O'U!P!Q'U![!a'U!}#R'U#S#T'U#o;'S'U;'S;=`'g<%lO'U~,_Oy~~,bSXY,_YZ,n]^,spq,_~,sOf~~,xPf~YZ,n~-QOx~~-TP#b#c-W~-ZP#`#a-^~-cXh~OY'UZ]'U^!Q'U![!c'U!}#R'U#S#T'U#o;'S'U;'S;=`'g<%lO'U~.TO{~",
|
|
888
|
+
tokenizers: [0, rawString, braces, words],
|
|
889
|
+
topRules: {"Program":[0,26]},
|
|
890
|
+
dialects: {x86: 0, arm64: 0, mips: 0},
|
|
891
|
+
tokenPrec: 608
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
const assemblyLanguage = language.LRLanguage.define({
|
|
895
|
+
name: "assembly",
|
|
896
|
+
parser: parser.configure({
|
|
897
|
+
props: [
|
|
898
|
+
language.foldNodeProp.add({
|
|
899
|
+
Parens: language.foldInside,
|
|
900
|
+
Brackets: language.foldInside,
|
|
901
|
+
Braces: language.foldInside,
|
|
902
|
+
BlockComment: language.foldInside
|
|
903
|
+
}),
|
|
904
|
+
highlight.styleTags({
|
|
905
|
+
Mnemonic: highlight.tags.keyword,
|
|
906
|
+
DirectiveName: highlight.tags.meta,
|
|
907
|
+
MacroName: highlight.tags.macroName,
|
|
908
|
+
Label: highlight.tags.definition(highlight.tags.labelName),
|
|
909
|
+
Register: highlight.tags.special(highlight.tags.variableName),
|
|
910
|
+
SizeKeyword: highlight.tags.typeName,
|
|
911
|
+
Identifier: highlight.tags.variableName,
|
|
912
|
+
Number: highlight.tags.number,
|
|
913
|
+
"String RawString": highlight.tags.string,
|
|
914
|
+
Char: highlight.tags.character,
|
|
915
|
+
Here: highlight.tags.atom,
|
|
916
|
+
MacroParameter: highlight.tags.local(highlight.tags.variableName),
|
|
917
|
+
ImmediatePrefix: highlight.tags.operator,
|
|
918
|
+
// The Ctrl-Z a DOS assembler wrote at the end of a file, and
|
|
919
|
+
// whatever somebody left behind it.
|
|
920
|
+
EndMarker: highlight.tags.comment,
|
|
921
|
+
"LineComment StatementComment": highlight.tags.lineComment,
|
|
922
|
+
BlockComment: highlight.tags.blockComment,
|
|
923
|
+
Continuation: highlight.tags.escape,
|
|
924
|
+
"ArithOp Star Slash Percent": highlight.tags.arithmeticOperator,
|
|
925
|
+
CompareOp: highlight.tags.compareOperator,
|
|
926
|
+
AssignOp: highlight.tags.definitionOperator,
|
|
927
|
+
Equals: highlight.tags.operator,
|
|
928
|
+
",": highlight.tags.separator,
|
|
929
|
+
": ? Bang": highlight.tags.punctuation,
|
|
930
|
+
"( )": highlight.tags.paren,
|
|
931
|
+
"[ ]": highlight.tags.squareBracket,
|
|
932
|
+
"{ }": highlight.tags.brace
|
|
933
|
+
})
|
|
934
|
+
]
|
|
935
|
+
}),
|
|
936
|
+
languageData: {
|
|
937
|
+
// Every assembler in the corpus this was measured against takes a
|
|
938
|
+
// semicolon, and the ones that take something else take it as well.
|
|
939
|
+
commentTokens: { line: ";" },
|
|
940
|
+
closeBrackets: { brackets: ["(", "[", "{", '"', "'"] }
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
function proposals(from, type) {
|
|
944
|
+
return [...from].map((label) => ({ label, type }));
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
Autocompletion for the assembler's own vocabulary: the directives that
|
|
948
|
+
reserve storage and open sections, the GNU assembler's dot-prefixed set, the
|
|
949
|
+
widths an Intel-syntax operand can carry, and the registers of every
|
|
950
|
+
instruction set the grammar knows. Mnemonics are not proposed, because a
|
|
951
|
+
mnemonic belongs to a processor rather than to assembly.
|
|
952
|
+
*/
|
|
953
|
+
const assemblyCompletion = autocomplete.completeFromList([
|
|
954
|
+
...proposals(DIRECTIVES, "keyword"),
|
|
955
|
+
...proposals(DOT_DIRECTIVES, "keyword"),
|
|
956
|
+
...proposals(SIZE_KEYWORDS, "type"),
|
|
957
|
+
...proposals(REGISTERS, "variable")
|
|
958
|
+
]);
|
|
959
|
+
/**
|
|
960
|
+
Assembly language support, with completion for directives, operand widths
|
|
961
|
+
and registers.
|
|
962
|
+
*/
|
|
963
|
+
function assembly() {
|
|
964
|
+
return new language.LanguageSupport(assemblyLanguage, [
|
|
965
|
+
assemblyLanguage.data.of({ autocomplete: assemblyCompletion })
|
|
966
|
+
]);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
const DATA = /^(d[bwdqtoyz]|res[bwdqtoyz]|times|incbin|istruc|\.(byte|word|long|quad|ascii|asciz|asciiz|string|zero|space|skip|fill|int|short|hword|octa|float|double|single|half|dword|xword|[248]byte))$/i;
|
|
970
|
+
const CONSTANT = /^(equ|\.equ|\.set|\.equiv|set)$/i;
|
|
971
|
+
const ALIAS = /^\.req$/i;
|
|
972
|
+
const DEFINE = /^%[ix]?(define|xdefine|assign|deftok|defstr|strlen|strcat|substr)$|^%i?xdefine$|^#define$|^\.(equ|set|equiv|eqv)$/i;
|
|
973
|
+
const MACRO_OPEN = /^(%i?macro|\.macro|macro)$/i;
|
|
974
|
+
const MACRO_CLOSE = /^(%endmacro|\.endm|\.endmacro|\.end_macro|endm|endmacro)$/i;
|
|
975
|
+
function text(doc, node) {
|
|
976
|
+
return doc.sliceString(node.from, node.to);
|
|
977
|
+
}
|
|
978
|
+
/** The name of a statement node: its mnemonic, directive or preprocessor word. */
|
|
979
|
+
function statementWord(doc, node) {
|
|
980
|
+
const first = node.firstChild;
|
|
981
|
+
return first ? text(doc, first).toLowerCase() : "";
|
|
982
|
+
}
|
|
983
|
+
/** The first word in a statement's operands, such as `SIZE` in `%define SIZE 8`. */
|
|
984
|
+
function firstOperandWord(doc, node) {
|
|
985
|
+
var _a;
|
|
986
|
+
const operand = (_a = node.getChild("Operands")) === null || _a === void 0 ? void 0 : _a.getChild("Operand");
|
|
987
|
+
const first = operand === null || operand === void 0 ? void 0 : operand.firstChild;
|
|
988
|
+
if (!first)
|
|
989
|
+
return null;
|
|
990
|
+
return first.name == "Identifier" || first.name == "Register" || first.name == "Mnemonic"
|
|
991
|
+
? first
|
|
992
|
+
: null;
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Whether a label belongs to the function around it: `.loop`, and `%%end`
|
|
996
|
+
* inside a NASM macro. To gas, `1:` belongs to it as well.
|
|
997
|
+
*/
|
|
998
|
+
function isLocalLabel(name, style = "nasm") {
|
|
999
|
+
if (style == "gas" && /^\d+$/.test(name))
|
|
1000
|
+
return true;
|
|
1001
|
+
return (name.startsWith(".") && !name.startsWith("..@")) || name.startsWith("%%");
|
|
1002
|
+
}
|
|
1003
|
+
const caches = {
|
|
1004
|
+
nasm: new WeakMap(),
|
|
1005
|
+
gas: new WeakMap()
|
|
1006
|
+
};
|
|
1007
|
+
/**
|
|
1008
|
+
* The names declared in the file. A label, an `equ` constant, a `%define`
|
|
1009
|
+
* and a macro are global. A local label such as `.loop` belongs to the global
|
|
1010
|
+
* label before it, as NASM reads it. A `%%end` label belongs to the macro it
|
|
1011
|
+
* is written in. A field such as `.size` inside `struc item` is offered as
|
|
1012
|
+
* `item.size`, and the structure also gives `item_size`.
|
|
1013
|
+
*/
|
|
1014
|
+
function declaredNames(tree, doc, style = "nasm") {
|
|
1015
|
+
const cache = caches[style];
|
|
1016
|
+
const cached = cache.get(tree);
|
|
1017
|
+
if (cached)
|
|
1018
|
+
return cached;
|
|
1019
|
+
const names = [];
|
|
1020
|
+
const end = doc.length;
|
|
1021
|
+
let ownerFrom = 0;
|
|
1022
|
+
let pendingLocals = [];
|
|
1023
|
+
let macro = null;
|
|
1024
|
+
let struc = null;
|
|
1025
|
+
const closeOwner = (at) => {
|
|
1026
|
+
for (const local of pendingLocals)
|
|
1027
|
+
local.to = at;
|
|
1028
|
+
pendingLocals = [];
|
|
1029
|
+
};
|
|
1030
|
+
for (let node = tree.topNode.firstChild; node; node = node.nextSibling) {
|
|
1031
|
+
if (node.name == "Label") {
|
|
1032
|
+
const name = text(doc, node).replace(/:+$/, "");
|
|
1033
|
+
const next = node.nextSibling;
|
|
1034
|
+
const nextWord = next && sameLine(doc, node, next) ? statementWord(doc, next) : "";
|
|
1035
|
+
const type = CONSTANT.test(nextWord) || (next === null || next === void 0 ? void 0 : next.name) == "Assignment"
|
|
1036
|
+
? "constant"
|
|
1037
|
+
: DATA.test(nextWord) || ALIAS.test(nextWord) ? "variable" : null;
|
|
1038
|
+
if (style == "gas") {
|
|
1039
|
+
// gas labels are global, and a numeric one is kept as its digits for
|
|
1040
|
+
// namesInScope to turn into `1b` or `1f`.
|
|
1041
|
+
names.push({ name, type: type !== null && type !== void 0 ? type : (/^\d+$/.test(name) ? "label" : "function"), at: node.from, from: 0, to: end });
|
|
1042
|
+
}
|
|
1043
|
+
else if (struc && name.startsWith(".")) {
|
|
1044
|
+
names.push({ name: struc + name, type: "constant", at: node.from, from: 0, to: end });
|
|
1045
|
+
}
|
|
1046
|
+
else if (name.startsWith("%%")) {
|
|
1047
|
+
if (macro) {
|
|
1048
|
+
const label = { name, type: "label", at: node.from, from: macro.from, to: end };
|
|
1049
|
+
macro.labels.push(label);
|
|
1050
|
+
names.push(label);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
else if (isLocalLabel(name)) {
|
|
1054
|
+
const local = { name, type: type !== null && type !== void 0 ? type : "label", at: node.from, from: ownerFrom, to: end };
|
|
1055
|
+
pendingLocals.push(local);
|
|
1056
|
+
names.push(local);
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
if (!type) {
|
|
1060
|
+
closeOwner(node.from);
|
|
1061
|
+
ownerFrom = node.from;
|
|
1062
|
+
}
|
|
1063
|
+
names.push({ name, type: type !== null && type !== void 0 ? type : "function", at: node.from, from: 0, to: end });
|
|
1064
|
+
}
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
if (node.name != "Preprocessor" && node.name != "Directive" && node.name != "Instruction")
|
|
1068
|
+
continue;
|
|
1069
|
+
const word = statementWord(doc, node);
|
|
1070
|
+
const named = firstOperandWord(doc, node);
|
|
1071
|
+
if (DEFINE.test(word) && named) {
|
|
1072
|
+
names.push({ name: text(doc, named), type: "constant", at: named.from, from: 0, to: end });
|
|
1073
|
+
}
|
|
1074
|
+
else if (MACRO_OPEN.test(word) && named) {
|
|
1075
|
+
names.push({ name: text(doc, named), type: "macro", at: named.from, from: 0, to: end });
|
|
1076
|
+
macro = { from: node.from, labels: [], parameters: [] };
|
|
1077
|
+
if (word == ".macro") {
|
|
1078
|
+
for (const parameter of macroParameters(doc, node, named)) {
|
|
1079
|
+
const name = { name: parameter, type: "variable", at: node.from, from: node.from, to: end, macroParameter: true };
|
|
1080
|
+
macro.parameters.push(name);
|
|
1081
|
+
names.push(name);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
else if (MACRO_CLOSE.test(word) && macro) {
|
|
1086
|
+
for (const label of [...macro.labels, ...macro.parameters])
|
|
1087
|
+
label.to = node.to;
|
|
1088
|
+
macro = null;
|
|
1089
|
+
}
|
|
1090
|
+
else if (word == "struc" && named) {
|
|
1091
|
+
struc = text(doc, named);
|
|
1092
|
+
names.push({ name: struc, type: "type", at: named.from, from: 0, to: end });
|
|
1093
|
+
names.push({ name: struc + "_size", type: "constant", at: named.from, from: 0, to: end });
|
|
1094
|
+
}
|
|
1095
|
+
else if (word == "endstruc") {
|
|
1096
|
+
struc = null;
|
|
1097
|
+
}
|
|
1098
|
+
else if ((word == "extern" || word == "extrn" || word == ".extern") && node.getChild("Operands")) {
|
|
1099
|
+
for (const operand of node.getChild("Operands").getChildren("Operand")) {
|
|
1100
|
+
const first = operand.firstChild;
|
|
1101
|
+
if ((first === null || first === void 0 ? void 0 : first.name) == "Identifier") {
|
|
1102
|
+
names.push({ name: text(doc, first), type: "function", at: first.from, from: 0, to: end });
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
cache.set(tree, names);
|
|
1108
|
+
return names;
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* The parameters of a gas macro: `source`, `count` and `size` in
|
|
1112
|
+
* `.macro COPY source, count=1, size:req`. MARS writes them in brackets
|
|
1113
|
+
* behind the name, as `%from` and `%to` in `.macro copy(%from, %to)`.
|
|
1114
|
+
*/
|
|
1115
|
+
function macroParameters(doc, node, name) {
|
|
1116
|
+
var _a, _b, _c, _d, _e;
|
|
1117
|
+
const out = [];
|
|
1118
|
+
for (const operand of (_b = (_a = node.getChild("Operands")) === null || _a === void 0 ? void 0 : _a.getChildren("Operand")) !== null && _b !== void 0 ? _b : []) {
|
|
1119
|
+
for (let part = operand.firstChild; part; part = part.nextSibling) {
|
|
1120
|
+
if (part.name == "Parens") {
|
|
1121
|
+
for (const inner of (_d = (_c = part.getChild("Operands")) === null || _c === void 0 ? void 0 : _c.getChildren("Operand")) !== null && _d !== void 0 ? _d : []) {
|
|
1122
|
+
for (const parameter of inner.getChildren("MacroParameter"))
|
|
1123
|
+
out.push(text(doc, parameter));
|
|
1124
|
+
}
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
if (part.from == name.from)
|
|
1128
|
+
continue;
|
|
1129
|
+
const before = (_e = part.prevSibling) === null || _e === void 0 ? void 0 : _e.name;
|
|
1130
|
+
if (before == "Equals" || before == ":")
|
|
1131
|
+
continue;
|
|
1132
|
+
if (part.name == "Identifier" || part.name == "Register" || part.name == "SizeKeyword") {
|
|
1133
|
+
out.push(text(doc, part));
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
return out;
|
|
1138
|
+
}
|
|
1139
|
+
function sameLine(doc, a, b) {
|
|
1140
|
+
return doc.lineAt(a.from).number == doc.lineAt(b.from).number;
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* The names that can be used at `pos`, with the newest declaration of each.
|
|
1144
|
+
* In the gas style, a numeric label `1:` is offered as `1b` below it and as
|
|
1145
|
+
* `1f` above it.
|
|
1146
|
+
*/
|
|
1147
|
+
function namesInScope(tree, doc, pos, style = "nasm") {
|
|
1148
|
+
const seen = new Map();
|
|
1149
|
+
for (const declared of declaredNames(tree, doc, style)) {
|
|
1150
|
+
if (pos < declared.from || pos > declared.to)
|
|
1151
|
+
continue;
|
|
1152
|
+
let name = declared;
|
|
1153
|
+
if (style == "gas" && /^\d+$/.test(declared.name)) {
|
|
1154
|
+
if (doc.lineAt(declared.at).number == doc.lineAt(pos).number)
|
|
1155
|
+
continue;
|
|
1156
|
+
name = Object.assign(Object.assign({}, declared), { name: declared.name + (declared.at < pos ? "b" : "f") });
|
|
1157
|
+
}
|
|
1158
|
+
if (!seen.has(name.name))
|
|
1159
|
+
seen.set(name.name, name);
|
|
1160
|
+
}
|
|
1161
|
+
return [...seen.values()];
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const DONT_COMPLETE_IN = new Set([
|
|
1165
|
+
"LineComment",
|
|
1166
|
+
"BlockComment",
|
|
1167
|
+
"StatementComment",
|
|
1168
|
+
"String",
|
|
1169
|
+
"Char",
|
|
1170
|
+
"RawString",
|
|
1171
|
+
"EndMarker"
|
|
1172
|
+
]);
|
|
1173
|
+
const WORD = /[%$.\w?@]*$/;
|
|
1174
|
+
// The text in front of the word on its line, when the word starts a statement:
|
|
1175
|
+
// nothing, or labels such as `main:` and `.loop:`.
|
|
1176
|
+
const STATEMENT_START = /^\s*((%%|\.\.@|\.)?[\w$?@.]+::?\s*)*$/;
|
|
1177
|
+
// A statement whose next word is a new name, which completion can't know.
|
|
1178
|
+
const NAMES_SOMETHING = /^\s*(%[ix]?(define|xdefine|assign|macro|imacro|undef|deftok|defstr|strlen)|struc|\.macro|\.set|\.equ|\.eqv)\s+$/i;
|
|
1179
|
+
function options(words, type) {
|
|
1180
|
+
return [...words].map((label) => ({ label, type }));
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* A completion source for one instruction set. Where a statement starts, it
|
|
1184
|
+
* offers the mnemonics, directives, macros declared in the file and snippets.
|
|
1185
|
+
* After `%` it offers the preprocessor words. In an operand it offers the
|
|
1186
|
+
* registers, size keywords, and the labels and constants in scope. Local labels
|
|
1187
|
+
* such as `.loop` are offered only after the global label they belong to.
|
|
1188
|
+
* Nothing is offered in comments and strings, or where a new name is being
|
|
1189
|
+
* written.
|
|
1190
|
+
*/
|
|
1191
|
+
function assemblyCompletionSource(vocabulary) {
|
|
1192
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1193
|
+
const statement = [
|
|
1194
|
+
...options(vocabulary.mnemonics, "keyword"),
|
|
1195
|
+
...options(vocabulary.directives, "keyword"),
|
|
1196
|
+
...((_a = vocabulary.snippets) !== null && _a !== void 0 ? _a : [])
|
|
1197
|
+
];
|
|
1198
|
+
const mnemonics = options(vocabulary.mnemonics, "keyword");
|
|
1199
|
+
const preprocessor = options((_b = vocabulary.preprocessor) !== null && _b !== void 0 ? _b : [], "keyword");
|
|
1200
|
+
const operand = [
|
|
1201
|
+
...options(vocabulary.registers, "variable"),
|
|
1202
|
+
...options((_c = vocabulary.sizeKeywords) !== null && _c !== void 0 ? _c : [], "type"),
|
|
1203
|
+
...options((_d = vocabulary.operandDirectives) !== null && _d !== void 0 ? _d : [], "keyword")
|
|
1204
|
+
];
|
|
1205
|
+
const sections = options((_e = vocabulary.sections) !== null && _e !== void 0 ? _e : [], "namespace");
|
|
1206
|
+
const relocations = options((_f = vocabulary.relocations) !== null && _f !== void 0 ? _f : [], "type");
|
|
1207
|
+
const style = (_g = vocabulary.labels) !== null && _g !== void 0 ? _g : "nasm";
|
|
1208
|
+
const prefixes = [...((_h = vocabulary.prefixes) !== null && _h !== void 0 ? _h : [])];
|
|
1209
|
+
const afterPrefix = prefixes.length
|
|
1210
|
+
? new RegExp(`^\\s*((%%|\\.)?[\\w$?@.]+:\\s*)*(${prefixes.join("|")})\\s+$`, "i")
|
|
1211
|
+
: null;
|
|
1212
|
+
return (context) => {
|
|
1213
|
+
const { state, pos } = context;
|
|
1214
|
+
const tree = language.syntaxTree(state);
|
|
1215
|
+
const inner = tree.resolveInner(pos, -1);
|
|
1216
|
+
for (let node = inner; node; node = node.parent) {
|
|
1217
|
+
if (DONT_COMPLETE_IN.has(node.name))
|
|
1218
|
+
return null;
|
|
1219
|
+
}
|
|
1220
|
+
const word = context.matchBefore(WORD);
|
|
1221
|
+
if (!word || (word.from == word.to && !context.explicit))
|
|
1222
|
+
return null;
|
|
1223
|
+
const line = state.doc.lineAt(pos);
|
|
1224
|
+
const before = line.text.slice(0, word.from - line.from);
|
|
1225
|
+
if (NAMES_SOMETHING.test(before))
|
|
1226
|
+
return null;
|
|
1227
|
+
const result = (list) => ({
|
|
1228
|
+
from: word.from,
|
|
1229
|
+
options: list,
|
|
1230
|
+
validFor: /^[%$.\w?@]*$/
|
|
1231
|
+
});
|
|
1232
|
+
if (/(^|\s)\.?(section|segment)\s+$/i.test(before))
|
|
1233
|
+
return result(sections);
|
|
1234
|
+
if (afterPrefix === null || afterPrefix === void 0 ? void 0 : afterPrefix.test(before))
|
|
1235
|
+
return result(mnemonics);
|
|
1236
|
+
if (relocations.length && /(^|[\s,#]):$/.test(before))
|
|
1237
|
+
return result(relocations);
|
|
1238
|
+
const names = namesInScope(tree, state.doc, pos, style);
|
|
1239
|
+
const parameters = names
|
|
1240
|
+
.filter((name) => name.macroParameter)
|
|
1241
|
+
.map((name) => ({ label: name.name, type: name.type }));
|
|
1242
|
+
// A gas macro writes its parameters as `\name`.
|
|
1243
|
+
if (before.endsWith("\\"))
|
|
1244
|
+
return result(parameters);
|
|
1245
|
+
if (STATEMENT_START.test(before)) {
|
|
1246
|
+
if (word.text.startsWith("%") && !word.text.startsWith("%%"))
|
|
1247
|
+
return result(preprocessor);
|
|
1248
|
+
const macros = names
|
|
1249
|
+
.filter((name) => name.type == "macro")
|
|
1250
|
+
.map((name) => ({ label: name.name, type: "function" }));
|
|
1251
|
+
return result([...statement, ...macros]);
|
|
1252
|
+
}
|
|
1253
|
+
const declared = names
|
|
1254
|
+
.filter((name) => name.type != "macro" && !name.macroParameter)
|
|
1255
|
+
.map((name) => ({ label: name.name, type: name.type }));
|
|
1256
|
+
// MARS writes a macro's parameters as `%name`.
|
|
1257
|
+
if (word.text.startsWith("%") && parameters.some((name) => name.label.startsWith("%"))) {
|
|
1258
|
+
return result(parameters.filter((name) => name.label.startsWith("%")));
|
|
1259
|
+
}
|
|
1260
|
+
// An immediate such as `#SIZE` can only be a number or a constant.
|
|
1261
|
+
if (before.endsWith("#"))
|
|
1262
|
+
return result(declared.filter((name) => name.type == "constant"));
|
|
1263
|
+
return result([...operand, ...declared]);
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
const STATEMENTS = new Set(["Instruction", "Directive", "Preprocessor", "Assignment", "Data", "Label", "Block"]);
|
|
1268
|
+
function lineEndBefore(state, pos) {
|
|
1269
|
+
const line = state.doc.lineAt(pos);
|
|
1270
|
+
return line.number > 1 ? state.doc.line(line.number - 1).to : line.from;
|
|
1271
|
+
}
|
|
1272
|
+
function blockOf(options, word, side) {
|
|
1273
|
+
var _a;
|
|
1274
|
+
return (_a = options.blocks.find((block) => block[side].test(word))) !== null && _a !== void 0 ? _a : null;
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* Folding for a flat tree. Every statement is a child of the program, so a
|
|
1278
|
+
* block is found by walking along the statements. A block such as `%macro`
|
|
1279
|
+
* folds to its closing word. A global code label folds to the last statement
|
|
1280
|
+
* before the next global label or section directive, which is the body of the
|
|
1281
|
+
* function it names.
|
|
1282
|
+
*/
|
|
1283
|
+
function foldStatements(options) {
|
|
1284
|
+
return language.foldService.of((state, lineStart, lineEnd) => {
|
|
1285
|
+
const tree = language.syntaxTree(state);
|
|
1286
|
+
let first = tree.topNode.childAfter(lineStart);
|
|
1287
|
+
while (first && !STATEMENTS.has(first.name) && first.from < lineEnd)
|
|
1288
|
+
first = first.nextSibling;
|
|
1289
|
+
if (!first || first.from >= lineEnd)
|
|
1290
|
+
return null;
|
|
1291
|
+
const doc = state.doc;
|
|
1292
|
+
const word = statementWord(doc, first);
|
|
1293
|
+
const block = first.name == "Label" ? null : blockOf(options, word, "open");
|
|
1294
|
+
if (block) {
|
|
1295
|
+
let depth = 0;
|
|
1296
|
+
for (let node = first.nextSibling; node; node = node.nextSibling) {
|
|
1297
|
+
const next = statementWord(doc, node);
|
|
1298
|
+
if (block.open.test(next))
|
|
1299
|
+
depth++;
|
|
1300
|
+
else if (block.close.test(next) && depth-- == 0) {
|
|
1301
|
+
const to = lineEndBefore(state, node.from);
|
|
1302
|
+
return to > lineEnd ? { from: lineEnd, to } : null;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
return null;
|
|
1306
|
+
}
|
|
1307
|
+
if (first.name != "Label")
|
|
1308
|
+
return null;
|
|
1309
|
+
const name = doc.sliceString(first.from, first.to).replace(/:+$/, "");
|
|
1310
|
+
if (isLocalLabel(name, options.labels))
|
|
1311
|
+
return null;
|
|
1312
|
+
let last = null;
|
|
1313
|
+
for (let node = first.nextSibling; node; node = node.nextSibling) {
|
|
1314
|
+
if (!STATEMENTS.has(node.name))
|
|
1315
|
+
continue;
|
|
1316
|
+
if (node.name == "Label") {
|
|
1317
|
+
const other = doc.sliceString(node.from, node.to).replace(/:+$/, "");
|
|
1318
|
+
if (!isLocalLabel(other, options.labels))
|
|
1319
|
+
break;
|
|
1320
|
+
}
|
|
1321
|
+
else if (options.sectionBreaks.test(statementWord(doc, node))) {
|
|
1322
|
+
break;
|
|
1323
|
+
}
|
|
1324
|
+
last = node;
|
|
1325
|
+
}
|
|
1326
|
+
if (!last)
|
|
1327
|
+
return null;
|
|
1328
|
+
const to = doc.lineAt(last.to).to;
|
|
1329
|
+
return to > lineEnd ? { from: lineEnd, to } : null;
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
// A label alone on its line, such as `main:` or `.loop:`.
|
|
1333
|
+
const LABEL_LINE = /^\s*(%%|\.\.@|\.)?[\w$?@.]+::?\s*$/;
|
|
1334
|
+
const LABEL_START = /^\s*(%%|\.\.@|\.)?[\w$?@.]+::?/;
|
|
1335
|
+
/** A line without its comment, for reading its first word. */
|
|
1336
|
+
function code(line, comments) {
|
|
1337
|
+
let quote = "";
|
|
1338
|
+
for (let i = 0; i < line.length; i++) {
|
|
1339
|
+
const ch = line[i];
|
|
1340
|
+
if (quote) {
|
|
1341
|
+
if (ch == quote)
|
|
1342
|
+
quote = "";
|
|
1343
|
+
}
|
|
1344
|
+
else if (ch == '"' || ch == "'" || ch == "`") {
|
|
1345
|
+
quote = ch;
|
|
1346
|
+
}
|
|
1347
|
+
else if (comments.some((comment) => line.startsWith(comment, i))) {
|
|
1348
|
+
return line.slice(0, i).trimEnd();
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
return line.trimEnd();
|
|
1352
|
+
}
|
|
1353
|
+
function firstWord(line) {
|
|
1354
|
+
var _a, _b, _c;
|
|
1355
|
+
return (_c = (_b = (_a = /^\s*([%.\w$?@]+)/.exec(line.replace(LABEL_START, ""))) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : "";
|
|
1356
|
+
}
|
|
1357
|
+
function indentOf(state, text) {
|
|
1358
|
+
let column = 0;
|
|
1359
|
+
for (const ch of text) {
|
|
1360
|
+
if (ch == " ")
|
|
1361
|
+
column++;
|
|
1362
|
+
else if (ch == "\t")
|
|
1363
|
+
column += state.tabSize - (column % state.tabSize);
|
|
1364
|
+
else
|
|
1365
|
+
break;
|
|
1366
|
+
}
|
|
1367
|
+
return column;
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Indentation as assembly is written: labels and section directives at the
|
|
1371
|
+
* margin, and instructions one unit in. A line after a label on its own is
|
|
1372
|
+
* indented, and the body of a block such as `%macro` is indented one unit
|
|
1373
|
+
* further than its opening line. A label or a closing word such as
|
|
1374
|
+
* `%endmacro` lines up with the block's opening line.
|
|
1375
|
+
*/
|
|
1376
|
+
function indentStatements(options) {
|
|
1377
|
+
return language.indentService.of((context, pos) => {
|
|
1378
|
+
var _a;
|
|
1379
|
+
const { state } = context;
|
|
1380
|
+
const unit = language.getIndentUnit(state);
|
|
1381
|
+
const line = state.doc.lineAt(pos);
|
|
1382
|
+
const comments = (_a = options.lineComments) !== null && _a !== void 0 ? _a : [";"];
|
|
1383
|
+
const current = code(line.text.slice(pos - line.from), comments);
|
|
1384
|
+
const currentWord = firstWord(current);
|
|
1385
|
+
// The block that the line is in: its opening line's indent, or -1 at the top.
|
|
1386
|
+
let depth = 0;
|
|
1387
|
+
let blockIndent = -1;
|
|
1388
|
+
let previous = null;
|
|
1389
|
+
for (let number = line.number - 1; number >= 1; number--) {
|
|
1390
|
+
const text = code(state.doc.line(number).text, comments);
|
|
1391
|
+
if (!text.trim())
|
|
1392
|
+
continue;
|
|
1393
|
+
if (previous == null)
|
|
1394
|
+
previous = text;
|
|
1395
|
+
const word = firstWord(text);
|
|
1396
|
+
if (options.blocks.some((block) => block.close.test(word)))
|
|
1397
|
+
depth++;
|
|
1398
|
+
else if (options.blocks.some((block) => block.open.test(word)) && depth-- == 0) {
|
|
1399
|
+
blockIndent = indentOf(state, text);
|
|
1400
|
+
break;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
const base = blockIndent < 0 ? 0 : blockIndent;
|
|
1404
|
+
if (options.blocks.some((block) => block.close.test(currentWord)))
|
|
1405
|
+
return base;
|
|
1406
|
+
if (LABEL_START.test(current))
|
|
1407
|
+
return base;
|
|
1408
|
+
if (options.sectionBreaks.test(currentWord))
|
|
1409
|
+
return base;
|
|
1410
|
+
if (previous == null)
|
|
1411
|
+
return 0;
|
|
1412
|
+
const previousIndent = indentOf(state, previous);
|
|
1413
|
+
const previousWord = firstWord(previous);
|
|
1414
|
+
if (LABEL_LINE.test(previous))
|
|
1415
|
+
return previousIndent + unit;
|
|
1416
|
+
if (options.blocks.some((block) => block.open.test(previousWord)))
|
|
1417
|
+
return previousIndent + unit;
|
|
1418
|
+
if (blockIndent >= 0 && previousIndent <= blockIndent)
|
|
1419
|
+
return blockIndent + unit;
|
|
1420
|
+
return previousIndent;
|
|
1421
|
+
});
|
|
1422
|
+
}
|
|
1423
|
+
/** Folding and indentation for the blocks and labels of one assembler. */
|
|
1424
|
+
function assemblyLayout(options) {
|
|
1425
|
+
return [foldStatements(options), indentStatements(options)];
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
const CONDITIONS = "o no b nb c nc ae nae z nz e ne be nbe a na s ns p np pe po l nl ge nge le nle g ng";
|
|
1429
|
+
function conditional$1(prefix) {
|
|
1430
|
+
return CONDITIONS.split(" ").map((cc) => prefix + cc).join(" ");
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* The x86-64 instructions offered by completion. It covers the general
|
|
1434
|
+
* purpose instructions, x87, SSE to SSE4.2, the common AVX, AVX2 and AVX-512
|
|
1435
|
+
* instructions, BMI, AES and SHA. Instructions that are invalid in 64-bit mode,
|
|
1436
|
+
* such as `aaa` and `pusha`, are left out.
|
|
1437
|
+
*/
|
|
1438
|
+
const MNEMONICS$2 = wordSet(`
|
|
1439
|
+
mov movzx movsx movsxd lea xchg xadd cmpxchg cmpxchg8b cmpxchg16b bswap movbe
|
|
1440
|
+
push pop pushf pushfq popf popfq lahf sahf
|
|
1441
|
+
add adc sub sbb imul mul idiv div inc dec neg cmp
|
|
1442
|
+
and or xor not test
|
|
1443
|
+
shl shr sal sar rol ror rcl rcr shld shrd
|
|
1444
|
+
bt bts btr btc bsf bsr popcnt lzcnt tzcnt
|
|
1445
|
+
andn bextr blsi blsmsk blsr bzhi mulx pdep pext rorx sarx shlx shrx
|
|
1446
|
+
adcx adox crc32
|
|
1447
|
+
cbw cwde cdqe cwd cdq cqo
|
|
1448
|
+
jmp call ret leave enter loop loope loopne loopz loopnz jrcxz jecxz
|
|
1449
|
+
${conditional$1("j")} ${conditional$1("set")} ${conditional$1("cmov")}
|
|
1450
|
+
movsb movsw movsd movsq cmpsb cmpsw cmpsd cmpsq scasb scasw scasd scasq
|
|
1451
|
+
lodsb lodsw lodsd lodsq stosb stosw stosd stosq
|
|
1452
|
+
rep repe repz repne repnz lock
|
|
1453
|
+
clc stc cmc cld std cli sti
|
|
1454
|
+
nop pause hlt int int3 syscall sysret cpuid rdtsc rdtscp rdrand rdseed
|
|
1455
|
+
lfence mfence sfence prefetcht0 prefetcht1 prefetcht2 prefetchnta prefetchw
|
|
1456
|
+
clflush clflushopt xlatb ud2 endbr64
|
|
1457
|
+
|
|
1458
|
+
fld fst fstp fild fist fistp fisttp fxch fldz fld1 fldpi fldl2e fldln2
|
|
1459
|
+
fadd faddp fsub fsubp fsubr fsubrp fmul fmulp fdiv fdivp fdivr fdivrp
|
|
1460
|
+
fabs fchs fsqrt fsin fcos fsincos fptan fpatan fprem frndint fscale
|
|
1461
|
+
fcom fcomp fcompp fcomi fcomip fucomi fucomip ftst fxam
|
|
1462
|
+
finit fninit fldcw fstcw fnstcw fstsw fnstsw fwait
|
|
1463
|
+
|
|
1464
|
+
emms movd movq
|
|
1465
|
+
movaps movups movapd movupd movss movsd movhlps movlhps movhps movlps
|
|
1466
|
+
movhpd movlpd movmskps movmskpd movdqa movdqu movntdq movnti movntps lddqu
|
|
1467
|
+
movddup movshdup movsldup movdq2q movq2dq
|
|
1468
|
+
addps addss addpd addsd subps subss subpd subsd mulps mulss mulpd mulsd
|
|
1469
|
+
divps divss divpd divsd sqrtps sqrtss sqrtpd sqrtsd rcpps rcpss rsqrtps rsqrtss
|
|
1470
|
+
maxps maxss maxpd maxsd minps minss minpd minsd
|
|
1471
|
+
addsubps addsubpd haddps haddpd hsubps hsubpd dpps dppd
|
|
1472
|
+
andps andpd andnps andnpd orps orpd xorps xorpd
|
|
1473
|
+
cmpps cmpss cmppd cmpsd cmpeqps cmpltps cmpleps cmpneqps cmpnltps cmpnleps
|
|
1474
|
+
cmpeqss cmpltss cmpless cmpneqss cmpeqpd cmpltpd cmplepd cmpeqsd cmpltsd cmplesd
|
|
1475
|
+
comiss comisd ucomiss ucomisd
|
|
1476
|
+
shufps shufpd unpcklps unpckhps unpcklpd unpckhpd
|
|
1477
|
+
blendps blendpd blendvps blendvpd insertps extractps
|
|
1478
|
+
roundps roundss roundpd roundsd
|
|
1479
|
+
cvtsi2ss cvtsi2sd cvtss2si cvtsd2si cvttss2si cvttsd2si cvtss2sd cvtsd2ss
|
|
1480
|
+
cvtps2pd cvtpd2ps cvtdq2ps cvtps2dq cvttps2dq cvtdq2pd cvtpd2dq cvttpd2dq
|
|
1481
|
+
ldmxcsr stmxcsr
|
|
1482
|
+
paddb paddw paddd paddq paddsb paddsw paddusb paddusw
|
|
1483
|
+
psubb psubw psubd psubq psubsb psubsw psubusb psubusw
|
|
1484
|
+
pmullw pmulld pmulhw pmulhuw pmuludq pmuldq pmaddwd pmaddubsw pmulhrsw
|
|
1485
|
+
pand pandn por pxor
|
|
1486
|
+
psllw pslld psllq psrlw psrld psrlq psraw psrad pslldq psrldq
|
|
1487
|
+
pcmpeqb pcmpeqw pcmpeqd pcmpeqq pcmpgtb pcmpgtw pcmpgtd pcmpgtq
|
|
1488
|
+
pmaxsb pmaxsw pmaxsd pmaxub pmaxuw pmaxud pminsb pminsw pminsd pminub pminuw pminud
|
|
1489
|
+
pabsb pabsw pabsd psignb psignw psignd pavgb pavgw psadbw
|
|
1490
|
+
packsswb packssdw packuswb packusdw
|
|
1491
|
+
punpcklbw punpcklwd punpckldq punpcklqdq punpckhbw punpckhwd punpckhdq punpckhqdq
|
|
1492
|
+
pshufb pshufd pshufhw pshuflw palignr pblendw pblendvb
|
|
1493
|
+
pextrb pextrw pextrd pextrq pinsrb pinsrw pinsrd pinsrq
|
|
1494
|
+
pmovmskb pmovsxbw pmovsxbd pmovsxbq pmovsxwd pmovsxwq pmovsxdq
|
|
1495
|
+
pmovzxbw pmovzxbd pmovzxbq pmovzxwd pmovzxwq pmovzxdq
|
|
1496
|
+
phaddw phaddd phaddsw phsubw phsubd phsubsw phminposuw
|
|
1497
|
+
ptest mpsadbw pcmpestri pcmpestrm pcmpistri pcmpistrm pclmulqdq
|
|
1498
|
+
aesenc aesenclast aesdec aesdeclast aesimc aeskeygenassist
|
|
1499
|
+
sha1rnds4 sha1nexte sha1msg1 sha1msg2 sha256rnds2 sha256msg1 sha256msg2
|
|
1500
|
+
|
|
1501
|
+
vzeroupper vzeroall
|
|
1502
|
+
vmovaps vmovups vmovapd vmovupd vmovss vmovsd vmovd vmovq vmovdqa vmovdqu
|
|
1503
|
+
vmovdqa32 vmovdqa64 vmovdqu8 vmovdqu16 vmovdqu32 vmovdqu64 vmovmskps vmovmskpd
|
|
1504
|
+
vaddps vaddss vaddpd vaddsd vsubps vsubss vsubpd vsubsd
|
|
1505
|
+
vmulps vmulss vmulpd vmulsd vdivps vdivss vdivpd vdivsd
|
|
1506
|
+
vsqrtps vsqrtss vsqrtpd vsqrtsd vmaxps vmaxpd vminps vminpd
|
|
1507
|
+
vandps vandpd vorps vorpd vxorps vxorpd vandnps vandnpd
|
|
1508
|
+
vcmpps vcmppd vcmpss vcmpsd vcomiss vcomisd vucomiss vucomisd
|
|
1509
|
+
vshufps vshufpd vpermilps vpermilpd vperm2f128 vperm2i128 vpermd vpermq vpermps
|
|
1510
|
+
vbroadcastss vbroadcastsd vbroadcastf128 vpbroadcastb vpbroadcastw vpbroadcastd vpbroadcastq
|
|
1511
|
+
vextractf128 vextracti128 vinsertf128 vinserti128 vblendps vblendpd vblendvps vblendvpd
|
|
1512
|
+
vhaddps vhaddpd vroundps vroundpd vroundss vroundsd
|
|
1513
|
+
vcvtsi2ss vcvtsi2sd vcvtss2si vcvtsd2si vcvttss2si vcvttsd2si vcvtss2sd vcvtsd2ss
|
|
1514
|
+
vcvtdq2ps vcvtps2dq vcvttps2dq vcvtps2pd vcvtpd2ps vcvtph2ps vcvtps2ph
|
|
1515
|
+
vfmadd132ps vfmadd213ps vfmadd231ps vfmadd132pd vfmadd213pd vfmadd231pd
|
|
1516
|
+
vfmadd132ss vfmadd213ss vfmadd231ss vfmadd132sd vfmadd213sd vfmadd231sd
|
|
1517
|
+
vfmsub132ps vfmsub213ps vfmsub231ps vfmsub132sd vfmsub213sd vfmsub231sd
|
|
1518
|
+
vfnmadd132ps vfnmadd213ps vfnmadd231ps vfnmadd132sd vfnmadd213sd vfnmadd231sd
|
|
1519
|
+
vpaddb vpaddw vpaddd vpaddq vpsubb vpsubw vpsubd vpsubq
|
|
1520
|
+
vpmullw vpmulld vpmuludq vpand vpandn vpor vpxor vpandd vpandq vpord vporq vpxord vpxorq
|
|
1521
|
+
vpsllw vpslld vpsllq vpsrlw vpsrld vpsrlq vpsraw vpsrad vpsllvd vpsllvq vpsrlvd vpsrlvq
|
|
1522
|
+
vpcmpeqb vpcmpeqw vpcmpeqd vpcmpeqq vpcmpgtb vpcmpgtw vpcmpgtd vpcmpgtq
|
|
1523
|
+
vpcmpb vpcmpub vpcmpd vpcmpud vpcmpq vpcmpuq
|
|
1524
|
+
vpmaxsd vpmaxud vpminsd vpminud vpabsb vpabsw vpabsd
|
|
1525
|
+
vpshufb vpshufd vpalignr vpblendd vpblendvb vpmovmskb vptest
|
|
1526
|
+
vpunpcklbw vpunpckhbw vpunpckldq vpunpckhdq vpackuswb vpacksswb
|
|
1527
|
+
vpgatherdd vpgatherqq vgatherdps vgatherdpd vpcompressd vpexpandd
|
|
1528
|
+
vpternlogd vpternlogq vptestmb vptestmd vptestnmb vpconflictd vplzcntd vpopcntd vpopcntb
|
|
1529
|
+
kmovb kmovw kmovd kmovq kandw kandd kandq korw kord korq kxorw kxord kxorq
|
|
1530
|
+
knotw knotd knotq kortestw kortestd kortestq ktestw ktestd ktestq kshiftlw kshiftrw
|
|
1531
|
+
`);
|
|
1532
|
+
|
|
1533
|
+
/**
|
|
1534
|
+
The assembly language with the `x86` dialect, which reads NASM as written
|
|
1535
|
+
for x86-64.
|
|
1536
|
+
*/
|
|
1537
|
+
const x86_64AssemblyLanguage = assemblyLanguage.configure({ dialect: "x86" }, "x86-64-assembly");
|
|
1538
|
+
/**
|
|
1539
|
+
Snippets for the common shapes of an x86-64 NASM program.
|
|
1540
|
+
*/
|
|
1541
|
+
const snippets$2 = [
|
|
1542
|
+
autocomplete.snippetCompletion("section .text\nglobal ${name}\n${name}:\n\t${}\n\tret", {
|
|
1543
|
+
label: "section .text",
|
|
1544
|
+
detail: "with a global function",
|
|
1545
|
+
type: "keyword"
|
|
1546
|
+
}),
|
|
1547
|
+
autocomplete.snippetCompletion("section .data\n${name}: db ${\"text\"}, 0\n${name}_len: equ $ - ${name}", {
|
|
1548
|
+
label: "section .data",
|
|
1549
|
+
detail: "with a string",
|
|
1550
|
+
type: "keyword"
|
|
1551
|
+
}),
|
|
1552
|
+
autocomplete.snippetCompletion("section .bss\n${name}: resb ${64}", {
|
|
1553
|
+
label: "section .bss",
|
|
1554
|
+
detail: "with a buffer",
|
|
1555
|
+
type: "keyword"
|
|
1556
|
+
}),
|
|
1557
|
+
autocomplete.snippetCompletion("${name}:\n\tpush rbp\n\tmov rbp, rsp\n\t${}\n\tmov rsp, rbp\n\tpop rbp\n\tret", {
|
|
1558
|
+
label: "function",
|
|
1559
|
+
detail: "with prologue and epilogue",
|
|
1560
|
+
type: "keyword"
|
|
1561
|
+
}),
|
|
1562
|
+
autocomplete.snippetCompletion("\tmov rcx, ${count}\n.${loop}:\n\t${}\n\tdec rcx\n\tjnz .${loop}", {
|
|
1563
|
+
label: "loop",
|
|
1564
|
+
detail: "counting down rcx",
|
|
1565
|
+
type: "keyword"
|
|
1566
|
+
}),
|
|
1567
|
+
autocomplete.snippetCompletion("\tcmp ${rax}, ${0}\n\tj${e} .${label}", {
|
|
1568
|
+
label: "cmp",
|
|
1569
|
+
detail: "and jump",
|
|
1570
|
+
type: "keyword"
|
|
1571
|
+
}),
|
|
1572
|
+
autocomplete.snippetCompletion("%macro ${name} ${1}\n\t${}\n%endmacro", {
|
|
1573
|
+
label: "%macro",
|
|
1574
|
+
detail: "block",
|
|
1575
|
+
type: "keyword"
|
|
1576
|
+
}),
|
|
1577
|
+
autocomplete.snippetCompletion("struc ${name}\n\t.${field}: resq 1\nendstruc", {
|
|
1578
|
+
label: "struc",
|
|
1579
|
+
detail: "block",
|
|
1580
|
+
type: "keyword"
|
|
1581
|
+
})
|
|
1582
|
+
];
|
|
1583
|
+
/**
|
|
1584
|
+
Completion for x86-64 NASM. Where a statement starts it offers the x86-64
|
|
1585
|
+
mnemonics, NASM's directives, the macros declared in the file and the
|
|
1586
|
+
snippets. In an operand it offers the registers, the size keywords and the
|
|
1587
|
+
labels and constants declared in the file.
|
|
1588
|
+
*/
|
|
1589
|
+
const x86_64Completion = assemblyCompletionSource({
|
|
1590
|
+
mnemonics: MNEMONICS$2,
|
|
1591
|
+
directives: DIRECTIVES$3,
|
|
1592
|
+
preprocessor: PREPROCESSOR,
|
|
1593
|
+
registers: REGISTERS$3,
|
|
1594
|
+
sizeKeywords: SIZE_KEYWORDS$1,
|
|
1595
|
+
operandDirectives: [...DATA_DIRECTIVES, "times"],
|
|
1596
|
+
sections: SECTIONS$1,
|
|
1597
|
+
prefixes: PREFIXES,
|
|
1598
|
+
snippets: snippets$2
|
|
1599
|
+
});
|
|
1600
|
+
/**
|
|
1601
|
+
Folding and indentation for NASM's blocks, and for a function's body under
|
|
1602
|
+
its label.
|
|
1603
|
+
*/
|
|
1604
|
+
const x86_64Layout = assemblyLayout({
|
|
1605
|
+
blocks: [
|
|
1606
|
+
{ open: /^%i?macro$/, close: /^%endmacro$/ },
|
|
1607
|
+
{ open: /^%rep$/, close: /^%endrep$/ },
|
|
1608
|
+
{ open: /^%if/, close: /^%endif$/ },
|
|
1609
|
+
{ open: /^struc$/, close: /^endstruc$/ },
|
|
1610
|
+
{ open: /^istruc$/, close: /^iend$/ }
|
|
1611
|
+
],
|
|
1612
|
+
sectionBreaks: /^(section|segment|global|extern|default|bits)$/
|
|
1613
|
+
});
|
|
1614
|
+
/**
|
|
1615
|
+
x86-64 assembly support for NASM, as used on Exercism's x86-64 Assembly
|
|
1616
|
+
track.
|
|
1617
|
+
*/
|
|
1618
|
+
function x86_64Assembly() {
|
|
1619
|
+
return new language.LanguageSupport(x86_64AssemblyLanguage, [
|
|
1620
|
+
x86_64AssemblyLanguage.data.of({
|
|
1621
|
+
autocomplete: x86_64Completion,
|
|
1622
|
+
// Typing the colon of a label or the end of a block moves it back to
|
|
1623
|
+
// the margin of its block.
|
|
1624
|
+
indentOnInput: /^\s*(%end\w*|%else|%elif\w*|endstruc|iend|(%%|\.)?[\w$?@.]+:)$/i
|
|
1625
|
+
}),
|
|
1626
|
+
x86_64Layout
|
|
1627
|
+
]);
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
function conditional(prefix) {
|
|
1631
|
+
return [...CONDITIONS$1].map((cc) => prefix + cc).join(" ");
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* The AArch64 instructions offered by completion. It covers the base
|
|
1635
|
+
* instructions, the aliases the GNU assembler accepts, the atomics, the
|
|
1636
|
+
* floating point instructions and the common Advanced SIMD (NEON)
|
|
1637
|
+
* instructions. `b.eq` and the other conditional branches are listed one by
|
|
1638
|
+
* one, so typing `b.` offers them all.
|
|
1639
|
+
*/
|
|
1640
|
+
const MNEMONICS$1 = wordSet(`
|
|
1641
|
+
mov movz movn movk mvn adr adrp
|
|
1642
|
+
add adds sub subs neg negs adc adcs sbc sbcs ngc ngcs cmp cmn
|
|
1643
|
+
mul madd msub mneg smull umull smaddl umaddl smsubl umsubl smnegl umnegl smulh umulh
|
|
1644
|
+
sdiv udiv
|
|
1645
|
+
and ands orr orn eor eon bic bics tst
|
|
1646
|
+
lsl lsr asr ror lslv lsrv asrv rorv
|
|
1647
|
+
sxtb sxth sxtw uxtb uxth
|
|
1648
|
+
sbfm ubfm bfm sbfx ubfx bfi bfxil sbfiz ubfiz bfc extr
|
|
1649
|
+
clz cls rbit rev rev16 rev32 rev64 cnt abs smax smin umax umin
|
|
1650
|
+
csel csinc csinv csneg cset csetm cinc cinv cneg ccmp ccmn
|
|
1651
|
+
b bl br blr ret cbz cbnz tbz tbnz
|
|
1652
|
+
${conditional("b.")} ${conditional("bc.")}
|
|
1653
|
+
beq bne bcs bhs bcc blo bmi bpl bvs bvc bhi bls bge blt bgt ble bal
|
|
1654
|
+
ldr ldrb ldrh ldrsb ldrsh ldrsw str strb strh
|
|
1655
|
+
ldur ldurb ldurh ldursb ldursh ldursw stur sturb sturh
|
|
1656
|
+
ldp ldpsw stp ldnp stnp ldtr ldtrb ldtrh sttr sttrb sttrh
|
|
1657
|
+
ldxr ldxrb ldxrh ldxp stxr stxrb stxrh stxp
|
|
1658
|
+
ldaxr ldaxrb ldaxrh ldaxp stlxr stlxrb stlxrh stlxp
|
|
1659
|
+
ldar ldarb ldarh stlr stlrb stlrh ldapr ldaprb ldaprh
|
|
1660
|
+
cas casa casl casal casp swp swpa swpl swpal
|
|
1661
|
+
ldadd ldadda ldaddl ldaddal ldclr ldeor ldset ldsmax ldsmin ldumax ldumin
|
|
1662
|
+
stadd stclr steor stset
|
|
1663
|
+
prfm prfum
|
|
1664
|
+
svc hvc smc brk hlt nop yield wfe wfi sev sevl isb dsb dmb
|
|
1665
|
+
mrs msr sys sysl ic dc tlbi at hint clrex
|
|
1666
|
+
paciasp autiasp pacibsp autibsp bti
|
|
1667
|
+
fmov fadd fsub fmul fdiv fnmul fmadd fmsub fnmadd fnmsub fneg fabs fsqrt
|
|
1668
|
+
fmax fmin fmaxnm fminnm fcmp fcmpe fccmp fccmpe fcsel
|
|
1669
|
+
fcvt fcvtzs fcvtzu fcvtas fcvtau fcvtms fcvtmu fcvtns fcvtnu fcvtps fcvtpu
|
|
1670
|
+
scvtf ucvtf frinta frinti frintm frintn frintp frintx frintz
|
|
1671
|
+
ld1 ld2 ld3 ld4 ld1r st1 st2 st3 st4
|
|
1672
|
+
movi mvni dup ins umov smov ext zip1 zip2 uzp1 uzp2 trn1 trn2 tbl tbx
|
|
1673
|
+
addv addp uaddlv saddlv umaxv uminv smaxv sminv umaxp uminp smaxp sminp
|
|
1674
|
+
cmeq cmge cmgt cmhi cmhs cmle cmlt cmtst
|
|
1675
|
+
mla mls uaddl uaddw usubl usubw saddl saddw ssubl ssubw
|
|
1676
|
+
shl ushr sshr ushl sshl uqadd sqadd uqsub sqsub
|
|
1677
|
+
xtn uxtl sxtl not bsl bit bif
|
|
1678
|
+
faddp fmaxv fminv fmla fmls frecpe frsqrte
|
|
1679
|
+
`);
|
|
1680
|
+
|
|
1681
|
+
/**
|
|
1682
|
+
The assembly language with the `arm64` dialect, which reads the GNU
|
|
1683
|
+
assembler as written for AArch64.
|
|
1684
|
+
*/
|
|
1685
|
+
const arm64AssemblyLanguage = assemblyLanguage.configure({ dialect: "arm64" }, "arm64-assembly");
|
|
1686
|
+
/**
|
|
1687
|
+
Snippets for the common shapes of an AArch64 program.
|
|
1688
|
+
*/
|
|
1689
|
+
const snippets$1 = [
|
|
1690
|
+
autocomplete.snippetCompletion(".text\n.globl ${name}\n${name}:\n\t${}\n\tret", {
|
|
1691
|
+
label: ".text",
|
|
1692
|
+
detail: "with a global function",
|
|
1693
|
+
type: "keyword"
|
|
1694
|
+
}),
|
|
1695
|
+
autocomplete.snippetCompletion(".data\n${name}: .asciz ${\"text\"}\n.equ ${name}_len, . - ${name}", {
|
|
1696
|
+
label: ".data",
|
|
1697
|
+
detail: "with a string",
|
|
1698
|
+
type: "keyword"
|
|
1699
|
+
}),
|
|
1700
|
+
autocomplete.snippetCompletion(".bss\n${name}: .skip ${64}", {
|
|
1701
|
+
label: ".bss",
|
|
1702
|
+
detail: "with a buffer",
|
|
1703
|
+
type: "keyword"
|
|
1704
|
+
}),
|
|
1705
|
+
autocomplete.snippetCompletion("${name}:\n\tstp x29, x30, [sp, #-16]!\n\tmov x29, sp\n\t${}\n\tldp x29, x30, [sp], #16\n\tret", {
|
|
1706
|
+
label: "function",
|
|
1707
|
+
detail: "with prologue and epilogue",
|
|
1708
|
+
type: "keyword"
|
|
1709
|
+
}),
|
|
1710
|
+
autocomplete.snippetCompletion("\tmov x9, #${count}\n1:\n\t${}\n\tsubs x9, x9, #1\n\tb.ne 1b", {
|
|
1711
|
+
label: "loop",
|
|
1712
|
+
detail: "counting down x9",
|
|
1713
|
+
type: "keyword"
|
|
1714
|
+
}),
|
|
1715
|
+
autocomplete.snippetCompletion("\tcmp ${x0}, #${0}\n\tb.${eq} ${label}", {
|
|
1716
|
+
label: "cmp",
|
|
1717
|
+
detail: "and branch",
|
|
1718
|
+
type: "keyword"
|
|
1719
|
+
}),
|
|
1720
|
+
autocomplete.snippetCompletion("\tcbz ${x0}, ${label}", {
|
|
1721
|
+
label: "cbz",
|
|
1722
|
+
detail: "branch if zero",
|
|
1723
|
+
type: "keyword"
|
|
1724
|
+
}),
|
|
1725
|
+
autocomplete.snippetCompletion("\tadrp ${x0}, ${label}\n\tadd ${x0}, ${x0}, :lo12:${label}", {
|
|
1726
|
+
label: "adrp",
|
|
1727
|
+
detail: "load an address",
|
|
1728
|
+
type: "keyword"
|
|
1729
|
+
}),
|
|
1730
|
+
autocomplete.snippetCompletion(".macro ${name} ${arg}\n\t${}\n.endm", {
|
|
1731
|
+
label: ".macro",
|
|
1732
|
+
detail: "block",
|
|
1733
|
+
type: "keyword"
|
|
1734
|
+
})
|
|
1735
|
+
];
|
|
1736
|
+
/**
|
|
1737
|
+
Completion for AArch64 in GNU assembler syntax. Where a statement starts it
|
|
1738
|
+
offers the AArch64 mnemonics, the GNU assembler's directives, the macros
|
|
1739
|
+
declared in the file and the snippets. In an operand it offers the
|
|
1740
|
+
registers, shifts, condition codes, and the labels, `.equ` constants and
|
|
1741
|
+
`.req` aliases declared in the file. A numeric label `1:` is offered as `1b`
|
|
1742
|
+
below it and `1f` above it.
|
|
1743
|
+
*/
|
|
1744
|
+
const arm64Completion = assemblyCompletionSource({
|
|
1745
|
+
mnemonics: MNEMONICS$1,
|
|
1746
|
+
directives: DIRECTIVES$2,
|
|
1747
|
+
registers: REGISTERS$2,
|
|
1748
|
+
sizeKeywords: [...SHIFTS, ...CONDITIONS$1],
|
|
1749
|
+
sections: SECTIONS,
|
|
1750
|
+
relocations: RELOCATIONS,
|
|
1751
|
+
snippets: snippets$1,
|
|
1752
|
+
labels: "gas"
|
|
1753
|
+
});
|
|
1754
|
+
/**
|
|
1755
|
+
Folding and indentation for the GNU assembler's blocks, and for a
|
|
1756
|
+
function's body under its label.
|
|
1757
|
+
*/
|
|
1758
|
+
const arm64Layout = assemblyLayout({
|
|
1759
|
+
blocks: [
|
|
1760
|
+
{ open: /^\.macro$/, close: /^\.endm$/ },
|
|
1761
|
+
{ open: /^\.(rept|irpc?)$/, close: /^\.endr$/ },
|
|
1762
|
+
{ open: /^\.if/, close: /^\.endif$/ }
|
|
1763
|
+
],
|
|
1764
|
+
sectionBreaks: /^\.(section|text|data|bss|pushsection|popsection|globl|global|extern|type|size|equ|set)$/,
|
|
1765
|
+
labels: "gas",
|
|
1766
|
+
lineComments: ["//", "/*", ";"]
|
|
1767
|
+
});
|
|
1768
|
+
/**
|
|
1769
|
+
AArch64 assembly support for the GNU assembler, as used on Exercism's
|
|
1770
|
+
ARM64 Assembly track.
|
|
1771
|
+
*/
|
|
1772
|
+
function arm64Assembly() {
|
|
1773
|
+
return new language.LanguageSupport(arm64AssemblyLanguage, [
|
|
1774
|
+
arm64AssemblyLanguage.data.of({
|
|
1775
|
+
autocomplete: arm64Completion,
|
|
1776
|
+
commentTokens: { line: "//", block: { open: "/*", close: "*/" } },
|
|
1777
|
+
// Typing the colon of a label or the end of a block moves it back to
|
|
1778
|
+
// the margin of its block.
|
|
1779
|
+
indentOnInput: /^\s*(\.endm|\.endr|\.endif|[\w$.\\@]+:)$/i
|
|
1780
|
+
}),
|
|
1781
|
+
arm64Layout
|
|
1782
|
+
]);
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
/** The MIPS32 instructions that MARS assembles, offered where a statement starts. */
|
|
1786
|
+
const INSTRUCTIONS = wordSet(`
|
|
1787
|
+
add addu addi addiu sub subu and andi or ori xor xori nor
|
|
1788
|
+
sll srl sra sllv srlv srav slt sltu slti sltiu lui
|
|
1789
|
+
mult multu div divu mfhi mflo mthi mtlo madd maddu msub msubu mul clo clz
|
|
1790
|
+
movn movz movf movt
|
|
1791
|
+
beq bne bgez bgezal bgtz blez bltz bltzal j jal jr jalr
|
|
1792
|
+
lb lbu lh lhu lw lwl lwr ll sb sh sw swl swr sc
|
|
1793
|
+
syscall break eret nop
|
|
1794
|
+
teq teqi tne tnei tge tgeu tgei tgeiu tlt tltu tlti tltiu
|
|
1795
|
+
mfc0 mtc0 mfc1 mtc1 lwc1 swc1 ldc1 sdc1 bc1t bc1f
|
|
1796
|
+
add.s add.d sub.s sub.d mul.s mul.d div.s div.d
|
|
1797
|
+
abs.s abs.d neg.s neg.d mov.s mov.d sqrt.s sqrt.d
|
|
1798
|
+
movn.s movn.d movz.s movz.d movf.s movf.d movt.s movt.d
|
|
1799
|
+
cvt.s.d cvt.d.s cvt.w.s cvt.w.d cvt.s.w cvt.d.w
|
|
1800
|
+
c.eq.s c.eq.d c.lt.s c.lt.d c.le.s c.le.d
|
|
1801
|
+
ceil.w.s ceil.w.d floor.w.s floor.w.d round.w.s round.w.d trunc.w.s trunc.w.d
|
|
1802
|
+
`);
|
|
1803
|
+
/** The pseudo-instructions that MARS expands into one or more instructions. */
|
|
1804
|
+
const PSEUDO_INSTRUCTIONS = wordSet(`
|
|
1805
|
+
li la move not neg negu abs
|
|
1806
|
+
b beqz bnez bge bgeu bgt bgtu ble bleu blt bltu
|
|
1807
|
+
subi subiu seq sne sge sgeu sgt sgtu sle sleu
|
|
1808
|
+
rem remu mulo mulou mulu rol ror
|
|
1809
|
+
ld sd ulw usw ulh ulhu ush
|
|
1810
|
+
l.s l.d s.s s.d
|
|
1811
|
+
`);
|
|
1812
|
+
const MNEMONICS = [...INSTRUCTIONS, ...PSEUDO_INSTRUCTIONS];
|
|
1813
|
+
|
|
1814
|
+
/**
|
|
1815
|
+
The assembly language with the `mips` dialect, which reads MIPS32 as the
|
|
1816
|
+
MARS simulator assembles it.
|
|
1817
|
+
*/
|
|
1818
|
+
const mipsAssemblyLanguage = assemblyLanguage.configure({ dialect: "mips" }, "mips-assembly");
|
|
1819
|
+
/**
|
|
1820
|
+
Snippets for the common shapes of a MIPS program.
|
|
1821
|
+
*/
|
|
1822
|
+
const snippets = [
|
|
1823
|
+
autocomplete.snippetCompletion(".text\n.globl ${name}\n${name}:\n\t${}\n\tjr $ra", {
|
|
1824
|
+
label: ".text",
|
|
1825
|
+
detail: "with a global function",
|
|
1826
|
+
type: "keyword"
|
|
1827
|
+
}),
|
|
1828
|
+
autocomplete.snippetCompletion(".data\n${name}: .asciiz ${\"text\"}", {
|
|
1829
|
+
label: ".data",
|
|
1830
|
+
detail: "with a string",
|
|
1831
|
+
type: "keyword"
|
|
1832
|
+
}),
|
|
1833
|
+
autocomplete.snippetCompletion("${name}:\n\taddi $sp, $sp, -8\n\tsw $ra, 4($sp)\n\tsw $s0, 0($sp)\n\t${}\n" +
|
|
1834
|
+
"\tlw $s0, 0($sp)\n\tlw $ra, 4($sp)\n\taddi $sp, $sp, 8\n\tjr $ra", {
|
|
1835
|
+
label: "function",
|
|
1836
|
+
detail: "with a stack frame",
|
|
1837
|
+
type: "keyword"
|
|
1838
|
+
}),
|
|
1839
|
+
autocomplete.snippetCompletion("\tli $t0, 0\n${loop}:\n\tbge $t0, ${count}, ${done}\n\t${}\n\taddi $t0, $t0, 1\n\tj ${loop}\n${done}:", {
|
|
1840
|
+
label: "loop",
|
|
1841
|
+
detail: "counting up $t0",
|
|
1842
|
+
type: "keyword"
|
|
1843
|
+
}),
|
|
1844
|
+
autocomplete.snippetCompletion("\tbne ${$t0}, ${$t1}, ${else}\n\t${}\n\tj ${done}\n${else}:\n\t\n${done}:", {
|
|
1845
|
+
label: "if",
|
|
1846
|
+
detail: "branch on equal and else",
|
|
1847
|
+
type: "keyword"
|
|
1848
|
+
}),
|
|
1849
|
+
autocomplete.snippetCompletion("\tli $v0, 4\n\tla $a0, ${msg}\n\tsyscall", {
|
|
1850
|
+
label: "print_string",
|
|
1851
|
+
detail: "syscall 4",
|
|
1852
|
+
type: "keyword"
|
|
1853
|
+
}),
|
|
1854
|
+
autocomplete.snippetCompletion("\tli $v0, 1\n\tmove $a0, ${$t0}\n\tsyscall", {
|
|
1855
|
+
label: "print_int",
|
|
1856
|
+
detail: "syscall 1",
|
|
1857
|
+
type: "keyword"
|
|
1858
|
+
}),
|
|
1859
|
+
autocomplete.snippetCompletion("\tli $v0, 5\n\tsyscall\n\tmove ${$t0}, $v0", {
|
|
1860
|
+
label: "read_int",
|
|
1861
|
+
detail: "syscall 5",
|
|
1862
|
+
type: "keyword"
|
|
1863
|
+
}),
|
|
1864
|
+
autocomplete.snippetCompletion("\tli $v0, 10\n\tsyscall", {
|
|
1865
|
+
label: "exit",
|
|
1866
|
+
detail: "syscall 10",
|
|
1867
|
+
type: "keyword"
|
|
1868
|
+
}),
|
|
1869
|
+
autocomplete.snippetCompletion(".macro ${name}(%${arg})\n\t${}\n.end_macro", {
|
|
1870
|
+
label: ".macro",
|
|
1871
|
+
detail: "block",
|
|
1872
|
+
type: "keyword"
|
|
1873
|
+
})
|
|
1874
|
+
];
|
|
1875
|
+
/**
|
|
1876
|
+
Completion for MIPS32 as MARS assembles it. Where a statement starts it
|
|
1877
|
+
offers the instructions and pseudo-instructions, the directives, the
|
|
1878
|
+
macros declared in the file and the snippets. In an operand it offers the
|
|
1879
|
+
registers, and the labels and `.eqv` constants declared in the file. After
|
|
1880
|
+
`%` inside a macro, it offers the macro's parameters.
|
|
1881
|
+
*/
|
|
1882
|
+
const mipsCompletion = assemblyCompletionSource({
|
|
1883
|
+
mnemonics: MNEMONICS,
|
|
1884
|
+
directives: DIRECTIVES$1,
|
|
1885
|
+
registers: WRITTEN_REGISTERS,
|
|
1886
|
+
snippets,
|
|
1887
|
+
labels: "gas"
|
|
1888
|
+
});
|
|
1889
|
+
/**
|
|
1890
|
+
Folding and indentation for MARS macros, and for a function's body under
|
|
1891
|
+
its label.
|
|
1892
|
+
*/
|
|
1893
|
+
const mipsLayout = assemblyLayout({
|
|
1894
|
+
blocks: [{ open: /^\.macro$/, close: /^\.end_macro$/ }],
|
|
1895
|
+
sectionBreaks: /^\.(text|data|ktext|kdata|globl|extern|eqv|include)$/,
|
|
1896
|
+
labels: "gas",
|
|
1897
|
+
lineComments: ["#"]
|
|
1898
|
+
});
|
|
1899
|
+
/**
|
|
1900
|
+
MIPS32 assembly support for MARS, as used on Exercism's MIPS Assembly
|
|
1901
|
+
track.
|
|
1902
|
+
*/
|
|
1903
|
+
function mipsAssembly() {
|
|
1904
|
+
return new language.LanguageSupport(mipsAssemblyLanguage, [
|
|
1905
|
+
mipsAssemblyLanguage.data.of({
|
|
1906
|
+
autocomplete: mipsCompletion,
|
|
1907
|
+
commentTokens: { line: "#" },
|
|
1908
|
+
// Typing the colon of a label or the end of a macro moves it back to the
|
|
1909
|
+
// margin of its block.
|
|
1910
|
+
indentOnInput: /^\s*(\.end_macro|[\w$.]+:)$/i
|
|
1911
|
+
}),
|
|
1912
|
+
mipsLayout
|
|
1913
|
+
]);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
exports.arm64Assembly = arm64Assembly;
|
|
1917
|
+
exports.arm64AssemblyLanguage = arm64AssemblyLanguage;
|
|
1918
|
+
exports.arm64Completion = arm64Completion;
|
|
1919
|
+
exports.arm64Layout = arm64Layout;
|
|
1920
|
+
exports.arm64Snippets = snippets$1;
|
|
1921
|
+
exports.assembly = assembly;
|
|
1922
|
+
exports.assemblyCompletion = assemblyCompletion;
|
|
1923
|
+
exports.assemblyCompletionSource = assemblyCompletionSource;
|
|
1924
|
+
exports.assemblyLanguage = assemblyLanguage;
|
|
1925
|
+
exports.assemblyLayout = assemblyLayout;
|
|
1926
|
+
exports.declaredNames = declaredNames;
|
|
1927
|
+
exports.mipsAssembly = mipsAssembly;
|
|
1928
|
+
exports.mipsAssemblyLanguage = mipsAssemblyLanguage;
|
|
1929
|
+
exports.mipsCompletion = mipsCompletion;
|
|
1930
|
+
exports.mipsLayout = mipsLayout;
|
|
1931
|
+
exports.mipsSnippets = snippets;
|
|
1932
|
+
exports.namesInScope = namesInScope;
|
|
1933
|
+
exports.parser = parser;
|
|
1934
|
+
exports.x86_64Assembly = x86_64Assembly;
|
|
1935
|
+
exports.x86_64AssemblyLanguage = x86_64AssemblyLanguage;
|
|
1936
|
+
exports.x86_64Completion = x86_64Completion;
|
|
1937
|
+
exports.x86_64Layout = x86_64Layout;
|
|
1938
|
+
exports.x86_64Snippets = snippets$2;
|