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 "word" is a function with a name. Words can be either native or
30 * interpreted; native words are implemented as some in-compiler special
33 * Some native words (not all of them) have a C implementation and can
34 * thus be part of the generated C code. Native words with no C
35 * implementation can be used only during compilation; this is typically
36 * the case for words that support the syntax (e.g. 'if').
42 * The compiler context for this word.
49 * Immediate words are executed immediately when encountered in the
50 * source code, even while compiling another word.
52 internal bool Immediate {
57 * Each word has a unique name. Names are case-sensitive.
59 internal string Name {
64 * Words are allocated slot numbers when output code is generated.
71 * Each word may have a known stack effect.
73 internal SType StackEffect {
77 internal Word(T0Comp owner, string name)
81 StackEffect = SType.UNKNOWN;
85 * Resolving a word means looking up all references to external
88 internal virtual void Resolve()
93 * Execute this word. If the word is native, then its code is
94 * run right away; if the word is interpreted, then the entry
95 * sequence is executed.
97 internal virtual void Run(CPU cpu)
99 throw new Exception(String.Format(
100 "cannot run '{0}' at compile-time", Name));
104 * All words may have an explicit C implementations. To be part
105 * of the generated C code, a word must either be interpreted,
106 * or have an explicit C implementation, or both.
108 internal string CCode {
113 * Get all words referenced from this one. This implies
114 * resolving the word.
116 internal virtual List<Word> GetReferences()
118 return new List<Word>();
122 * Get all data blocks directly referenced from this one. This
123 * implies resolving the word.
125 internal virtual List<ConstData> GetDataBlocks()
127 return new List<ConstData>();
131 * Produce the code elements for this word.
133 internal virtual void GenerateCodeElements(List<CodeElement> dst)
135 throw new Exception("Word does not yield code elements");
139 * Compute/verify stack effect for this word.
141 internal virtual void AnalyseFlow()
146 * Get maximum data stack usage for this word. This is the number
147 * of extra slots that this word may need on the data stack. If
148 * the stack effect is not known, this returns -1.
150 internal virtual int MaxDataStack {
152 SType se = StackEffect;
159 return Math.Min(0, se.DataOut - se.DataIn);
165 * Get maximum return stack usage for this word.
167 internal virtual int MaxReturnStack {