2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 using System.Collections.Generic;
29 * A WordBuilder instance organizes construction of a new interpreted word.
31 * Opcodes are accumulated with specific methods. A control-flow stack
32 * is maintained to resolve jumps.
34 * Each instance shall be used for only one word.
44 List<string> toResolve;
45 Dictionary<string, int> locals;
48 internal SType StackEffect {
53 * Create a new instance, with the specified word name.
55 internal WordBuilder(T0Comp TC, string name)
59 cfStack = new int[16];
61 code = new List<Opcode>();
62 toResolve = new List<string>();
63 locals = new Dictionary<string, int>();
65 StackEffect = SType.UNKNOWN;
69 * Build the word. The control-flow stack must be empty. A 'ret'
70 * opcode is automatically appended if required.
75 throw new Exception("control-flow stack is not empty");
77 if (jumpToLast || code[code.Count - 1].MayFallThrough) {
80 Word w = new WordInterpreted(TC, name, locals.Count,
81 code.ToArray(), toResolve.ToArray());
82 w.StackEffect = StackEffect;
91 void Add(Opcode op, string refName)
94 toResolve.Add(refName);
99 * Rotate the control-flow stack at depth 'depth'.
101 internal void CSRoll(int depth)
103 int x = cfStack[cfPtr - depth];
104 Array.Copy(cfStack, cfPtr - (depth - 1),
105 cfStack, cfPtr - depth, depth);
110 * Make a copy of the control-flow element at depth 'depth', and
111 * push it on top of the control-flow stack.
113 internal void CSPick(int depth)
115 int x = cfStack[cfPtr - depth];
121 int len = cfStack.Length;
122 if (++ cfPtr == len) {
123 int[] ncf = new int[len << 1];
124 Array.Copy(cfStack, 0, ncf, 0, len);
132 return cfStack[cfPtr --];
136 * Push an origin on the control-flow stack, corresponding to the
137 * next opcode to add.
139 internal void CSPushOrig()
145 * Push a destination on the control-flow stack, corresponding to
146 * the next opcode to add.
148 internal void CSPushDest()
150 CSPush(-code.Count - 1);
154 * Pop an origin from the control-flow stack. An exception is
155 * thrown if the value is not an origin.
157 internal int CSPopOrig()
161 throw new Exception("not an origin");
167 * Pop a destination from the control-flow stack. An exception is
168 * thrown if the value is not a destination.
170 internal int CSPopDest()
174 throw new Exception("not a destination");
180 * Add a "push literal" opcode.
182 internal void Literal(TValue v)
184 Add(new OpcodeConst(v));
188 * Compile a "call" by name. This method implements the support
189 * for local variables:
191 * - If the target is '>' followed by a local variable name, then
192 * a "put local" opcode is added.
194 * - Otherwise, if the target is a local variable name, then a
195 * "get local" opcode is added.
197 * - Otherwise, a call to the named word is added. The target name
198 * will be resolved later on (typically, when the word containing
199 * the call opcode is first invoked, or when C code is generated).
201 internal void Call(string target)
205 if (target.StartsWith(">")) {
206 lname = target.Substring(1);
213 if (locals.TryGetValue(lname, out lnum)) {
215 Add(new OpcodePutLocal(lnum));
217 Add(new OpcodeGetLocal(lnum));
220 Add(new OpcodeCall(), target);
225 * Add a "call" opcode to the designated word.
227 internal void CallExt(Word wtarget)
229 Add(new OpcodeCall(wtarget), null);
233 * Add a "call" opcode to a word which is not currently resolved.
234 * This method ignores local variables.
236 internal void CallExt(string target)
238 Add(new OpcodeCall(), target);
242 * Add a "get local" opcode; the provided local name must already
245 internal void GetLocal(string name)
248 if (locals.TryGetValue(name, out lnum)) {
249 Add(new OpcodeGetLocal(lnum));
251 throw new Exception("no such local: " + name);
256 * Add a "put local" opcode; the provided local name must already
259 internal void PutLocal(string name)
262 if (locals.TryGetValue(name, out lnum)) {
263 Add(new OpcodePutLocal(lnum));
265 throw new Exception("no such local: " + name);
270 * Define a new local name.
272 internal void DefLocal(string lname)
274 if (locals.ContainsKey(lname)) {
275 throw new Exception(String.Format(
276 "local already defined: {0}", lname));
278 locals[lname] = locals.Count;
282 * Add a "call" opcode whose target is an XT value (which may be
283 * resolved or as yet unresolved).
285 internal void Call(TPointerXT xt)
287 if (xt.Target == null) {
288 Add(new OpcodeCall(), xt.Name);
290 Add(new OpcodeCall(xt.Target));
295 * Add a "ret" opcode.
299 Add(new OpcodeRet());
303 * Add a forward unconditional jump. The new opcode address is
304 * pushed on the control-flow stack as an origin.
306 internal void Ahead()
309 Add(new OpcodeJumpUncond());
313 * Add a forward conditional jump, which will be taken at runtime
314 * if the top-of-stack value is 'true'. The new opcode address is
315 * pushed on the control-flow stack as an origin.
317 internal void AheadIf()
320 Add(new OpcodeJumpIf());
324 * Add a forward conditional jump, which will be taken at runtime
325 * if the top-of-stack value is 'false'. The new opcode address is
326 * pushed on the control-flow stack as an origin.
328 internal void AheadIfNot()
331 Add(new OpcodeJumpIfNot());
335 * Resolve a previous forward jump to the current code address.
336 * The top of control-flow stack is popped and must be an origin.
341 code[x].ResolveJump(code.Count - x - 1);
346 * Push the current code address on the control-flow stack as a
347 * destination, to be used by an ulterior backward jump.
349 internal void Begin()
355 * Add a backward unconditional jump. The jump target is popped
356 * from the control-flow stack as a destination.
358 internal void Again()
361 Add(new OpcodeJumpUncond(x - code.Count - 1));
365 * Add a backward conditional jump, which will be taken at runtime
366 * if the top-of-stack value is 'true'. The jump target is popped
367 * from the control-flow stack as a destination.
369 internal void AgainIf()
372 Add(new OpcodeJumpIf(x - code.Count - 1));
376 * Add a backward conditional jump, which will be taken at runtime
377 * if the top-of-stack value is 'false'. The jump target is popped
378 * from the control-flow stack as a destination.
380 internal void AgainIfNot()
383 Add(new OpcodeJumpIfNot(x - code.Count - 1));