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 * The implementation for interpreted words.
32 class WordInterpreted : Word {
35 * Get the number of local variables for this word.
37 internal int NumLocals {
42 * Get the sequence of opcodes for this word.
44 internal Opcode[] Code {
50 internal WordInterpreted(T0Comp owner, string name,
51 int numLocals, Opcode[] code, string[] toResolve)
55 this.toResolve = toResolve;
56 NumLocals = numLocals;
59 internal override void Resolve()
61 if (toResolve == null) {
64 for (int i = 0; i < toResolve.Length; i ++) {
65 string tt = toResolve[i];
69 Code[i].ResolveTarget(TC.Lookup(tt));
74 internal override void Run(CPU cpu)
77 cpu.Enter(Code, NumLocals);
80 internal override List<Word> GetReferences()
83 List<Word> r = new List<Word>();
84 foreach (Opcode op in Code) {
85 Word w = op.GetReference(TC);
93 internal override List<ConstData> GetDataBlocks()
96 List<ConstData> r = new List<ConstData>();
97 foreach (Opcode op in Code) {
98 ConstData cd = op.GetDataBlock(TC);
106 internal override void GenerateCodeElements(List<CodeElement> dst)
110 CodeElement[] gcode = new CodeElement[n];
111 for (int i = 0; i < n; i ++) {
112 gcode[i] = Code[i].ToCodeElement();
114 for (int i = 0; i < n; i ++) {
115 Code[i].FixUp(gcode, i);
117 dst.Add(new CodeElementUInt((uint)NumLocals));
118 for (int i = 0; i < n; i ++) {
127 bool MergeSA(int[] sa, int j, int c)
129 if (sa[j] == Int32.MinValue) {
132 } else if (sa[j] != c) {
133 throw new Exception(string.Format(
134 "In word '{0}', offset {1}:"
135 + " stack action mismatch ({2} / {3})",
142 internal override void AnalyseFlow()
144 switch (flowAnalysis) {
150 throw new Exception("recursive call detected in '"
155 int[] sa = new int[n];
156 for (int i = 0; i < n; i ++) {
157 sa[i] = Int32.MinValue;
160 int[] toExplore = new int[n];
164 int exitSA = Int32.MinValue;
171 Opcode op = Code[off];
172 bool mft = op.MayFallThrough;
175 if (op is OpcodeCall) {
176 Word w = op.GetReference(TC);
178 SType se = w.StackEffect;
180 throw new Exception(string.Format(
181 "call from '{0}' to '{1}'"
182 + " with unknown stack effect",
189 a = se.DataOut - se.DataIn;
191 mds = Math.Max(mds, c + w.MaxDataStack);
192 mrs = Math.Max(mrs, w.MaxReturnStack);
193 maxDepth = Math.Min(maxDepth, c - se.DataIn);
194 } else if (op is OpcodeRet) {
195 if (exitSA == Int32.MinValue) {
197 } else if (exitSA != c) {
198 throw new Exception(string.Format(
199 "'{0}': exit stack action"
200 + " mismatch: {1} / {2}"
202 Name, exitSA, c, off));
207 mds = Math.Max(mds, c + a);
210 maxDepth = Math.Min(maxDepth, c);
215 toExplore[tY ++] = j;
219 if (!mft || !MergeSA(sa, off, c)) {
221 off = toExplore[tX ++];
229 maxReturnStack = 1 + NumLocals + mrs;
232 * TODO: see about this warning. Usage of a 'fail'
233 * word (that does not exit) within a 'case..endcase'
234 * structure will make an unreachable opcode. In a future
235 * version we might want to automatically remove dead
237 for (int i = 0; i < n; i ++) {
238 if (sa[i] == Int32.MinValue) {
239 Console.WriteLine("warning: word '{0}',"
240 + " offset {1}: unreachable opcode",
248 if (exitSA == Int32.MinValue) {
249 computed = new SType(-maxDepth, -1);
251 computed = new SType(-maxDepth, -maxDepth + exitSA);
254 if (StackEffect.IsKnown) {
255 if (!computed.IsSubOf(StackEffect)) {
256 throw new Exception(string.Format(
258 + " computed stack effect {1}"
259 + " does not match declared {2}",
260 Name, computed.ToString(),
261 StackEffect.ToString()));
264 StackEffect = computed;
270 internal override int MaxDataStack {
277 internal override int MaxReturnStack {
280 return maxReturnStack;