1*0957b409SSimon J. Gerraty /* 2*0957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org> 3*0957b409SSimon J. Gerraty * 4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining 5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the 6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including 7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish, 8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to 9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to 10*0957b409SSimon J. Gerraty * the following conditions: 11*0957b409SSimon J. Gerraty * 12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be 13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software. 14*0957b409SSimon J. Gerraty * 15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*0957b409SSimon J. Gerraty * SOFTWARE. 23*0957b409SSimon J. Gerraty */ 24*0957b409SSimon J. Gerraty 25*0957b409SSimon J. Gerraty using System; 26*0957b409SSimon J. Gerraty using System.Collections.Generic; 27*0957b409SSimon J. Gerraty 28*0957b409SSimon J. Gerraty /* 29*0957b409SSimon J. Gerraty * A "word" is a function with a name. Words can be either native or 30*0957b409SSimon J. Gerraty * interpreted; native words are implemented as some in-compiler special 31*0957b409SSimon J. Gerraty * code. 32*0957b409SSimon J. Gerraty * 33*0957b409SSimon J. Gerraty * Some native words (not all of them) have a C implementation and can 34*0957b409SSimon J. Gerraty * thus be part of the generated C code. Native words with no C 35*0957b409SSimon J. Gerraty * implementation can be used only during compilation; this is typically 36*0957b409SSimon J. Gerraty * the case for words that support the syntax (e.g. 'if'). 37*0957b409SSimon J. Gerraty */ 38*0957b409SSimon J. Gerraty 39*0957b409SSimon J. Gerraty abstract class Word { 40*0957b409SSimon J. Gerraty 41*0957b409SSimon J. Gerraty /* 42*0957b409SSimon J. Gerraty * The compiler context for this word. 43*0957b409SSimon J. Gerraty */ 44*0957b409SSimon J. Gerraty internal T0Comp TC { 45*0957b409SSimon J. Gerraty get; private set; 46*0957b409SSimon J. Gerraty } 47*0957b409SSimon J. Gerraty 48*0957b409SSimon J. Gerraty /* 49*0957b409SSimon J. Gerraty * Immediate words are executed immediately when encountered in the 50*0957b409SSimon J. Gerraty * source code, even while compiling another word. 51*0957b409SSimon J. Gerraty */ 52*0957b409SSimon J. Gerraty internal bool Immediate { 53*0957b409SSimon J. Gerraty get; set; 54*0957b409SSimon J. Gerraty } 55*0957b409SSimon J. Gerraty 56*0957b409SSimon J. Gerraty /* 57*0957b409SSimon J. Gerraty * Each word has a unique name. Names are case-sensitive. 58*0957b409SSimon J. Gerraty */ 59*0957b409SSimon J. Gerraty internal string Name { 60*0957b409SSimon J. Gerraty get; private set; 61*0957b409SSimon J. Gerraty } 62*0957b409SSimon J. Gerraty 63*0957b409SSimon J. Gerraty /* 64*0957b409SSimon J. Gerraty * Words are allocated slot numbers when output code is generated. 65*0957b409SSimon J. Gerraty */ 66*0957b409SSimon J. Gerraty internal int Slot { 67*0957b409SSimon J. Gerraty get; set; 68*0957b409SSimon J. Gerraty } 69*0957b409SSimon J. Gerraty 70*0957b409SSimon J. Gerraty /* 71*0957b409SSimon J. Gerraty * Each word may have a known stack effect. 72*0957b409SSimon J. Gerraty */ 73*0957b409SSimon J. Gerraty internal SType StackEffect { 74*0957b409SSimon J. Gerraty get; set; 75*0957b409SSimon J. Gerraty } 76*0957b409SSimon J. Gerraty Word(T0Comp owner, string name)77*0957b409SSimon J. Gerraty internal Word(T0Comp owner, string name) 78*0957b409SSimon J. Gerraty { 79*0957b409SSimon J. Gerraty TC = owner; 80*0957b409SSimon J. Gerraty Name = name; 81*0957b409SSimon J. Gerraty StackEffect = SType.UNKNOWN; 82*0957b409SSimon J. Gerraty } 83*0957b409SSimon J. Gerraty 84*0957b409SSimon J. Gerraty /* 85*0957b409SSimon J. Gerraty * Resolving a word means looking up all references to external 86*0957b409SSimon J. Gerraty * words. 87*0957b409SSimon J. Gerraty */ Resolve()88*0957b409SSimon J. Gerraty internal virtual void Resolve() 89*0957b409SSimon J. Gerraty { 90*0957b409SSimon J. Gerraty } 91*0957b409SSimon J. Gerraty 92*0957b409SSimon J. Gerraty /* 93*0957b409SSimon J. Gerraty * Execute this word. If the word is native, then its code is 94*0957b409SSimon J. Gerraty * run right away; if the word is interpreted, then the entry 95*0957b409SSimon J. Gerraty * sequence is executed. 96*0957b409SSimon J. Gerraty */ Run(CPU cpu)97*0957b409SSimon J. Gerraty internal virtual void Run(CPU cpu) 98*0957b409SSimon J. Gerraty { 99*0957b409SSimon J. Gerraty throw new Exception(String.Format( 100*0957b409SSimon J. Gerraty "cannot run '{0}' at compile-time", Name)); 101*0957b409SSimon J. Gerraty } 102*0957b409SSimon J. Gerraty 103*0957b409SSimon J. Gerraty /* 104*0957b409SSimon J. Gerraty * All words may have an explicit C implementations. To be part 105*0957b409SSimon J. Gerraty * of the generated C code, a word must either be interpreted, 106*0957b409SSimon J. Gerraty * or have an explicit C implementation, or both. 107*0957b409SSimon J. Gerraty */ 108*0957b409SSimon J. Gerraty internal string CCode { 109*0957b409SSimon J. Gerraty get; set; 110*0957b409SSimon J. Gerraty } 111*0957b409SSimon J. Gerraty 112*0957b409SSimon J. Gerraty /* 113*0957b409SSimon J. Gerraty * Get all words referenced from this one. This implies 114*0957b409SSimon J. Gerraty * resolving the word. 115*0957b409SSimon J. Gerraty */ GetReferences()116*0957b409SSimon J. Gerraty internal virtual List<Word> GetReferences() 117*0957b409SSimon J. Gerraty { 118*0957b409SSimon J. Gerraty return new List<Word>(); 119*0957b409SSimon J. Gerraty } 120*0957b409SSimon J. Gerraty 121*0957b409SSimon J. Gerraty /* 122*0957b409SSimon J. Gerraty * Get all data blocks directly referenced from this one. This 123*0957b409SSimon J. Gerraty * implies resolving the word. 124*0957b409SSimon J. Gerraty */ GetDataBlocks()125*0957b409SSimon J. Gerraty internal virtual List<ConstData> GetDataBlocks() 126*0957b409SSimon J. Gerraty { 127*0957b409SSimon J. Gerraty return new List<ConstData>(); 128*0957b409SSimon J. Gerraty } 129*0957b409SSimon J. Gerraty 130*0957b409SSimon J. Gerraty /* 131*0957b409SSimon J. Gerraty * Produce the code elements for this word. 132*0957b409SSimon J. Gerraty */ GenerateCodeElements(List<CodeElement> dst)133*0957b409SSimon J. Gerraty internal virtual void GenerateCodeElements(List<CodeElement> dst) 134*0957b409SSimon J. Gerraty { 135*0957b409SSimon J. Gerraty throw new Exception("Word does not yield code elements"); 136*0957b409SSimon J. Gerraty } 137*0957b409SSimon J. Gerraty 138*0957b409SSimon J. Gerraty /* 139*0957b409SSimon J. Gerraty * Compute/verify stack effect for this word. 140*0957b409SSimon J. Gerraty */ AnalyseFlow()141*0957b409SSimon J. Gerraty internal virtual void AnalyseFlow() 142*0957b409SSimon J. Gerraty { 143*0957b409SSimon J. Gerraty } 144*0957b409SSimon J. Gerraty 145*0957b409SSimon J. Gerraty /* 146*0957b409SSimon J. Gerraty * Get maximum data stack usage for this word. This is the number 147*0957b409SSimon J. Gerraty * of extra slots that this word may need on the data stack. If 148*0957b409SSimon J. Gerraty * the stack effect is not known, this returns -1. 149*0957b409SSimon J. Gerraty */ 150*0957b409SSimon J. Gerraty internal virtual int MaxDataStack { 151*0957b409SSimon J. Gerraty get { 152*0957b409SSimon J. Gerraty SType se = StackEffect; 153*0957b409SSimon J. Gerraty if (!se.IsKnown) { 154*0957b409SSimon J. Gerraty return -1; 155*0957b409SSimon J. Gerraty } 156*0957b409SSimon J. Gerraty if (se.NoExit) { 157*0957b409SSimon J. Gerraty return 0; 158*0957b409SSimon J. Gerraty } else { 159*0957b409SSimon J. Gerraty return Math.Min(0, se.DataOut - se.DataIn); 160*0957b409SSimon J. Gerraty } 161*0957b409SSimon J. Gerraty } 162*0957b409SSimon J. Gerraty } 163*0957b409SSimon J. Gerraty 164*0957b409SSimon J. Gerraty /* 165*0957b409SSimon J. Gerraty * Get maximum return stack usage for this word. 166*0957b409SSimon J. Gerraty */ 167*0957b409SSimon J. Gerraty internal virtual int MaxReturnStack { 168*0957b409SSimon J. Gerraty get { 169*0957b409SSimon J. Gerraty return 0; 170*0957b409SSimon J. Gerraty } 171*0957b409SSimon J. Gerraty } 172*0957b409SSimon J. Gerraty } 173