1grammar::me::cpu::gasm(n)Grammar operations and usagegrammar::me::cpu::gasm(n)
2
3
4
5______________________________________________________________________________
6
8 grammar::me::cpu::gasm - ME assembler
9
11 package require grammar::me::cpu::gasm ?0.1?
12
13 ::grammar::me::cpu::gasm::begin g n ?mode? ?note?
14
15 ::grammar::me::cpu::gasm::done --> t
16
17 ::grammar::me::cpu::gasm::state
18
19 ::grammar::me::cpu::gasm::state! s
20
21 ::grammar::me::cpu::gasm::lift t dst = src
22
23 ::grammar::me::cpu::gasm::Inline t node label
24
25 ::grammar::me::cpu::gasm::Cmd cmd ?arg...?
26
27 ::grammar::me::cpu::gasm::Bra
28
29 ::grammar::me::cpu::gasm::Nop text
30
31 ::grammar::me::cpu::gasm::Note text
32
33 ::grammar::me::cpu::gasm::Jmp label
34
35 ::grammar::me::cpu::gasm::Exit
36
37 ::grammar::me::cpu::gasm::Who label
38
39 ::grammar::me::cpu::gasm::/Label name
40
41 ::grammar::me::cpu::gasm::/Clear
42
43 ::grammar::me::cpu::gasm::/Ok
44
45 ::grammar::me::cpu::gasm::/Fail
46
47 ::grammar::me::cpu::gasm::/At name
48
49 ::grammar::me::cpu::gasm::/CloseLoop
50
51_________________________________________________________________
52
54 This package provides a simple in-memory assembler. Its origin is that
55 of a support package for use by packages converting PEG and other gram‐
56 mars into a corresponding matcher based on the ME virtual machine, like
57 page::compiler::peg::mecpu. Despite that it is actually mostly agnostic
58 regarding the instructions, users can choose any instruction set they
59 like.
60
61 The program under construction is held in a graph structure (See pack‐
62 age struct::graph) during assembly and subsequent manipulation, with
63 instructions represented by nodes, and the flow of execution between
64 instructions explicitly encoded in the arcs between them.
65
66 In this model jumps are not encoded explicitly, they are implicit in
67 the arcs. The generation of explicit jumps is left to any code convert‐
68 ing the graph structure into a more conventional representation. The
69 same goes for branches. They are implicitly encoded by all instructions
70 which have two outgoing arcs, whereas all other instructions have only
71 one outgoing arc. Their conditonality is handled by tagging their out‐
72 going arcs with information about the conditions under which they are
73 taken.
74
75 While the graph the assembler operates on is supplied from the outside,
76 i.e. external, it does manage some internal state, namely:
77
78 [1] The handle of the graph node most assembler operations will work
79 on, the anchor.
80
81 [2] A mapping from arbitrary strings to instructions. I.e. it is
82 possible to label an instruction during assembly, and later
83 recall that instruction by its label.
84
85 [3] The condition code to use when creating arcs between instruc‐
86 tions, which is one of always, ok, and fail.
87
88 [4] The current operation mode, one of halt, okfail, and !okfail.
89
90 [5] The name of a node in a tree. This, and the operation mode above
91 are the parts most heavily influenced by the needs of a grammar
92 compiler, as they assume some basic program structures (selected
93 through the operation mode), and intertwine the graph with a
94 tree, like the AST for the grammar to be compiled.
95
97 As the graph the assembler is operating on, and the tree it is inter‐
98 twined with, are supplied to the assembler from the outside it is nec‐
99 essary to specify the API expected from them, and to describe the
100 structures expected and/or generated by the assembler in either.
101
102 [1] Any graph object command used by the assembler has to provide
103 the API as specified in the documentation for the package
104 struct::graph.
105
106 [2] Any tree object command used by the assembler has to provide the
107 API as specified in the documentation for the package
108 struct::tree.
109
110 [3] Any instruction (node) generated by the assembler in a graph
111 will have at least two, and at most three attributes:
112
113 instruction
114 The value of this attribute is the name of the instruc‐
115 tion. The only names currently defined by the assembler
116 are the three pseudo-instructions
117
118 NOP This instruction does nothing. Useful for fixed
119 framework nodes, unchanging jump destinations, and
120 the like. No arguments.
121
122 C A .NOP to allow the insertion of arbitrary com‐
123 ments into the instruction stream, i.e. a comment
124 node. One argument, the text of the comment.
125
126 BRA A .NOP serving as explicitly coded conditional
127 branch. No arguments.
128 However we reserve the space of all instructions whose names
129 begin with a "." (dot) for future use by the assembler.
130
131 arguments
132 The value of this attribute is a list of strings, the
133 arguments of the instruction. The contents are dependent
134 on the actual instruction and the assembler doesn't know
135 or care about them. This means for example that it has no
136 builtin knowledge about what instruction need which argu‐
137 ments and thus doesn't perform any type of checking.
138
139 expr This attribute is optional. When it is present its value
140 is the name of a node in the tree intertwined with the
141 graph.
142
143 [4] Any arc between two instructions will have one attribute:
144
145 condition
146 The value of this attribute determines under which condi‐
147 tion execution will take this arc. It is one of always,
148 ok, and fail. The first condition is used for all arcs
149 which are the single outgoing arc of an instruction. The
150 other two are used for the two outgoing arcs of an
151 instruction which implicitly encode a branch.
152
153 [5] A tree node given to the assembler for cross-referencing will be
154 written to and given the following attributes, some fixed, some
155 dependent on the operation mode. All values will be references
156 to nodes in the instruction graph. Some of the instruction will
157 expect some or specific sets of these attributes.
158
159 gas::entry
160 Always written.
161
162 gas::exit
163 Written for all modes but okfail.
164
165 gas::exit::ok
166 Written for mode okfail.
167
168 gas::exit::fail
169 Written for mode okfail.
170
172 ::grammar::me::cpu::gasm::begin g n ?mode? ?note?
173 This command starts the assembly of an instruction sequence, and
174 (re)initializes the state of the assembler. After completion of
175 the instruction sequence use ::grammar::me::cpu::gasm::done to
176 finalize the assembler.
177
178 It will operate on the graph g in the specified mode (Default is
179 okfail). As part of the initialization it will always create a
180 standard .NOP instruction and label it "entry". The creation of
181 the remaining standard instructions is mode-dependent:
182
183 halt An "icf_halt" instruction labeled "exit/return".
184
185 !okfail
186 An "icf_ntreturn" instruction labeled "exit/return".
187
188 okfail Two .NOP instructions labeled "exit/ok" and "exit/fail"
189 respectively.
190 The note, if specified (default is not), is given to the "entry" .NOP
191 instruction.
192
193 The node reference n is simply stored for use by ::gram‐
194 mar::me::cpu::gasm::done. It has to refer to a node in the tree t argu‐
195 ment of that command.
196
197 After the initialization is done the "entry" instruction will be the
198 anchor, and the condition code will be set to always.
199
200 The command returns the empy string as its result.
201
202 ::grammar::me::cpu::gasm::done --> t
203 This command finalizes the creation of an instruction sequence
204 and then clears the state of the assembler. NOTE that this does
205 not delete any of the created instructions. They can be made
206 available to future begin/done cycles. Further assembly will be
207 possible only after reinitialization of the system via ::gram‐
208 mar::me::cpu::gasm::begin.
209
210 Before the state is cleared selected references to selected
211 instructions will be written to attributes of the node n in the
212 tree t. Which instructions are saved is mode-dependent. Both
213 mode and the destination node n were specified during invokation
214 of ::grammar::me::cpu::gasm::begin.
215
216 Independent of the mode a reference to the instruction labeled
217 "entry" will be saved to the attribute gas::entry of n. The ref‐
218 erence to the node n will further be saved into the attribute
219 "expr" of the "entry" instruction. Beyond that
220
221 halt A reference to the instruction labeled "exit/return" will
222 be saved to the attribute gas::exit of n.
223
224 okfail See halt.
225
226 !okfail
227 Reference to the two instructions labeled "exit/ok" and
228 "exit/fail" will be saved to the attributes gas::exit::ok
229 and gas::exit::fail of n respectively.
230
231 The command returns the empy string as its result.
232
233 ::grammar::me::cpu::gasm::state
234 This command returns the current state of the assembler. Its
235 format is not documented and considered to be internal to the
236 package.
237
238 ::grammar::me::cpu::gasm::state! s
239 This command takes a serialized assembler state s as returned by
240 ::grammar::me::cpu::gasm::state and makes it the current state
241 of the assembler.
242
243 Note that this may overwrite label definitions, however all non-
244 conflicting label definitions in the state before are not
245 touched and merged with s.
246
247 The command returns the empty string as its result.
248
249 ::grammar::me::cpu::gasm::lift t dst = src
250 This command operates on the tree t. It copies the contents of
251 the attributes gas::entry, gas::exit::ok and gas::exit::fail
252 from the node src to the node dst. It returns the empty string
253 as its result.
254
255 ::grammar::me::cpu::gasm::Inline t node label
256 This command links an instruction sequence created by an earlier
257 begin/done pair into the current instruction sequence.
258
259 To this end it
260
261 [1] reads the instruction references from the attributes
262 gas::entry, gas::exit::ok, and gas::exit::fail from the
263 node n of the tree t and makes them available to assem‐
264 bler und the labels label/entry, label/exit::ok, and
265 label/exit::fail respectively.
266
267 [2] Creates an arc from the anchor to the node labeled
268 label/entry, and tags it with the current condition code.
269
270 [3] Makes the node labeled label/exit/ok the new anchor.
271 The command returns the empty string as its result.
272
273 ::grammar::me::cpu::gasm::Cmd cmd ?arg...?
274 This is the basic command to add instructions to the graph. It
275 creates a new instruction of type cmd with the given arguments
276 arg... If the anchor was defined it will also create an arc
277 from the anchor to the new instruction using the current condi‐
278 tion code. After the call the new instruction will be the
279 anchor and the current condition code will be set to always.
280
281 The command returns the empty string as its result.
282
283 ::grammar::me::cpu::gasm::Bra
284 This is a convenience command to create a .BRA pseudo-instruc‐
285 tion. It uses ::grammar::me::cpu::gasm::Cmd to actually create
286 the instruction and inherits its behaviour.
287
288 ::grammar::me::cpu::gasm::Nop text
289 This is a convenience command to create a .NOP pseudo-instruc‐
290 tion. It uses ::grammar::me::cpu::gasm::Cmd to actually create
291 the instruction and inherits its behaviour. The text will be
292 saved as the first and only argument of the new instruction.
293
294 ::grammar::me::cpu::gasm::Note text
295 This is a convenience command to create a .C pseudo-instruction,
296 i.e. a comment. It uses ::grammar::me::cpu::gasm::Cmd to actu‐
297 ally create the instruction and inherits its behaviour. The
298 text will be saved as the first and only argument of the new
299 instruction.
300
301 ::grammar::me::cpu::gasm::Jmp label
302 This command creates an arc from the anchor to the instruction
303 labeled with label, and tags with the the current condition
304 code.
305
306 The command returns the empty string as its result.
307
308 ::grammar::me::cpu::gasm::Exit
309 This command creates an arc from the anchor to one of the exit
310 instructions, based on the operation mode (see ::gram‐
311 mar::me::cpu::gasm::begin), and tags it with current condition
312 code.
313
314 For mode okfail it links to the instruction labeled either
315 "exit/ok" or "exit/fail", depending on the current condition
316 code, and tagging it with the current condition code For the
317 other two modes it links to the instruction labeled
318 "exit/return", tagging it condition code always, independent the
319 current condition code.
320
321 The command returns the empty string as its result.
322
323 ::grammar::me::cpu::gasm::Who label
324 This command returns a reference to the instruction labeled with
325 label.
326
327 ::grammar::me::cpu::gasm::/Label name
328 This command labels the anchor with name. Note that an instruc‐
329 tion can have more than one label.
330
331 The command returns the empty string as its result.
332
333 ::grammar::me::cpu::gasm::/Clear
334 This command clears the anchor, leaving it undefined, and fur‐
335 ther resets the current condition code to always.
336
337 The command returns the empty string as its result.
338
339 ::grammar::me::cpu::gasm::/Ok
340 This command sets the current condition code to ok.
341
342 The command returns the empty string as its result.
343
344 ::grammar::me::cpu::gasm::/Fail
345 This command sets the current condition code to fail.
346
347 The command returns the empty string as its result.
348
349 ::grammar::me::cpu::gasm::/At name
350 This command sets the anchor to the instruction labeled with
351 name, and further resets the current condition code to always.
352
353 The command returns the empty string as its result.
354
355 ::grammar::me::cpu::gasm::/CloseLoop
356 This command marks the anchor as the last instruction in a loop
357 body, by creating the attribute LOOP.
358
359 The command returns the empty string as its result.
360
362 This document, and the package it describes, will undoubtedly contain
363 bugs and other problems. Please report such in the category grammar_me
364 of the Tcllib SF Trackers [http://source‐
365 forge.net/tracker/?group_id=12883]. Please also report any ideas for
366 enhancements you may have for either package and/or documentation.
367
369 assembler, grammar, graph, parsing, tree, virtual machine
370
372 Copyright (c) 2005 Andreas Kupries <andreas_kupries@users.sourceforge.net>
373
374
375
376
377grammar_me 0.1 grammar::me::cpu::gasm(n)