├── .gitattributes ├── .github └── workflows │ └── nqc_ci.yml ├── .gitignore ├── CHANGELOG.md ├── Doxyfile ├── LICENSE.md ├── Makefile ├── Makefile.utility ├── PORTING ├── README.md ├── compiler ├── AcquireStmt.cpp ├── AcquireStmt.h ├── AddrOfExpr.cpp ├── AddrOfExpr.h ├── ArrayExpr.cpp ├── ArrayExpr.h ├── AsmStmt.cpp ├── AsmStmt.h ├── AssignMathStmt.cpp ├── AssignMathStmt.h ├── AssignStmt.cpp ├── AssignStmt.h ├── AtomExpr.cpp ├── AtomExpr.h ├── AutoFree.cpp ├── AutoFree.h ├── BinaryExpr.cpp ├── BinaryExpr.h ├── BlockStmt.cpp ├── BlockStmt.h ├── Buffer.cpp ├── Buffer.h ├── Bytecode.cpp ├── Bytecode.h ├── CallStmt.cpp ├── CallStmt.h ├── CaseStmt.cpp ├── CaseStmt.h ├── CatchStmt.cpp ├── CatchStmt.h ├── Compiler.cpp ├── Compiler.h ├── CondParser.cpp ├── CondParser.h ├── Conditional.cpp ├── Conditional.h ├── DeclareStmt.cpp ├── DeclareStmt.h ├── DerefExpr.cpp ├── DerefExpr.h ├── DoStmt.cpp ├── DoStmt.h ├── Error.cpp ├── Error.h ├── EventSrcExpr.cpp ├── EventSrcExpr.h ├── Expansion.cpp ├── Expansion.h ├── Expr.cpp ├── Expr.h ├── ExprStmt.cpp ├── ExprStmt.h ├── ForStmt.cpp ├── ForStmt.h ├── Fragment.cpp ├── Fragment.h ├── FunctionDef.cpp ├── FunctionDef.h ├── GosubParamStmt.cpp ├── GosubParamStmt.h ├── GosubStmt.cpp ├── GosubStmt.h ├── GotoStmt.cpp ├── GotoStmt.h ├── IfStmt.cpp ├── IfStmt.h ├── IncDecExpr.cpp ├── IncDecExpr.h ├── IndirectExpr.cpp ├── IndirectExpr.h ├── InlineStmt.cpp ├── InlineStmt.h ├── JumpStmt.cpp ├── JumpStmt.h ├── LabelStmt.cpp ├── LabelStmt.h ├── LexLocation.h ├── LocationNode.h ├── LogicalExpr.cpp ├── LogicalExpr.h ├── Macro.cpp ├── Macro.h ├── Mapping.cpp ├── Mapping.h ├── ModExpr.cpp ├── ModExpr.h ├── MonitorStmt.cpp ├── MonitorStmt.h ├── NegateExpr.cpp ├── NegateExpr.h ├── NodeExpr.cpp ├── NodeExpr.h ├── PreProc.cpp ├── PreProc.h ├── Program.cpp ├── Program.h ├── RelExpr.cpp ├── RelExpr.h ├── RepeatStmt.cpp ├── RepeatStmt.h ├── Resource.cpp ├── Resource.h ├── Scope.cpp ├── Scope.h ├── ScopeStmt.cpp ├── ScopeStmt.h ├── SensorExpr.cpp ├── SensorExpr.h ├── ShiftExpr.cpp ├── ShiftExpr.h ├── Stmt.cpp ├── Stmt.h ├── SwitchStmt.cpp ├── SwitchStmt.h ├── Symbol.cpp ├── Symbol.h ├── TaskIdExpr.cpp ├── TaskIdExpr.h ├── TaskStmt.cpp ├── TaskStmt.h ├── TernaryExpr.cpp ├── TernaryExpr.h ├── Token.h ├── TypeExpr.cpp ├── TypeExpr.h ├── UnaryExpr.cpp ├── UnaryExpr.h ├── ValueExpr.cpp ├── ValueExpr.h ├── VarAllocator.cpp ├── VarAllocator.h ├── VarTranslator.cpp ├── VarTranslator.h ├── Variable.h ├── WhileStmt.cpp ├── WhileStmt.h ├── build.bat ├── lex.l ├── parse.y ├── parse_util.cpp ├── parse_util.h ├── parser.h ├── rcx1.nqh ├── rcx2.nqh └── version.h ├── default ├── fastdl.srec ├── fastdl_odd.srec ├── lexer.cpp ├── parse.cpp └── parse.tab.h ├── doc ├── NQC_Guide.pdf ├── NQC_Manual.pdf └── rcx.png ├── emscripten ├── emscripten_shell_minimal.html └── webnqc_shell.html ├── mkdata.cbx ├── mkdata.cbx.local ├── mkdata └── mkdata.cpp ├── nqc-man.man ├── nqc.cbx ├── nqc.cbx.local ├── nqc ├── Carbon.r ├── CmdLine.cpp ├── CmdLine.h ├── DirList.cpp ├── DirList.h ├── SRecord.cpp ├── SRecord.h ├── debug_env.c ├── debug_prefix.h ├── nqc.cpp └── version.rc ├── platform ├── LEGOUSBTowerioctl.h ├── PDebug.cpp ├── PDebug.h ├── PHashTable.cpp ├── PHashTable.h ├── PListS.cpp ├── PListS.h ├── PSerial.h ├── PSerial_mac.cpp ├── PSerial_none.cpp ├── PSerial_unix.cpp ├── PSerial_win.cpp ├── PStream.cpp ├── PStream.h ├── PTypes.h ├── StrlUtil.cpp ├── StrlUtil.h └── TowerApi.h ├── rcxlib ├── RCX_Cmd.cpp ├── RCX_Cmd.h ├── RCX_Constants.h ├── RCX_Disasm.cpp ├── RCX_Disasm.h ├── RCX_GhostTransport.cpp ├── RCX_GhostTransport.h ├── RCX_Image.cpp ├── RCX_Image.h ├── RCX_Link.cpp ├── RCX_Link.h ├── RCX_Log.cpp ├── RCX_Log.h ├── RCX_Pipe.cpp ├── RCX_Pipe.h ├── RCX_PipeTransport.cpp ├── RCX_PipeTransport.h ├── RCX_Result.h ├── RCX_SerialPipe.cpp ├── RCX_SerialPipe.h ├── RCX_SourceTag.h ├── RCX_SpyboticsLinker.cpp ├── RCX_SpyboticsLinker.h ├── RCX_Target.cpp ├── RCX_Target.h ├── RCX_TcpPipe_linux.cpp ├── RCX_TcpPipe_none.cpp ├── RCX_Transport.cpp ├── RCX_Transport.h ├── RCX_USBTowerPipe_fbsd.cpp ├── RCX_USBTowerPipe_linux.cpp ├── RCX_USBTowerPipe_none.cpp ├── RCX_USBTowerPipe_osx.cpp ├── RCX_USBTowerPipe_win.cpp ├── README.txt ├── USBTowerWin.cpp ├── USBTowerWin.h ├── fastdl.s ├── nqc_rcxlib.cpp ├── nqc_rcxlib.h └── rcxifile.h ├── rcxspy └── rcxspy.cpp ├── readme.txt ├── readme_macosx.rtf ├── scout.txt └── test.nqc /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behaviour, in case users don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files we want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.c text 7 | *.cpp text 8 | *.h text 9 | *.l text 10 | 11 | # Text, but please do not touch. 12 | *.srec -text 13 | 14 | # Denote all files that are truly binary and should not be modified. 15 | *.png binary 16 | *.jpg binary 17 | *.lgo binary 18 | -------------------------------------------------------------------------------- /.github/workflows/nqc_ci.yml: -------------------------------------------------------------------------------- 1 | name: NQC CI 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | # Following POSIX cron syntax, run every Monday morning at 5:30 AM UTC 7 | # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07 8 | - cron: '30 5 * * 1' 9 | push: 10 | # branches: [ master ] 11 | pull_request: 12 | # branches: [ master ] 13 | 14 | env: 15 | UBUNTU_BUILD_DEPENDS: flex bison emscripten stow 16 | UBUNTU_CROSS_BUILD_DEPENDS: binutils-h8300-hms 17 | 18 | jobs: 19 | build_with_cross_compiler: 20 | name: Build with H8/300 Cross-Compiler 21 | runs-on: ${{ matrix.os }} 22 | strategy: 23 | matrix: 24 | os: [ ubuntu-latest ] 25 | 26 | steps: 27 | - name: Get the latest from version control 28 | uses: actions/checkout@v4 29 | - name: Update build environment 30 | run: sudo apt-get --assume-yes update 31 | - name: Install build dependencies 32 | run: sudo apt-get --assume-yes install $UBUNTU_BUILD_DEPENDS $UBUNTU_CROSS_BUILD_DEPENDS 33 | - name: Execute the build 34 | run: make DEFAULT_SERIAL_NAME='"/dev/ttyS0"' DEFAULT_USB_NAME='"/dev/usb/legousbtower0"' DEFAULT_PORT_NAME='"usb"' 35 | - name: Archive native build artifacts 36 | uses: actions/upload-artifact@v4 37 | with: 38 | name: nqc-native-${{ matrix.os }} 39 | path: build/bin/ 40 | - name: Archive JavaScript-WebAssembly build artifacts 41 | uses: actions/upload-artifact@v4 42 | with: 43 | name: nqc-JS-WebAssembly-${{ matrix.os }} 44 | path: build/js-wasm/ 45 | - name: Install the new build using Stow 46 | run: sudo make stow 47 | 48 | build_without_cross_compiler: 49 | name: Build without H8/300 Cross-Compiler 50 | runs-on: ${{ matrix.os }} 51 | strategy: 52 | matrix: 53 | os: [ ubuntu-latest ] 54 | 55 | steps: 56 | - name: Get the latest from version control 57 | uses: actions/checkout@v4 58 | - name: Update build environment 59 | run: sudo apt-get --assume-yes update 60 | - name: Install build dependencies 61 | run: sudo apt-get --assume-yes install $UBUNTU_BUILD_DEPENDS 62 | - name: Execute the build 63 | run: make DEFAULT_SERIAL_NAME='"/dev/ttyS0"' DEFAULT_USB_NAME='"/dev/usb/legousbtower0"' DEFAULT_PORT_NAME='"usb"' 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OS X Noise 2 | .DS_Store 3 | 4 | # IDE noise 5 | .idea/* 6 | 7 | # RCX specific "binaries" 8 | *.rcx 9 | 10 | # Build output folder 11 | build/* 12 | 13 | # Object files 14 | *.o 15 | 16 | # Language parser intermediates 17 | compiler/lexer.cpp 18 | compiler/parse.cpp 19 | compiler/parse.tab.h 20 | 21 | # Generated docs 22 | doc/html/* 23 | doc/latex/* 24 | doc/*.log 25 | -------------------------------------------------------------------------------- /compiler/AcquireStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "AcquireStmt.h" 15 | #include "Bytecode.h" 16 | #include "Expr.h" 17 | #include "RCX_Cmd.h" 18 | #include "RCX_Target.h" 19 | #include "Error.h" 20 | 21 | AcquireStmt::AcquireStmt(UByte resources, Stmt* body, Stmt *handler, const LexLocation &loc) : 22 | BinaryStmt(body, handler), fResources(resources), fLocation(loc) 23 | { 24 | } 25 | 26 | 27 | void AcquireStmt::EmitActual(Bytecode &b) 28 | { 29 | if (!b.GetTarget()->fResources) { 30 | Error(kErr_NoTargetResources, b.GetTarget()->fName).Raise(&fLocation); 31 | return; 32 | } 33 | 34 | if (!b.BeginHandler(Bytecode::kAcquireHandler)) { 35 | Error(kErr_NoNestedResources).Raise(&fLocation); 36 | return; 37 | } 38 | 39 | RCX_Cmd cmd; 40 | int handlerLabel = b.NewLabel(); 41 | int endLabel = b.NewLabel(); 42 | 43 | // start event monitoring 44 | cmd.Set(kRCX_EnterAccessCtrlOp, fResources, 0, 0); 45 | b.Add(cmd); 46 | b.AddFixup(Bytecode::kSignBitLongFixup, handlerLabel); 47 | 48 | GetPrimary()->Emit(b); 49 | 50 | // end event monitoring 51 | cmd.Set(kRCX_ExitAccessCtrlOp); 52 | b.Add(cmd); 53 | 54 | if (GetSecondary()) 55 | b.AddJump(endLabel); 56 | 57 | // here's the handler 58 | b.SetLabel(handlerLabel); 59 | 60 | if (GetSecondary()) 61 | GetSecondary()->Emit(b); 62 | 63 | b.SetLabel(endLabel); 64 | 65 | b.EndHandler(Bytecode::kAcquireHandler); 66 | } 67 | 68 | 69 | Stmt* AcquireStmt::CloneActual(Mapping *b) const 70 | { 71 | Stmt *s = GetSecondary() ? GetSecondary()->Clone(b) : 0; 72 | return new AcquireStmt(fResources, GetPrimary()->Clone(b), s, fLocation); 73 | } 74 | -------------------------------------------------------------------------------- /compiler/AcquireStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __AcquireStmt_h 15 | #define __AcquireStmt_h 16 | 17 | #ifndef __LexLocation_h 18 | #include "LexLocation.h" 19 | #endif 20 | 21 | #ifndef __Stmt_h 22 | #include "Stmt.h" 23 | #endif 24 | 25 | class AcquireStmt : public BinaryStmt 26 | { 27 | public: 28 | AcquireStmt(UByte resources, Stmt *body, Stmt *handler, const LexLocation &loc); 29 | 30 | void EmitActual(Bytecode &b); 31 | Stmt* CloneActual(Mapping *b) const; 32 | 33 | private: 34 | UByte fResources; 35 | LexLocation fLocation; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /compiler/AddrOfExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "AddrOfExpr.h" 15 | #include "Variable.h" 16 | #include "Mapping.h" 17 | #include "Bytecode.h" 18 | #include "RCX_Target.h" 19 | 20 | AddrOfExpr::AddrOfExpr(int value, int offset, const LexLocation &loc) : 21 | Expr(loc), 22 | fValue(value), 23 | foffset(offset) 24 | { 25 | } 26 | 27 | 28 | bool AddrOfExpr::PromiseConstant() const 29 | { 30 | // if (fValue & kVirtualConstantFlag) return true; 31 | // return false; 32 | return true; 33 | } 34 | 35 | 36 | bool AddrOfExpr::Evaluate(int &value) const 37 | { 38 | if (fValue & kVirtualConstantFlag) return false; 39 | value = fValue+foffset; 40 | return true; 41 | } 42 | 43 | 44 | bool AddrOfExpr::Contains(int var) const 45 | { 46 | return (fValue==var); 47 | } 48 | 49 | 50 | RCX_Value AddrOfExpr::EmitAny_(Bytecode &) const 51 | { 52 | return RCX_VALUE(kRCX_ConstantType, fValue); 53 | } 54 | 55 | 56 | void AddrOfExpr::Translate(int from, int to) 57 | { 58 | if (fValue == from) 59 | fValue = to; 60 | } 61 | 62 | 63 | Expr* AddrOfExpr::Clone(Mapping *m) const 64 | { 65 | if (m && (fValue & kVirtualVarBase)) { 66 | const Expr *e = m->Get(fValue); 67 | if (e) { 68 | Expr *cloned = e->Clone(0); 69 | 70 | // I can't decide if substituted vars from inline functions 71 | // should bind their location to the expr within the function 72 | // or the actual parameter. For actual parameter, do nothing, 73 | // for expr within function, set the location of the cloned 74 | // expression. 75 | cloned->SetLoc(GetLoc()); 76 | return cloned; 77 | } 78 | } 79 | 80 | return new AddrOfExpr(fValue, foffset, GetLoc()); 81 | } 82 | 83 | 84 | RCX_Value AddrOfExpr::GetStaticEA_() const 85 | { 86 | return RCX_VALUE(kRCX_ConstantType, fValue+foffset); 87 | } 88 | 89 | bool AddrOfExpr::LValueIsPointer() const 90 | { 91 | return true; 92 | } 93 | -------------------------------------------------------------------------------- /compiler/AddrOfExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __AddrOfExpr_h 15 | #define __AddrOfExpr_h 16 | 17 | #ifndef __Expr_h 18 | #include "Expr.h" 19 | #endif 20 | 21 | #ifndef __VarTranslator_h 22 | #include "VarTranslator.h" 23 | #endif 24 | 25 | class AddrOfExpr : public Expr, public Translatable 26 | { 27 | public: 28 | AddrOfExpr(int value, int offset, const LexLocation &loc); 29 | 30 | virtual bool PromiseConstant() const; 31 | 32 | virtual bool Evaluate(int &value) const; 33 | virtual Expr* Clone(Mapping *b) const; 34 | 35 | virtual bool Contains(int var) const; 36 | virtual RCX_Value EmitAny_(Bytecode &b) const; 37 | virtual RCX_Value GetStaticEA_() const; 38 | virtual bool LValueIsPointer() const; 39 | 40 | void Translate(int from, int to); 41 | private: 42 | int fValue; 43 | int foffset; 44 | }; 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /compiler/ArrayExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "ArrayExpr.h" 15 | #include "Mapping.h" 16 | #include "RCX_Cmd.h" 17 | #include "Bytecode.h" 18 | #include "Program.h" 19 | 20 | Expr* ArrayExpr::Clone(Mapping *m) const 21 | { 22 | int newVar = m ? m->GetMappedVar(fVar) : fVar; 23 | return new ArrayExpr(newVar, Get(0)->Clone(m)); 24 | } 25 | 26 | 27 | RCX_Value ArrayExpr::EmitAny_(Bytecode &b) const 28 | { 29 | int value; 30 | 31 | if (Get(0)->Evaluate(value)) { 32 | return RCX_VALUE(kRCX_VariableType,fVar + value); 33 | } 34 | 35 | // emit the index 36 | int dst = GetTempVar(b); 37 | if (dst < 0) 38 | return kIllegalEA; 39 | 40 | Get(0)->EmitTo(b, dst); 41 | 42 | // adjust for base of array 43 | if (fVar) { 44 | RCX_Cmd cmd; 45 | cmd.MakeVar(kRCX_AddVar,dst, RCX_VALUE(kRCX_ConstantType,fVar)); 46 | b.Add(cmd); 47 | } 48 | 49 | return RCX_VALUE(kRCX_IndirectType,dst) + kRCX_ValueUsesTemp; 50 | } 51 | 52 | 53 | void ArrayExpr::Translate(int from, int to) 54 | { 55 | if (fVar == from) 56 | fVar = to; 57 | } 58 | -------------------------------------------------------------------------------- /compiler/ArrayExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __ArrayExpr_h 16 | #define __ArrayExpr_h 17 | 18 | #include "NodeExpr.h" 19 | 20 | #ifndef __VarTranslator_h 21 | #include "VarTranslator.h" 22 | #endif 23 | 24 | 25 | class ArrayExpr : public NodeExpr, public Translatable 26 | { 27 | public: 28 | ArrayExpr(int var, Expr *e) : NodeExpr(e), fVar(var) {} 29 | 30 | virtual Expr* Clone(Mapping *b) const; 31 | virtual bool PromiseConstant() const { return false; } 32 | virtual bool PotentialLValue() const { return true; } 33 | 34 | virtual RCX_Value EmitAny_(Bytecode &b) const; 35 | 36 | virtual void Translate(int from, int to); 37 | 38 | private: 39 | int fVar; 40 | }; 41 | 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /compiler/AsmStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __AsmStmt_h 16 | #define __AsmStmt_h 17 | 18 | #ifndef __Stmt_h 19 | #include "Stmt.h" 20 | #endif 21 | 22 | #ifndef __RCX_Constants_h 23 | #include "RCX_Constants.h" 24 | #endif 25 | 26 | #include 27 | 28 | using std::vector; 29 | 30 | class Field; 31 | class Expr; 32 | 33 | 34 | class Field : public PLinkS, public AutoFree 35 | { 36 | public: 37 | Field(Expr *e) : fExpr(e) {} 38 | virtual ~Field(); 39 | 40 | virtual Field* Clone(Mapping *m) const = 0; 41 | virtual void PreEmit(Bytecode &) {}; 42 | virtual void Emit(Bytecode &b, vector &v) const = 0; 43 | 44 | Expr* GetExpr() { return fExpr; } 45 | 46 | protected: 47 | Expr* fExpr; 48 | }; 49 | 50 | 51 | class AsmStmt : public LeafStmt 52 | { 53 | public: 54 | ~AsmStmt(); 55 | 56 | void EmitActual(Bytecode &b); 57 | Stmt* CloneActual(Mapping *b) const; 58 | 59 | void Add(Field *f) { fFields.InsertTail(f); } 60 | 61 | virtual void GetExprs(vector & v) const; 62 | 63 | private: 64 | PListS fFields; 65 | }; 66 | 67 | 68 | 69 | 70 | class ConstField : public Field 71 | { 72 | public: 73 | ConstField(Expr *e) : Field(e) {}; 74 | 75 | virtual Field* Clone(Mapping *m) const; 76 | virtual void Emit(Bytecode &b, vector &v) const; 77 | 78 | private: 79 | }; 80 | 81 | 82 | class EAField : public Field 83 | { 84 | public: 85 | enum { 86 | kSmallOperand = 0x1000000, 87 | kNoTypeCode = 0x2000000, 88 | kNoLocal = 0x4000000, 89 | 90 | kSourceMask = 0x00ffffff 91 | }; 92 | 93 | EAField(Expr *e, ULong restrictor=0); 94 | 95 | virtual Field* Clone(Mapping *m) const; 96 | virtual void PreEmit(Bytecode &b); 97 | virtual void Emit(Bytecode &b, vector &v) const; 98 | 99 | private: 100 | ULong fRestrictor; 101 | RCX_Value fEA; 102 | }; 103 | 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /compiler/AssignMathStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "AssignMathStmt.h" 16 | #include "RCX_Cmd.h" 17 | #include "Bytecode.h" 18 | #include "Expr.h" 19 | #include "Error.h" 20 | 21 | 22 | AssignMathStmt::AssignMathStmt(Expr *lval, RCX_VarCode code, Expr * value) 23 | : AssignStmt(lval, value), 24 | fCode(code) 25 | { 26 | } 27 | 28 | void AssignMathStmt::EmitActual(Bytecode &b) 29 | { 30 | RCX_Value dst = fLval->EmitAny(b); 31 | 32 | if (RCX_VALUE_TYPE(dst) == kRCX_VariableType) { 33 | EmitOperation(b, RCX_VALUE_DATA(dst)); 34 | } 35 | else if (b.GetTarget()->fHasExtendedMathOps && (fCode != kRCX_IllegalVar)) { 36 | // this firmware supports math assignment to generic sources 37 | EmitValueOperation(b, dst); 38 | } else 39 | Error(kErr_NotSupported, "math assignment to non-vars").Raise(&fLval->GetLoc()); 40 | 41 | b.ReleaseTempEA(dst); 42 | } 43 | 44 | 45 | 46 | void AssignMathStmt::EmitOperation(Bytecode &b, int var) 47 | { 48 | RCX_Value ea = fValue->EmitMath(b); 49 | if (ea != Expr::kIllegalEA) { 50 | RCX_Cmd cmd; 51 | cmd.MakeVar(fCode, (UByte)var, ea); 52 | b.Add(cmd); 53 | b.ReleaseTempEA(ea); 54 | } 55 | } 56 | 57 | void AssignMathStmt::EmitValueOperation(Bytecode &b, RCX_Value dst) 58 | { 59 | RCX_Value ea = fValue->EmitMath(b); 60 | if (ea != Expr::kIllegalEA){ 61 | RCX_Cmd cmd; 62 | cmd.MakeSetMath(dst, ea, fCode); 63 | b.Add(cmd); 64 | b.ReleaseTempEA(ea); 65 | } 66 | } 67 | 68 | 69 | Stmt* AssignMathStmt::CloneActual(Mapping *m) const 70 | { 71 | return new AssignMathStmt(fLval->Clone(m), fCode, fValue->Clone(m)); 72 | } 73 | -------------------------------------------------------------------------------- /compiler/AssignMathStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __AssignMathStmt_h 15 | #define __AssignMathStmt_h 16 | 17 | #ifndef __AssignStmt_h 18 | #include "AssignStmt.h" 19 | #endif 20 | 21 | #ifndef __RCX_Constants_h 22 | #include "RCX_Constants.h" 23 | #endif 24 | 25 | class AssignMathStmt : public AssignStmt 26 | { 27 | public: 28 | AssignMathStmt(Expr *lval, RCX_VarCode code, Expr* value); 29 | 30 | void EmitActual(Bytecode &b); 31 | Stmt* CloneActual(Mapping *b) const; 32 | 33 | private: 34 | void EmitOperation(Bytecode &b, int var); 35 | void EmitValueOperation(Bytecode &b, RCX_Value dst); 36 | 37 | RCX_VarCode fCode; 38 | }; 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /compiler/AssignStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "AssignStmt.h" 16 | #include "RCX_Cmd.h" 17 | #include "Bytecode.h" 18 | #include "Expr.h" 19 | #include "RCX_Target.h" 20 | #include "Error.h" 21 | 22 | AssignStmt::AssignStmt(Expr *lval, Expr * value) 23 | : fLval(lval), 24 | fValue(value) 25 | { 26 | } 27 | 28 | 29 | AssignStmt::~AssignStmt() 30 | { 31 | delete fLval; 32 | delete fValue; 33 | } 34 | 35 | 36 | void AssignStmt::EmitActual(Bytecode &b) 37 | { 38 | RCX_Value dst = fLval->EmitAny(b); 39 | int type = RCX_VALUE_TYPE(dst); 40 | 41 | if (type == kRCX_VariableType) 42 | { 43 | fValue->EmitTo(b, RCX_VALUE_DATA(dst)); 44 | } 45 | else 46 | { 47 | if (b.GetTarget()->SourceWritable(type)) 48 | { 49 | RCX_Cmd cmd; 50 | RCX_Value ea = fValue->EmitAny(b); 51 | 52 | cmd.MakeSet(dst, ea); 53 | b.Add(cmd); 54 | 55 | b.ReleaseTempEA(ea); 56 | } 57 | else 58 | Error(kErr_LValueNeeded).Raise(&fLval->GetLoc()); 59 | 60 | } 61 | 62 | b.ReleaseTempEA(dst); // ea may be an array with temp index 63 | } 64 | 65 | 66 | Stmt* AssignStmt::CloneActual(Mapping *m) const 67 | { 68 | return new AssignStmt(fLval->Clone(m), fValue->Clone(m)); 69 | } 70 | 71 | 72 | void AssignStmt::GetExprs(vector &v) const 73 | { 74 | v.push_back(fLval); 75 | v.push_back(fValue); 76 | } 77 | -------------------------------------------------------------------------------- /compiler/AssignStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __AssignStmt_h 15 | #define __AssignStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | class Expr; 22 | 23 | class AssignStmt : public LeafStmt 24 | { 25 | public: 26 | AssignStmt(Expr *lval, Expr* value); 27 | ~AssignStmt(); 28 | 29 | void EmitActual(Bytecode &b); 30 | Stmt* CloneActual(Mapping *b) const; 31 | 32 | virtual void GetExprs(vector & v) const; 33 | 34 | protected: 35 | Expr* fLval; 36 | Expr* fValue; 37 | }; 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /compiler/AtomExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __AtomExpr_h 16 | #define __AtomExpr_h 17 | 18 | #ifndef __Expr_h 19 | #include "Expr.h" 20 | #endif 21 | 22 | #ifndef __VarTranslator_h 23 | #include "VarTranslator.h" 24 | #endif 25 | 26 | class AtomExpr : public Expr, public Translatable 27 | { 28 | public: 29 | AtomExpr(RCX_ValueType type, int value, const LexLocation &loc, bool ptr = false); 30 | 31 | 32 | virtual bool PromiseConstant() const; 33 | 34 | virtual bool Evaluate(int &value) const; 35 | virtual Expr* Clone(Mapping *b) const; 36 | 37 | virtual bool PotentialLValue() const; 38 | virtual int GetLValue() const; 39 | virtual bool LValueIsPointer() const; 40 | 41 | virtual bool Contains(int var) const; 42 | virtual RCX_Value EmitAny_(Bytecode &b) const; 43 | virtual RCX_Value GetStaticEA_() const; 44 | 45 | void Translate(int from, int to); 46 | private: 47 | RCX_ValueType fType; 48 | int fValue; 49 | bool fPtr; 50 | }; 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /compiler/AutoFree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "AutoFree.h" 15 | 16 | AutoFreeGroup& GetAutoFreeGroup() 17 | { 18 | static AutoFreeGroup afGroup; 19 | 20 | return afGroup; 21 | } 22 | 23 | 24 | AutoFreeGroup::AutoFreeGroup() 25 | { 26 | head_.next_ = &tail_; 27 | head_.prev_ = 0; 28 | tail_.next_ = 0; 29 | tail_.prev_ = &head_; 30 | } 31 | 32 | 33 | void* AutoFreeGroup::allocate(size_t n) 34 | { 35 | // allocate memory plus overhead for link 36 | Link *link = (Link *) ::operator new(n + sizeof(Link)); 37 | 38 | // link memory into pool list 39 | link->next_ = head_.next_; 40 | link->prev_ = &head_; 41 | link->next_->prev_ = link; 42 | link->prev_->next_ = link; 43 | 44 | // return pointer to memory past the link 45 | return (void *)((char *)link + sizeof(Link)); 46 | } 47 | 48 | 49 | void AutoFreeGroup::free(void *ptr) 50 | { 51 | if (ptr==0) return; 52 | 53 | // determine pointer to link 54 | Link *link = (Link *)((char *)ptr - sizeof(Link)); 55 | 56 | dispose(link); 57 | } 58 | 59 | 60 | void AutoFreeGroup::dispose(Link *link) 61 | { 62 | // unlink memory from pool list 63 | link->prev_->next_ = link->next_; 64 | link->next_->prev_ = link->prev_; 65 | 66 | // free the memory 67 | ::operator delete((void*)link); 68 | } 69 | 70 | 71 | void AutoFreeGroup::freeAll() 72 | { 73 | Link *link; 74 | 75 | while((link = head_.next_)->next_ != 0) { 76 | dispose(link); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /compiler/AutoFree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __AutoFree_h 15 | #define __AutoFree_h 16 | 17 | #include 18 | 19 | using std::size_t; 20 | 21 | class AutoFreeGroup 22 | { 23 | public: 24 | AutoFreeGroup(); 25 | 26 | /// Called by ::new operator 27 | void* allocate(size_t); 28 | //// Called by ::delete operator 29 | void free(void*); 30 | 31 | /// Call this to release all remaining objects 32 | void freeAll(); 33 | 34 | private: 35 | struct Link 36 | { 37 | Link* next_; 38 | Link* prev_; 39 | }; 40 | 41 | // internal call to unlink and dispose an item 42 | void dispose(Link *link); 43 | 44 | 45 | Link head_; 46 | Link tail_; 47 | }; 48 | 49 | 50 | extern AutoFreeGroup& GetAutoFreeGroup(); 51 | 52 | class AutoFree 53 | { 54 | public: 55 | void* operator new(size_t n) { return GetAutoFreeGroup().allocate(n); } 56 | void operator delete(void* ptr, size_t) { GetAutoFreeGroup().free(ptr); } 57 | void* operator new[](size_t n) { return GetAutoFreeGroup().allocate(n); } 58 | void operator delete[](void * ptr, size_t) { GetAutoFreeGroup().free(ptr); } 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /compiler/BinaryExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __BinaryExpr_h 15 | #define __BinaryExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class BinaryExpr : public NodeExpr 20 | { 21 | public: 22 | BinaryExpr(Expr *lhs, int op, Expr *rhs); 23 | 24 | virtual bool Evaluate(int &value) const; 25 | virtual Expr* Clone(Mapping *b) const; 26 | 27 | virtual RCX_Value EmitAny_(Bytecode &b) const; 28 | virtual bool EmitTo_(Bytecode &b, int dst) const; 29 | 30 | static bool NeedsConstant(int op); 31 | 32 | private: 33 | int fOp; 34 | }; 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /compiler/BlockStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "BlockStmt.h" 15 | 16 | BlockStmt::BlockStmt() 17 | { 18 | } 19 | 20 | 21 | BlockStmt::~BlockStmt() 22 | { 23 | Stmt *s; 24 | 25 | while ((s = fList.RemoveHead()) != nil) 26 | delete s; 27 | } 28 | 29 | 30 | Stmt* BlockStmt::GetChildren() 31 | { 32 | return fList.GetHead(); 33 | } 34 | 35 | 36 | void BlockStmt::Add(Stmt *s) 37 | { 38 | if (s) { 39 | fList.InsertTail(s); 40 | Adopt(s); 41 | } 42 | } 43 | 44 | 45 | void BlockStmt::Prepend(Stmt *s) 46 | { 47 | if (s) { 48 | fList.InsertHead(s); 49 | Adopt(s); 50 | } 51 | } 52 | 53 | 54 | void BlockStmt::EmitActual(Bytecode &b) 55 | { 56 | for(Stmt *s = fList.GetHead(); s; s = s->GetNext()) { 57 | s->Emit(b); 58 | } 59 | } 60 | 61 | 62 | Stmt* BlockStmt::CloneActual(Mapping *b) const 63 | { 64 | BlockStmt *block = new BlockStmt; 65 | 66 | for(Stmt *s = fList.GetHead(); s; s = s->GetNext()) { 67 | Stmt *child = s->Clone(b); 68 | block->fList.InsertTail(child); 69 | block->Adopt(child); 70 | } 71 | 72 | return block; 73 | } 74 | -------------------------------------------------------------------------------- /compiler/BlockStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __BlockStmt_h 15 | #define __BlockStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | 22 | class BlockStmt : public Stmt 23 | { 24 | public: 25 | BlockStmt(); 26 | ~BlockStmt(); 27 | 28 | virtual Stmt* GetChildren(); 29 | 30 | void Add(Stmt *s); 31 | void Prepend(Stmt *s); 32 | const Stmt* GetHead() const { return fList.GetHead(); } 33 | Stmt* GetHead() { return fList.GetHead(); } 34 | Stmt* RemoveHead() { return fList.RemoveHead(); } 35 | 36 | virtual void EmitActual(Bytecode &b); 37 | virtual Stmt* CloneActual(Mapping *b) const; 38 | 39 | private: 40 | PListS fList; 41 | }; 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /compiler/Buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef _STDIO_H 15 | #include 16 | using std::FILE; 17 | #endif 18 | 19 | class Buffer 20 | { 21 | public: 22 | Buffer(); 23 | virtual ~Buffer(); 24 | 25 | bool Create(const char *name, const char *pathname); 26 | void Create(const char *name, FILE *fp); 27 | void Create(const char *name, const char *data, int length); 28 | 29 | const char* GetName() const { return fName; } 30 | int GetLength() const { return fLength; } 31 | const char * GetData() const { return fData; } 32 | 33 | const char * GetLine(int line) const; 34 | 35 | int FindLine(int &offset) const; 36 | 37 | int FindStartOfLine(int offset) const; 38 | int FindEndOfLine(int offset) const; 39 | 40 | protected: 41 | void FinishCreate(const char *name); 42 | void ConvertLineEndings(); 43 | 44 | char* fName; 45 | char* fData; 46 | int fLength; 47 | }; 48 | -------------------------------------------------------------------------------- /compiler/CallStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __CallStmt_h 15 | #define __CallStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | #include 26 | 27 | using std::vector; 28 | 29 | class Symbol; 30 | class Expr; 31 | 32 | class CallStmt : public ChainStmt 33 | { 34 | public: 35 | CallStmt(); 36 | ~CallStmt(); 37 | 38 | void EmitActual(Bytecode &b); 39 | Stmt* CloneActual(Mapping *b) const; 40 | 41 | virtual void GetExprs(vector & v) const; 42 | 43 | void SetName(const Symbol *name) { fName = name; } 44 | void SetLocation(const struct LexLocation &loc) { fLocation = loc; } 45 | void AddParam(Expr *c) { fParams.push_back(c); } 46 | 47 | void Expand(Fragment *f); 48 | 49 | class Expander 50 | { 51 | public: 52 | Expander(Fragment *f) : fFragment(f) {} 53 | bool operator()(Stmt *s); 54 | 55 | private: 56 | Fragment* fFragment; 57 | }; 58 | 59 | private: 60 | 61 | void ExpandFunction(FunctionDef *func, Fragment *fragment); 62 | 63 | const Symbol* fName; 64 | struct LexLocation fLocation; 65 | vector fParams; 66 | }; 67 | 68 | 69 | 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /compiler/CaseStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "CaseStmt.h" 15 | #include "Bytecode.h" 16 | #include "Error.h" 17 | #include "SwitchStmt.h" 18 | 19 | CaseStmt::CaseStmt(int v, const LexLocation &loc, Stmt *s) : 20 | ChainStmt(s), 21 | fValue(v), 22 | fLocation(loc), 23 | fLabel(kIllegalLabel) 24 | { 25 | } 26 | 27 | 28 | CaseStmt::~CaseStmt() 29 | { 30 | } 31 | 32 | 33 | void CaseStmt::EmitActual(Bytecode &b) 34 | { 35 | if (fLabel == kIllegalLabel) 36 | { 37 | Error(kErr_NoSwitch).Raise(&fLocation); 38 | return; 39 | } 40 | 41 | b.SetLabel(fLabel); 42 | 43 | GetBody()->Emit(b); 44 | } 45 | 46 | 47 | void CaseStmt::EmitSwitchCases(Bytecode &b, SwitchState &state) 48 | { 49 | // check for duplicate case 50 | if (state.ContainsCase(fValue)) 51 | { 52 | Error(kErr_DuplicateCase).Raise(&fLocation); 53 | } 54 | 55 | // add the case 56 | state.AddCase(fValue); 57 | 58 | if (fValue == kDefaultValue) 59 | { 60 | // don't emit a test - just remember this is the default case 61 | fLabel = state.GetDefaultLabel(); 62 | } 63 | else 64 | { 65 | // create label for the case and emit the test 66 | fLabel = b.NewLabel(); 67 | b.AddTest(RCX_VALUE(kRCX_ConstantType, fValue), kRCX_EqualTo, state.GetSelector(), fLabel); 68 | } 69 | } 70 | 71 | 72 | Stmt* CaseStmt::CloneActual(Mapping *m) const 73 | { 74 | return new CaseStmt(fValue, fLocation, GetBody()->Clone(m)); 75 | } 76 | -------------------------------------------------------------------------------- /compiler/CaseStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | /* 16 | * CaseStmts are used to bind a case label to another statement. The 17 | * CaseStmt isn't really a statement itself - just a wrapper for 18 | * a single child statement. 19 | * 20 | * Upon construction CaseStmts are assigned their value and location. 21 | * During code generation of a switch statement, actual labels are 22 | * assigned to the CaseStmts within the switch. Code generation for 23 | * CaseStmt itself only consists of setting the designated label to the 24 | * current bytecode locaiton. 25 | * 26 | */ 27 | 28 | #ifndef __CaseStmt_h 29 | #define __CaseStmt_h 30 | 31 | #ifndef __Stmt_h 32 | #include "Stmt.h" 33 | #endif 34 | 35 | #ifndef __parser_h 36 | #include "parser.h" 37 | #endif 38 | 39 | class SwitchState; 40 | 41 | class CaseStmt : public ChainStmt 42 | { 43 | public: 44 | enum 45 | { 46 | kDefaultValue = 0x10000 // value for 'default:' 47 | }; 48 | 49 | CaseStmt(int v, const LexLocation &loc, Stmt *s = 0); 50 | ~CaseStmt(); 51 | 52 | void SetStmt(Stmt *s) { SetBody(s); } 53 | 54 | virtual void EmitActual(Bytecode &b); 55 | virtual Stmt* CloneActual(Mapping *b) const; 56 | 57 | void EmitSwitchCases(Bytecode &b, SwitchState &s); 58 | 59 | private: 60 | int fValue; 61 | LexLocation fLocation; 62 | int fLabel; 63 | }; 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /compiler/CatchStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "CatchStmt.h" 15 | #include "Bytecode.h" 16 | #include "Error.h" 17 | #include "RCX_Cmd.h" 18 | #include "RCX_Target.h" 19 | #include "Variable.h" 20 | 21 | CatchStmt::CatchStmt(int v, Stmt *s, const LexLocation &loc) : 22 | ChainStmt(s), 23 | fValue(v), 24 | fLocation(loc) 25 | { 26 | } 27 | 28 | 29 | CatchStmt::~CatchStmt() 30 | { 31 | } 32 | 33 | 34 | void CatchStmt::EmitActual(Bytecode &b) 35 | { 36 | if ((b.GetTarget()->fType != kRCX_RCX2Target) && (b.GetTarget()->fType != kRCX_SwanTarget)) 37 | { 38 | Error(kErr_NoTargetPartialCatch, b.GetTarget()->fName).Raise(&fLocation); 39 | return; 40 | } 41 | 42 | int var = b.GetTempVar(true); 43 | 44 | if (var == kIllegalVar) 45 | { 46 | Error(kErr_NoMoreTemps).Raise(&fLocation); 47 | return; 48 | } 49 | 50 | RCX_Value ea = RCX_VALUE(kRCX_VariableType, var); 51 | 52 | b.ReleaseTempEA(ea); 53 | 54 | // move active events into temp 55 | b.AddMove(var, RCX_VALUE(0x17, 0x0a)); 56 | 57 | // and with mask 58 | RCX_Cmd cmd; 59 | cmd.MakeVar(kRCX_AndVar, var, RCX_VALUE(kRCX_ConstantType, fValue)); 60 | b.Add(cmd); 61 | 62 | // jump if zero 63 | b.AddTest(RCX_VALUE(kRCX_ConstantType, 0), kRCX_EqualTo, RCX_VALUE(kRCX_VariableType, var), fLabel); 64 | 65 | GetBody()->Emit(b); 66 | } 67 | 68 | 69 | Stmt* CatchStmt::CloneActual(Mapping *m) const 70 | { 71 | return new CatchStmt(fValue, GetBody()->Clone(m), fLocation); 72 | } 73 | -------------------------------------------------------------------------------- /compiler/CatchStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __CatchStmt_h 16 | #define __CatchStmt_h 17 | 18 | #ifndef __Stmt_h 19 | #include "Stmt.h" 20 | #endif 21 | 22 | #ifndef __parser_h 23 | #include "parser.h" 24 | #endif 25 | 26 | class SwitchState; 27 | 28 | class CatchStmt : public ChainStmt 29 | { 30 | public: 31 | CatchStmt(int v, Stmt *s, const LexLocation &loc); 32 | ~CatchStmt(); 33 | 34 | virtual void EmitActual(Bytecode &b); 35 | virtual Stmt* CloneActual(Mapping *b) const; 36 | 37 | void SetLabel(int l) { fLabel = l; } 38 | 39 | private: 40 | int fValue; 41 | LexLocation fLocation; 42 | int fLabel; 43 | }; 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /compiler/Compiler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Compiler_h 15 | #define __Compiler_h 16 | 17 | #include 18 | 19 | #include "RCX_Constants.h" 20 | #include "RCX_Target.h" 21 | #include "RCX_Disasm.h" 22 | 23 | using std::vector; 24 | 25 | class RCX_Image; 26 | class Buffer; 27 | 28 | class Compiler : public RCX_SourceFiles 29 | { 30 | public: 31 | enum 32 | { 33 | kCompat_Flag = 1 << 0, 34 | kNoSysFile_Flag = 1 << 2 35 | }; 36 | 37 | Compiler() { sCompiler = this; fDirty = false; } 38 | virtual ~Compiler() {} 39 | 40 | static Compiler* Get() { return sCompiler; } 41 | 42 | void Reset(); 43 | RCX_Image * Compile(Buffer *buffer, const RCX_Target *target, int flags); 44 | 45 | void Define(const char *name, const char *value=0); 46 | void Undefine(const char *name); 47 | 48 | // hooks for the lexer 49 | int AddBuffer(Buffer *b); 50 | Buffer* GetBuffer(int i) { return fBuffers[i]; } 51 | 52 | virtual Buffer *CreateBuffer(const char *name) = 0; 53 | 54 | // access to system API buffer 55 | static Buffer* CreateApiBuffer(bool compatMode); 56 | 57 | // used to insert source code into assembly listings (from RCX_SourceFiles) 58 | virtual long Print(RCX_Printer *printer, short index, long start, long end); 59 | 60 | private: 61 | void ReleaseBuffers(); 62 | 63 | vector fBuffers; 64 | bool fDirty; 65 | 66 | static Compiler* sCompiler; 67 | }; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /compiler/CondParser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __CondParser_h 15 | #define __CondParser_h 16 | 17 | #ifndef __Token_h 18 | #include "Token.h" 19 | #endif 20 | 21 | #ifndef __PTypes_h 22 | #include "PTypes.h" 23 | #endif 24 | 25 | class Symbol; 26 | 27 | class CondParser 28 | { 29 | public: 30 | CondParser(); 31 | ~CondParser(); 32 | 33 | bool Parse(long &value); 34 | 35 | private: 36 | enum State 37 | { 38 | kValueState, 39 | kOpState 40 | }; 41 | 42 | void Init(); 43 | bool DoValueState(int t, TokenVal v); 44 | bool DoOpState(int t); 45 | bool ParseDefined(long &value); 46 | 47 | void ReduceUnaries(long x); 48 | void Reduce(int limit=0); 49 | 50 | void Push(long x); 51 | long Pop() { return fStack[--fDepth]; } 52 | void Drop(int n) { fDepth -= n; } 53 | 54 | State fState; 55 | int fUnaryCount; 56 | int fParen; 57 | int fDepth; 58 | int fMaxDepth; 59 | long* fStack; 60 | Symbol* fDefinedSymbol; 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /compiler/Conditional.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "Conditional.h" 15 | 16 | Conditional::Conditional() 17 | { 18 | Level *l = new Level; 19 | 20 | l->fState = Level::kActive; 21 | l->fElseAllowed = false; 22 | l->fTopLevel = true; 23 | 24 | fLevels.InsertHead(l); 25 | } 26 | 27 | 28 | Conditional::~Conditional() 29 | { 30 | Level *l; 31 | while((l = fLevels.RemoveHead()) != nil) 32 | delete l; 33 | } 34 | 35 | 36 | bool Conditional::Elif(bool b) 37 | { 38 | Level *l = GetLevel(); 39 | 40 | if (!l->fElseAllowed) return false; 41 | 42 | if (l->fState == Level::kPending) 43 | { 44 | l->fState = (b ? Level::kActive : Level::kPending); 45 | } 46 | else 47 | l->fState = Level::kInactive; 48 | 49 | return true; 50 | } 51 | 52 | 53 | void Conditional::If(bool b) 54 | { 55 | Level *l = new Level; 56 | Level::State state; 57 | 58 | if (IsActive()) 59 | { 60 | if (b) 61 | state = Level::kActive; 62 | else 63 | state = Level::kPending; 64 | } 65 | else 66 | state = Level::kInactive; 67 | 68 | l->fState = state; 69 | l->fElseAllowed = true; 70 | l->fTopLevel = false; 71 | 72 | fLevels.InsertHead(l); 73 | } 74 | 75 | 76 | bool Conditional::Else() 77 | { 78 | Level *l = GetLevel(); 79 | 80 | if (!l->fElseAllowed) return false; 81 | 82 | l->fElseAllowed = false; 83 | switch(l->fState) 84 | { 85 | case Level::kActive: 86 | l->fState = Level::kInactive; 87 | break; 88 | case Level::kPending: 89 | l->fState = Level::kActive; 90 | break; 91 | default: 92 | // no change for inactive level 93 | break; 94 | } 95 | 96 | return true; 97 | } 98 | 99 | 100 | bool Conditional::Endif() 101 | { 102 | Level *l; 103 | 104 | if (IsTopLevel()) return false; 105 | 106 | l = fLevels.RemoveHead(); 107 | delete l; 108 | 109 | return true; 110 | } 111 | -------------------------------------------------------------------------------- /compiler/Conditional.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Conditional_h 15 | #define __Conditional_h 16 | 17 | #ifndef __PListS_h 18 | #include "PListS.h" 19 | #endif 20 | 21 | class Conditional 22 | { 23 | public: 24 | Conditional(); 25 | ~Conditional(); 26 | 27 | bool IsActive() { return (GetLevel()->fState == Level::kActive) ? true : false; } 28 | bool IsTopLevel() { return GetLevel()->fTopLevel; } 29 | 30 | void If(bool b); 31 | bool Elif(bool b); 32 | bool Else(); 33 | bool Endif(); 34 | 35 | private: 36 | class Level : public PLinkS 37 | { 38 | public: 39 | // states for a level 40 | enum State 41 | { 42 | kInactive, 43 | kActive, 44 | kPending 45 | }; 46 | 47 | State fState; // see above 48 | bool fElseAllowed; // true until else is done 49 | bool fTopLevel; // only true for the top level 50 | }; 51 | 52 | Level* GetLevel() { return fLevels.GetHead(); } 53 | 54 | PListSS fLevels; 55 | }; 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /compiler/DeclareStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "DeclareStmt.h" 15 | #include "AssignStmt.h" 16 | #include "ScopeStmt.h" 17 | #include "Variable.h" 18 | #include "Bytecode.h" 19 | #include "Error.h" 20 | #include "AtomExpr.h" 21 | #include "Symbol.h" 22 | 23 | 24 | DeclareStmt::DeclareStmt(const Symbol *name, int var, const struct LexLocation &loc, int count, bool ptr, bool stack) : 25 | ChainStmt(), 26 | fName(name), 27 | fVar(var), 28 | fScope(0), 29 | fLoc(loc), 30 | fCount(count), 31 | fPtr(ptr), 32 | fStack(stack) 33 | { 34 | } 35 | 36 | 37 | DeclareStmt::~DeclareStmt() 38 | { 39 | } 40 | 41 | 42 | 43 | void DeclareStmt::SetInitialValue(Expr *e) 44 | { 45 | SetBody(new AssignStmt(new AtomExpr(kRCX_VariableType, fVar, e->GetLoc()), e)); 46 | } 47 | 48 | 49 | void DeclareStmt::EmitActual(Bytecode &b) 50 | { 51 | if (fVar & kVirtualVarBase && fScope) 52 | { 53 | int physical = b.GetVarAllocator().Allocate(false, true, fCount); 54 | if (physical == kIllegalVar) 55 | { 56 | Error(kErr_NoMoreVars).Raise(&fLoc); 57 | } 58 | 59 | fScope->RemapVar(fVar, physical, fCount); 60 | 61 | // add symbolic information 62 | if (fName) 63 | b.AddVariableName(physical, fName->GetKey()); 64 | } 65 | 66 | Stmt *body = GetBody(); 67 | if (body) body->Emit(b); 68 | } 69 | 70 | 71 | Stmt* DeclareStmt::CloneActual(Mapping *b) const 72 | { 73 | DeclareStmt *s = new DeclareStmt(fName, fVar, fLoc, fCount, fPtr, fStack); 74 | if (GetBody()) 75 | s->SetBody(GetBody()->Clone(b)); 76 | 77 | return s; 78 | } 79 | 80 | 81 | bool DeclareStmt::Binder::operator()(Stmt *s) 82 | { 83 | if (ScopeStmt *ss = dynamic_cast(s)) 84 | { 85 | // recurse into scopes with new Binder, then return false 86 | Binder b(ss); 87 | Apply(ss->GetBody(),b); 88 | return false; 89 | } 90 | 91 | // bind declarations to scopes 92 | if (DeclareStmt *ds = dynamic_cast(s)) 93 | { 94 | ds->fScope = fScope; 95 | } 96 | 97 | return true; 98 | } 99 | -------------------------------------------------------------------------------- /compiler/DeclareStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __DeclareStmt_h 15 | #define __DeclareStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __LexLocation_h 22 | #include "LexLocation.h" 23 | #endif 24 | 25 | class Symbol; 26 | class Expr; 27 | class ScopeStmt; 28 | 29 | class DeclareStmt : public ChainStmt 30 | { 31 | public: 32 | DeclareStmt(const Symbol *name, int var, const struct LexLocation &loc, int count, bool ptr, bool stack); 33 | ~DeclareStmt(); 34 | 35 | void SetInitialValue(Expr *e); 36 | 37 | int GetVar() const { return fVar; } 38 | const LexLocation& GetLoc() const { return fLoc; } 39 | int GetCount() const { return fCount; } 40 | const Symbol* GetName() const { return fName; } 41 | bool GetPointer() const { return fPtr; } 42 | bool GetStack() const { return fStack; } 43 | 44 | void EmitActual(Bytecode &b); 45 | Stmt* CloneActual(Mapping *b) const; 46 | 47 | // Binder is a function object that binds declarations to a scope 48 | class Binder 49 | { 50 | public: 51 | Binder(ScopeStmt *scope) : fScope(scope) {} 52 | bool operator()(Stmt *s); 53 | 54 | private: 55 | ScopeStmt* fScope; 56 | }; 57 | 58 | friend class Binder; 59 | 60 | private: 61 | const Symbol* fName; 62 | int fVar; 63 | ScopeStmt* fScope; 64 | struct LexLocation fLoc; 65 | int fCount; 66 | bool fPtr; 67 | bool fStack; 68 | }; 69 | 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /compiler/DerefExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __DerefExpr_h 16 | #define __DerefExpr_h 17 | 18 | #ifndef __Expr_h 19 | #include "Expr.h" 20 | #endif 21 | 22 | #ifndef __VarTranslator_h 23 | #include "VarTranslator.h" 24 | #endif 25 | 26 | class DerefExpr : public Expr, public Translatable 27 | { 28 | public: 29 | DerefExpr(int value, const LexLocation &loc); 30 | 31 | virtual bool PromiseConstant() const; 32 | 33 | virtual bool Evaluate(int &value) const; 34 | virtual Expr* Clone(Mapping *b) const; 35 | 36 | virtual bool PotentialLValue() const; 37 | virtual int GetLValue() const; 38 | 39 | virtual bool Contains(int var) const; 40 | virtual RCX_Value EmitAny_(Bytecode &b) const; 41 | virtual RCX_Value GetStaticEA_() const; 42 | 43 | void Translate(int from, int to); 44 | private: 45 | int fValue; 46 | }; 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /compiler/DoStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "DoStmt.h" 15 | #include "Bytecode.h" 16 | #include "JumpStmt.h" 17 | 18 | DoStmt::DoStmt(Expr *c, Stmt *s) : 19 | ChainStmt(s) 20 | { 21 | fCondition = c; 22 | } 23 | 24 | 25 | DoStmt::~DoStmt() 26 | { 27 | delete fCondition; 28 | } 29 | 30 | 31 | void DoStmt::EmitActual(Bytecode &b) 32 | { 33 | /* 34 | sLabel: 35 | fBody 36 | cLabel: 37 | test -> sLabel 38 | bLabel: 39 | */ 40 | 41 | int cLabel = b.PushFlow(Bytecode::kContinueFlow); 42 | int bLabel = b.PushFlow(Bytecode::kBreakFlow); 43 | int sLabel = b.NewLabel(); 44 | 45 | GetBody()->Emit(b); 46 | b.SetLabel(cLabel); 47 | 48 | fCondition->EmitBranch(b, sLabel, true); 49 | 50 | b.SetLabel(bLabel); 51 | b.PopFlow(Bytecode::kContinueFlow); 52 | b.PopFlow(Bytecode::kBreakFlow); 53 | } 54 | 55 | Stmt* DoStmt::CloneActual(Mapping *b) const 56 | { 57 | return new DoStmt(fCondition->Clone(b), GetBody()->Clone(b)); 58 | } 59 | -------------------------------------------------------------------------------- /compiler/DoStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __DoStmt_h 15 | #define __DoStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __Expr_h 22 | #include "Expr.h" 23 | #endif 24 | 25 | class DoStmt : public ChainStmt 26 | { 27 | public: 28 | DoStmt(Expr *c, Stmt *s); 29 | ~DoStmt(); 30 | 31 | void EmitActual(Bytecode &b); 32 | Stmt* CloneActual(Mapping *b) const; 33 | 34 | virtual void GetExprs(vector & v) const { v.push_back(fCondition); } 35 | 36 | private: 37 | Expr* fCondition; 38 | }; 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /compiler/EventSrcExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __EventSrcExpr_h 16 | #define __EventSrcExpr_h 17 | 18 | #include "NodeExpr.h" 19 | #include "RCX_Target.h" 20 | 21 | /** 22 | * This class is only partially implemented. Specifically, evaluation always 23 | * fails even though in theory the value will be constant. In order to fix this 24 | * some other virtual such as Expr::GetEA() would need to be added. 25 | */ 26 | class EventSrcExpr : public NodeExpr 27 | { 28 | public: 29 | EventSrcExpr(Expr *e, RCX_TargetType targetType) : NodeExpr(e), fTargetType(targetType) {} 30 | 31 | virtual Expr* Clone(Mapping *b) const; 32 | 33 | virtual RCX_Value EmitAny_(Bytecode &b) const; 34 | 35 | virtual bool PromiseConstant() const { return true; } 36 | virtual bool Evaluate(int &) const; 37 | 38 | private: 39 | RCX_TargetType fTargetType; 40 | }; 41 | 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /compiler/Expansion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "Expansion.h" 16 | #include "Macro.h" 17 | #include "parser.h" 18 | #include "Symbol.h" 19 | 20 | using std::memcpy; 21 | 22 | Expansion::Expansion(Macro *def) 23 | { 24 | fDef = def; 25 | fDef->SetMark(); 26 | fTokens = def->GetTokens(); 27 | fEnd = fTokens + def->GetTokenCount(); 28 | fArgCount = def->GetArgCount(); 29 | if (fArgCount > 0) 30 | { 31 | fArgs = new Token*[fArgCount]; 32 | fArgLengths = new int[fArgCount]; 33 | for(int i=0; ifArgs[i]; 51 | fEnd = fTokens + e->fArgLengths[i]; 52 | fArgCount = 0; 53 | fArgs = nil; 54 | fArgLengths = nil; 55 | } 56 | 57 | 58 | Expansion::~Expansion() 59 | { 60 | if (fDef) fDef->ClearMark(); 61 | if (fArgs) 62 | { 63 | for(int i=0; ifType; 76 | v = fTokens->fValue; 77 | 78 | fTokens++; 79 | return t; 80 | } 81 | 82 | 83 | void Expansion::SetArg(int i, const Token *t, int count) 84 | { 85 | Token *tokens = new Token[count]; 86 | memcpy(tokens, t, count * sizeof(Token)); 87 | 88 | fArgs[i] = tokens; 89 | fArgLengths[i] = count; 90 | } 91 | -------------------------------------------------------------------------------- /compiler/Expansion.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Expansion_h 15 | #define __Expansion_h 16 | 17 | #ifndef __Token_h 18 | #include "Token.h" 19 | #endif 20 | 21 | #ifndef __PListS_h 22 | #include "PListS.h" 23 | #endif 24 | 25 | class Macro; 26 | 27 | class Expansion : public PLinkS { 28 | public: 29 | Expansion(Macro *def); 30 | Expansion(const Expansion *e, int i); 31 | 32 | ~Expansion(); 33 | 34 | int NextToken(TokenVal &v); 35 | bool IsDone() const { return fTokens==fEnd; } 36 | int GetArgCount() const { return fArgCount; } 37 | void SetArg(int i, const Token *t, int count); 38 | 39 | private: 40 | Macro* fDef; 41 | const Token* fTokens; 42 | const Token* fEnd; 43 | int fArgCount; 44 | Token** fArgs; 45 | int* fArgLengths; 46 | }; 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /compiler/ExprStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "ExprStmt.h" 16 | #include "Bytecode.h" 17 | #include "Expr.h" 18 | 19 | 20 | ExprStmt::ExprStmt(Expr * value) 21 | : fValue(value) 22 | { 23 | } 24 | 25 | 26 | ExprStmt::~ExprStmt() 27 | { 28 | delete fValue; 29 | } 30 | 31 | 32 | void ExprStmt::EmitActual(Bytecode &b) 33 | { 34 | fValue->EmitSide(b); 35 | } 36 | 37 | 38 | Stmt* ExprStmt::CloneActual(Mapping *b) const 39 | { 40 | return new ExprStmt(fValue->Clone(b)); 41 | } 42 | 43 | 44 | void ExprStmt::GetExprs(vector &v) const 45 | { 46 | v.push_back(fValue); 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /compiler/ExprStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __ExprStmt_h 15 | #define __ExprStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | class Expr; 22 | 23 | class ExprStmt : public LeafStmt 24 | { 25 | public: 26 | ExprStmt(Expr* value); 27 | ~ExprStmt(); 28 | 29 | void EmitActual(Bytecode &b); 30 | Stmt* CloneActual(Mapping *b) const; 31 | virtual void GetExprs(vector & v) const; 32 | 33 | private: 34 | Expr* fValue; 35 | }; 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /compiler/ForStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "ForStmt.h" 15 | #include "Bytecode.h" 16 | 17 | 18 | ForStmt::ForStmt(Stmt *init, Expr *c, Stmt *iterate, Stmt *body) : 19 | fInit(init), ///< Optional; can be NULL 20 | fCondition(c), ///< Optional; can be NULL 21 | fIterate(iterate), ///< Optional; can be NULL 22 | fBody(body) 23 | { 24 | // we need to keep child statements in a list 25 | fChildren.InsertHead(fBody); 26 | if (fIterate) 27 | fChildren.InsertHead(fIterate); 28 | if (fInit) 29 | fChildren.InsertHead(fInit); 30 | 31 | Adopt(init); 32 | Adopt(iterate); 33 | Adopt(body); 34 | } 35 | 36 | 37 | ForStmt::~ForStmt() 38 | { 39 | delete fInit; 40 | delete fCondition; 41 | delete fIterate; 42 | delete fBody; 43 | } 44 | 45 | 46 | void ForStmt::EmitActual(Bytecode &b) 47 | { 48 | /* 49 | init 50 | loop: 51 | test !condition -> break 52 | body 53 | continue: 54 | iterate 55 | jump -> loop 56 | break: 57 | */ 58 | 59 | if (fInit) 60 | fInit->Emit(b); 61 | 62 | int cLabel = b.PushFlow(Bytecode::kContinueFlow); 63 | int bLabel = b.PushFlow(Bytecode::kBreakFlow); 64 | 65 | int loopLabel = b.NewLabel(); 66 | 67 | if (fCondition) 68 | fCondition->EmitBranch(b, bLabel, false); 69 | 70 | fBody->Emit(b); 71 | 72 | b.SetLabel(cLabel); 73 | if (fIterate) 74 | fIterate->Emit(b); 75 | b.AddJump(loopLabel); 76 | 77 | b.SetLabel(bLabel); 78 | b.PopFlow(Bytecode::kContinueFlow); 79 | b.PopFlow(Bytecode::kBreakFlow); 80 | } 81 | 82 | 83 | Stmt* ForStmt::CloneActual(Mapping *b) const 84 | { 85 | return new ForStmt( 86 | fInit ? fInit->Clone(b) : 0, 87 | fCondition ? fCondition->Clone(b) : 0, 88 | fIterate ? fIterate->Clone(b) : 0, 89 | fBody->Clone(b)); 90 | } 91 | -------------------------------------------------------------------------------- /compiler/ForStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __ForStmt_h 15 | #define __ForStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __Expr_h 22 | #include "Expr.h" 23 | #endif 24 | 25 | 26 | class ForStmt : public Stmt 27 | { 28 | public: 29 | ForStmt(Stmt *init, Expr *c, Stmt *iterate, Stmt *body); 30 | ~ForStmt(); 31 | 32 | virtual Stmt* GetChildren() { return fChildren.GetHead(); } 33 | virtual void GetExprs(vector & v) const { if (fCondition) v.push_back(fCondition); } 34 | 35 | virtual void EmitActual(Bytecode &b); 36 | virtual Stmt* CloneActual(Mapping *b) const; 37 | 38 | 39 | private: 40 | Stmt* fInit; 41 | Expr* fCondition; 42 | Stmt* fIterate; 43 | Stmt* fBody; 44 | PListSS fChildren; 45 | }; 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /compiler/FunctionDef.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #include "FunctionDef.h" 16 | #include "Stmt.h" 17 | #include "Scope.h" 18 | #include "Program.h" 19 | 20 | FunctionDef::FunctionDef() 21 | { 22 | fBody = 0; 23 | fName = 0; 24 | fExpanded = false; 25 | fListingEnabled = true; 26 | } 27 | 28 | 29 | FunctionDef::~FunctionDef() 30 | { 31 | delete fBody; 32 | } 33 | 34 | 35 | bool FunctionDef::AddArg(const Symbol *name, ArgType type) 36 | { 37 | Arg a; 38 | 39 | for(size_t i=0; iNextVirtualVar(); 68 | 69 | switch(fArgs[i].fType) 70 | { 71 | case kConstantArg: 72 | var |= kVirtualConstantFlag | kVirtualReadOnlyFlag; 73 | break; 74 | case kConstRefArg: 75 | case kSensorArg: 76 | var |= kVirtualReadOnlyFlag; 77 | break; 78 | case kPointerArg: 79 | var |= kPointerFlag; 80 | break; 81 | case kConstPtrArg: 82 | var |= kVirtualReadOnlyFlag | kPointerFlag; 83 | break; 84 | default: 85 | break; 86 | } 87 | 88 | fArgs[i].fVar = var; 89 | gProgram->DefineVar(fArgs[i].fName, var, false, 90 | ((fArgs[i].fType == kPointerArg) || (fArgs[i].fType == kConstPtrArg)), 91 | false); 92 | } 93 | } 94 | 95 | 96 | void FunctionDef::SetLocations(LocationNode *start, LocationNode *end) 97 | { 98 | fStart = start->GetLoc(); 99 | delete start; 100 | 101 | fEnd = end->GetLoc(); 102 | delete end; 103 | } 104 | 105 | -------------------------------------------------------------------------------- /compiler/GosubParamStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "GosubParamStmt.h" 15 | #include "Bytecode.h" 16 | #include "AtomExpr.h" 17 | #include "Fragment.h" 18 | #include "Error.h" 19 | #include "RCX_Cmd.h" 20 | 21 | void GosubParamStmt::AddParams(vector ¶ms) 22 | { 23 | for(size_t i=0; iGetLocalMask())) 33 | { 34 | Error(kErr_NoMoreVars).Raise(&fLocation); 35 | return; 36 | } 37 | 38 | size_t argCount = fFragment->GetArgCount(); 39 | 40 | if (argCount != fParams.size()) 41 | { 42 | Error(kErr_ParamCount).Raise(&fLocation); 43 | return; 44 | } 45 | 46 | RCX_Cmd cmd; 47 | 48 | // push parameters onto the stack 49 | for(int i = 0; i < (int)fParams.size(); i++) 50 | { 51 | RCX_Value ea = fParams[i]->EmitAny(b); 52 | cmd.MakePushStackEntry(ea); 53 | b.Add(cmd); 54 | b.ReleaseTempEA(ea); 55 | } 56 | 57 | cmd.Set(kRCX_GoSubOp, fFragment->GetNumber()); 58 | b.Add(cmd); 59 | 60 | if (fFragment->GetArgCount() > 0) 61 | { 62 | // pop parameters off the stack 63 | cmd.Set(kRCX_PopStackEntryOp, fParams.size()); 64 | b.Add(cmd); 65 | } 66 | 67 | } 68 | 69 | 70 | Stmt* GosubParamStmt::CloneActual(Mapping *b) const 71 | { 72 | GosubParamStmt *g = new GosubParamStmt(fFragment, fLocation); 73 | for(size_t i=0; ifParams.push_back(fParams[i]->Clone(b)); 75 | return g; 76 | } 77 | -------------------------------------------------------------------------------- /compiler/GosubParamStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __GosubParamStmt_h 15 | #define __GosubParamStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | #include 26 | 27 | using std::vector; 28 | 29 | class Fragment; 30 | class Expr; 31 | 32 | class GosubParamStmt : public LeafStmt 33 | { 34 | public: 35 | GosubParamStmt(Fragment *f, const struct LexLocation &loc) : fFragment(f), fLocation(loc) {} 36 | 37 | bool Check(CheckState &state); 38 | void EmitActual(Bytecode &b); 39 | Stmt* CloneActual(Mapping *b) const; 40 | Fragment* GetFragment() const { return fFragment; } 41 | void AddParams(vector ¶ms); 42 | 43 | private: 44 | Fragment* fFragment; 45 | struct LexLocation fLocation; 46 | vector fParams; 47 | 48 | }; 49 | 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /compiler/GosubStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "GosubStmt.h" 15 | #include "Bytecode.h" 16 | #include "Fragment.h" 17 | #include "Error.h" 18 | #include "RCX_Cmd.h" 19 | 20 | void GosubStmt::EmitActual(Bytecode &b) 21 | { 22 | if (!b.GetVarAllocator().CheckLocalMask(fFragment->GetLocalMask())) 23 | { 24 | Error(kErr_NoMoreVars).Raise(&fLocation); 25 | return; 26 | } 27 | 28 | RCX_Cmd cmd; 29 | 30 | cmd.Set(kRCX_GoSubOp, fFragment->GetNumber()); 31 | b.Add(cmd); 32 | 33 | } 34 | 35 | 36 | Stmt* GosubStmt::CloneActual(Mapping *) const 37 | { 38 | return new GosubStmt(fFragment, fLocation); 39 | } 40 | -------------------------------------------------------------------------------- /compiler/GosubStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __GosubStmt_h 15 | #define __GosubStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | class Fragment; 26 | 27 | class GosubStmt : public LeafStmt 28 | { 29 | public: 30 | GosubStmt(Fragment *f, const struct LexLocation &loc) : fFragment(f), fLocation(loc) {} 31 | 32 | bool Check(CheckState &state); 33 | void EmitActual(Bytecode &b); 34 | Stmt* CloneActual(Mapping *b) const; 35 | Fragment* GetFragment() const { return fFragment; } 36 | 37 | private: 38 | Fragment* fFragment; 39 | struct LexLocation fLocation; 40 | 41 | }; 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /compiler/GotoStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __GotoStmt_h 15 | #define __GotoStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | class Bytecode; 26 | 27 | class GotoStmt : public LeafStmt 28 | { 29 | public: 30 | GotoStmt(const Symbol *name, const struct LexLocation &loc); 31 | 32 | bool Check(CheckState &state); 33 | void EmitActual(Bytecode &b); 34 | Stmt* CloneActual(Mapping *b) const; 35 | 36 | void SetLabel(int l) { fLabel = l; } 37 | 38 | static void ResolveGotos(Stmt *s, Bytecode &b); 39 | 40 | friend class GotoBinder; 41 | 42 | private: 43 | const Symbol* fName; 44 | struct LexLocation fLocation; 45 | int fLabel; 46 | 47 | }; 48 | 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /compiler/IfStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "IfStmt.h" 15 | #include "Bytecode.h" 16 | 17 | IfStmt::IfStmt(Expr *c, Stmt *s1, Stmt *s2) : 18 | BinaryStmt(s1, s2), fCondition(c) 19 | { 20 | } 21 | 22 | 23 | IfStmt::~IfStmt() 24 | { 25 | delete fCondition; 26 | } 27 | 28 | 29 | void IfStmt::EmitActual(Bytecode &b) 30 | { 31 | int value; 32 | 33 | if (fCondition->Evaluate(value)) 34 | { 35 | if (value && (!GetSecondary() || !GetSecondary()->GetMustEmit())) 36 | { 37 | GetPrimary()->Emit(b); 38 | return; 39 | } 40 | else if (!GetPrimary()->GetMustEmit()) 41 | { 42 | if (GetSecondary()) GetSecondary()->Emit(b); 43 | return; 44 | } 45 | } 46 | 47 | if (GetSecondary()) 48 | EmitIfElse(b); 49 | else 50 | EmitIf(b); 51 | } 52 | 53 | 54 | void IfStmt::EmitIf(Bytecode &b) 55 | { 56 | int label; 57 | 58 | label = b.NewLabel(); 59 | fCondition->EmitBranch(b, label, false); 60 | GetPrimary()->Emit(b); 61 | b.SetLabel(label); 62 | } 63 | 64 | 65 | void IfStmt::EmitIfElse(Bytecode &b) 66 | { 67 | int testLabel = b.NewLabel(); 68 | int outLabel= b.NewLabel(); 69 | 70 | fCondition->EmitBranch(b, testLabel, false); 71 | 72 | // generate A and jump 73 | GetPrimary()->Emit(b); 74 | b.AddJump(outLabel); 75 | b.SetLabel(testLabel); 76 | 77 | // generate B 78 | GetSecondary()->Emit(b); 79 | b.SetLabel(outLabel); 80 | } 81 | 82 | 83 | Stmt* IfStmt::CloneActual(Mapping *b) const 84 | { 85 | return new IfStmt( 86 | fCondition->Clone(b), 87 | GetPrimary()->Clone(b), 88 | GetSecondary() ? GetSecondary()->Clone(b) : 0); 89 | } 90 | -------------------------------------------------------------------------------- /compiler/IfStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __IfStmt_h 15 | #define __IfStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __Expr_h 22 | #include "Expr.h" 23 | #endif 24 | 25 | class IfStmt : public BinaryStmt 26 | { 27 | public: 28 | IfStmt(Expr *c, Stmt *s1, Stmt *s2=nil); 29 | ~IfStmt(); 30 | 31 | void EmitActual(Bytecode &b); 32 | Stmt* CloneActual(Mapping *b) const; 33 | 34 | virtual void GetExprs(vector & v) const { v.push_back(fCondition); } 35 | 36 | private: 37 | void EmitIf(Bytecode &b); 38 | void EmitIfElse(Bytecode &b); 39 | 40 | Expr* fCondition; 41 | }; 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /compiler/IncDecExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #include "IncDecExpr.h" 16 | #include "RCX_Cmd.h" 17 | #include "Bytecode.h" 18 | #include "Mapping.h" 19 | 20 | IncDecExpr::IncDecExpr(int var, bool inc, bool pre, const LexLocation &loc) : 21 | Expr(loc), 22 | fVar(var), 23 | fInc(inc), 24 | fPre(pre) 25 | { 26 | } 27 | 28 | 29 | IncDecExpr::~IncDecExpr() 30 | { 31 | } 32 | 33 | 34 | Expr* IncDecExpr::Clone(Mapping *m) const 35 | { 36 | int newVar = m ? m->GetMappedVar(fVar) : fVar; 37 | return new IncDecExpr(newVar, fInc, fPre, GetLoc()); 38 | } 39 | 40 | 41 | bool IncDecExpr::Contains(int var) const 42 | { 43 | return var == fVar; 44 | } 45 | 46 | 47 | RCX_Value IncDecExpr::EmitAny_(Bytecode &b) const 48 | { 49 | RCX_Value src; 50 | 51 | src = RCX_VALUE(kRCX_VariableType, fVar); 52 | 53 | if (fPre) { 54 | (void)EmitSide_(b); 55 | return src; 56 | } 57 | else { 58 | int var = GetTempVar(b); 59 | if (var == kIllegalVar) return kIllegalEA; 60 | 61 | b.AddMove(var, src); 62 | (void)EmitSide_(b); 63 | 64 | return RCX_VALUE(kRCX_VariableType, var); 65 | } 66 | } 67 | 68 | 69 | bool IncDecExpr::EmitTo_(Bytecode &b, int dst) const 70 | { 71 | if (fPre) { 72 | EmitSide_(b); 73 | if (dst != fVar) 74 | b.AddMove(dst, fVar); 75 | } 76 | else { 77 | if (dst!=fVar) { 78 | b.AddMove(dst, fVar); 79 | EmitSide_(b); 80 | } 81 | } 82 | 83 | return true; 84 | } 85 | 86 | 87 | bool IncDecExpr::EmitSide_(Bytecode &b) const 88 | { 89 | RCX_Cmd cmd; 90 | 91 | cmd.MakeVar(fInc ? kRCX_AddVar : kRCX_SubVar, 92 | fVar, 93 | RCX_VALUE(kRCX_ConstantType, 1)); 94 | b.Add(cmd); 95 | return true; 96 | } 97 | 98 | 99 | void IncDecExpr::Translate(int from, int to) 100 | { 101 | if (fVar == from) 102 | fVar = to; 103 | } 104 | 105 | -------------------------------------------------------------------------------- /compiler/IncDecExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | 16 | #ifndef __IncDecExpr_h 17 | #define __IncDecExpr_h 18 | 19 | #ifndef __Expr_h 20 | #include "Expr.h" 21 | #endif 22 | 23 | #ifndef __VarTranslator_h 24 | #include "VarTranslator.h" 25 | #endif 26 | 27 | class IncDecExpr : public Expr, public Translatable 28 | { 29 | public: 30 | IncDecExpr(int var, bool inc, bool pre, const LexLocation &loc); 31 | ~IncDecExpr(); 32 | 33 | virtual Expr* Clone(Mapping *b) const; 34 | virtual bool Contains(int var) const; 35 | virtual bool PromiseConstant() const { return false; } 36 | virtual RCX_Value EmitAny_(Bytecode &b) const; 37 | virtual bool EmitTo_(Bytecode &b, int dst) const; 38 | virtual bool EmitSide_(Bytecode &b) const; 39 | 40 | void Translate(int from, int to); 41 | private: 42 | int fVar; 43 | bool fInc; 44 | bool fPre; 45 | }; 46 | 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /compiler/IndirectExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "IndirectExpr.h" 15 | #include "Mapping.h" 16 | #include "RCX_Cmd.h" 17 | #include "Bytecode.h" 18 | 19 | Expr* IndirectExpr::Clone(Mapping *m) const 20 | { 21 | return new IndirectExpr(Get(0)->Clone(m), Get(1)->Clone(m)); 22 | } 23 | 24 | 25 | 26 | RCX_Value IndirectExpr::EmitAny_(Bytecode &b) const 27 | { 28 | // decode src 29 | int src; 30 | if (!Get(0)->Evaluate(src)) 31 | return kIllegalEA; 32 | int directSrc = (src >> 8) & 0xff; 33 | int indirectSrc = (src) & 0xff; 34 | 35 | // see if direct can be used 36 | int idx; 37 | if (directSrc && Get(1)->Evaluate(idx)) 38 | { 39 | return RCX_VALUE(directSrc, idx); 40 | } 41 | 42 | // must use indirect 43 | 44 | // see if index is already in a var 45 | int var = Get(1)->GetLValue(); 46 | if (var != kIllegalVar) 47 | return RCX_VALUE(indirectSrc, var); 48 | 49 | // emit the index 50 | int dst = GetTempVar(b); 51 | if (dst < 0) 52 | return kIllegalEA; 53 | Get(1)->EmitTo(b, dst); 54 | 55 | return RCX_VALUE(indirectSrc, dst) + kRCX_ValueUsesTemp; 56 | } 57 | -------------------------------------------------------------------------------- /compiler/IndirectExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __IndirectExpr_h 16 | #define __IndirectExpr_h 17 | 18 | #include "NodeExpr.h" 19 | 20 | 21 | class IndirectExpr : public NodeExpr 22 | { 23 | public: 24 | IndirectExpr(Expr *src, Expr *idx) : NodeExpr(src, idx) {} 25 | 26 | virtual Expr* Clone(Mapping *b) const; 27 | virtual bool PromiseConstant() const { return false; } 28 | virtual bool PotentialLValue() const { return true; } 29 | 30 | virtual RCX_Value EmitAny_(Bytecode &b) const; 31 | 32 | private: 33 | }; 34 | 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /compiler/InlineStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #include "InlineStmt.h" 16 | #include "Bytecode.h" 17 | #include "FunctionDef.h" 18 | 19 | void InlineStmt::EmitActual(Bytecode &b) 20 | { 21 | b.AddSourceTag( 22 | fFunction->GetListingEnabled() ? RCX_SourceTag::kBegin : RCX_SourceTag::kBeginNoList, 23 | fFunction->GetStartLoc()); 24 | 25 | int rLabel = b.PushFlow(Bytecode::kReturnFlow); 26 | b.PushFlow(Bytecode::kContinueFlow, false); 27 | b.PushFlow(Bytecode::kBreakFlow, false); 28 | 29 | GetBody()->Emit(b); 30 | 31 | b.SetLabel(rLabel); 32 | 33 | b.PopFlow(Bytecode::kContinueFlow); 34 | b.PopFlow(Bytecode::kBreakFlow); 35 | b.PopFlow(Bytecode::kReturnFlow); 36 | 37 | b.AddSourceTag(RCX_SourceTag::kEnd, fFunction->GetEndLoc()); 38 | } 39 | 40 | 41 | /// This never really gets called because cloning happens before 42 | /// InlineStmts are substituted for CallStmts. 43 | Stmt* InlineStmt::CloneActual(Mapping *b) const 44 | { 45 | return new InlineStmt(GetBody()->Clone(b), fFunction); 46 | } 47 | -------------------------------------------------------------------------------- /compiler/InlineStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __InlineStmt_h 15 | #define __InlineStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | 22 | class FunctionDef; 23 | 24 | /** 25 | * The root of an expanded inline function\. Its only true function 26 | * is to setup the return label so that return statements within the 27 | * expanded function will exit to the correct place. 28 | */ 29 | class InlineStmt : public ChainStmt 30 | { 31 | public: 32 | InlineStmt(Stmt *s, FunctionDef *f) : ChainStmt(s), fFunction(f) { } 33 | void EmitActual(Bytecode &b); 34 | Stmt* CloneActual(Mapping *b) const; 35 | 36 | private: 37 | FunctionDef* fFunction; 38 | }; 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /compiler/JumpStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "JumpStmt.h" 15 | #include "Bytecode.h" 16 | #include "Error.h" 17 | 18 | 19 | JumpStmt::JumpStmt(int type, const LexLocation &loc) 20 | { 21 | fType = type; 22 | fLocation = loc; 23 | } 24 | 25 | 26 | void JumpStmt::EmitActual(Bytecode &b) 27 | { 28 | if (!b.AddFlowJump(static_cast(fType))) 29 | { 30 | Error( 31 | (fType==Bytecode::kContinueFlow) ? kErr_NoContinueContext : kErr_NoBreakContext 32 | ).Raise(&fLocation); 33 | } 34 | } 35 | 36 | 37 | Stmt* JumpStmt::CloneActual(Mapping *) const 38 | { 39 | return new JumpStmt(fType, fLocation); 40 | } 41 | -------------------------------------------------------------------------------- /compiler/JumpStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __JumpStmt_h 15 | #define __JumpStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | 26 | class JumpStmt : public LeafStmt 27 | { 28 | public: 29 | JumpStmt(int type, const LexLocation &loc); 30 | 31 | void EmitActual(Bytecode &b); 32 | Stmt* CloneActual(Mapping *b) const; 33 | 34 | private: 35 | int fType; 36 | LexLocation fLocation; 37 | }; 38 | 39 | /* 40 | enum 41 | { 42 | kJump_Break = 0, 43 | kJump_Continue, 44 | 45 | kJump_Count // must be the last one 46 | }; 47 | */ 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /compiler/LabelStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "LabelStmt.h" 15 | #include "Bytecode.h" 16 | #include "Error.h" 17 | #include "Symbol.h" 18 | 19 | 20 | LabelStmt::LabelStmt(const Symbol *name, const LexLocation &loc, Stmt *s) : 21 | ChainStmt(s), 22 | fName(name), 23 | fLocation(loc) 24 | { 25 | } 26 | 27 | 28 | LabelStmt::~LabelStmt() 29 | { 30 | } 31 | 32 | 33 | void LabelStmt::EmitActual(Bytecode &b) 34 | { 35 | b.SetLabel(fLabel); 36 | GetBody()->Emit(b); 37 | } 38 | 39 | 40 | Stmt* LabelStmt::CloneActual(Mapping *m) const 41 | { 42 | return new LabelStmt(fName, fLocation, GetBody()->Clone(m)); 43 | } 44 | -------------------------------------------------------------------------------- /compiler/LabelStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | /** 16 | * LabelStmts are used to bind a label to another statement. The 17 | * LabelStmt isn't really a statement itself - just a wrapper for 18 | * a single child statement. 19 | * 20 | */ 21 | 22 | #ifndef __LabelStmt_h 23 | #define __LabelStmt_h 24 | 25 | #ifndef __Stmt_h 26 | #include "Stmt.h" 27 | #endif 28 | 29 | #ifndef __parser_h 30 | #include "parser.h" 31 | #endif 32 | 33 | 34 | class LabelStmt : public ChainStmt 35 | { 36 | public: 37 | LabelStmt(const Symbol *name, const LexLocation &loc, Stmt *s = 0); 38 | ~LabelStmt(); 39 | 40 | virtual void EmitActual(Bytecode &b); 41 | virtual Stmt* CloneActual(Mapping *b) const; 42 | 43 | const Symbol* GetName() const { return fName; } 44 | int GetLabel() const { return fLabel; } 45 | 46 | friend class LabelFinder; 47 | 48 | private: 49 | const Symbol* fName; 50 | LexLocation fLocation; 51 | int fLabel; 52 | }; 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /compiler/LexLocation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __LexLocation_h 15 | #define __LexLocation_h 16 | 17 | class Buffer; 18 | 19 | #define kIllegalSrcIndex -1 20 | 21 | typedef struct LexLocation 22 | { 23 | short fIndex; 24 | short fLength; 25 | long fOffset; 26 | } LexLocation; 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /compiler/LocationNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __LocationNode_h 16 | #define __LocationNode_h 17 | 18 | #ifndef __LexLocation_h 19 | #include "LexLocation.h" 20 | #endif 21 | 22 | #ifndef __AutoFree_h 23 | #include "AutoFree.h" 24 | #endif 25 | 26 | extern void LexCurrentLocation(LexLocation &loc); 27 | 28 | class LocationNode : public AutoFree 29 | { 30 | public: 31 | LocationNode(const LexLocation &loc) : fLocation(loc) {} 32 | LocationNode() { LexCurrentLocation(fLocation); } 33 | 34 | const LexLocation& GetLoc() const { return fLocation; } 35 | 36 | private: 37 | LexLocation fLocation; 38 | }; 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /compiler/LogicalExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "LogicalExpr.h" 15 | #include "Bytecode.h" 16 | 17 | 18 | LogicalExpr::LogicalExpr (Expr *lhs, int op, Expr *rhs) 19 | : NodeExpr(lhs, rhs), fOp(op) 20 | { 21 | } 22 | 23 | 24 | 25 | Expr* LogicalExpr::Clone(Mapping *b) const 26 | { 27 | return new LogicalExpr(Get(0)->Clone(b), fOp, Get(1)->Clone(b)); 28 | } 29 | 30 | 31 | bool LogicalExpr::Evaluate(int &v) const 32 | { 33 | int v1, v2; 34 | bool d1, d2; 35 | bool b1, b2; 36 | 37 | bool dom; 38 | 39 | dom = (fOp==kLogicalOr) ? true : false; 40 | 41 | d1 = Get(0)->Evaluate(v1); 42 | d2 = Get(1)->Evaluate(v2); 43 | 44 | // convert to bool for easy check 45 | b1 = v1; 46 | b2 = v2; 47 | 48 | if ((d1 && (b1 == dom)) || 49 | (d2 && (b2 == dom))) 50 | { 51 | v = dom ? 1 : 0; 52 | return true; 53 | } 54 | 55 | if (d1 && d2) 56 | { 57 | v = dom ? 0 : 1; 58 | return true; 59 | } 60 | 61 | return false; 62 | } 63 | 64 | 65 | bool LogicalExpr::EmitBranch_(Bytecode &b, int label, bool condition) const 66 | { 67 | bool eitherOne; 68 | 69 | if (fOp == kLogicalOr) 70 | eitherOne = condition; 71 | else 72 | eitherOne = !condition; 73 | 74 | 75 | if (eitherOne) 76 | { 77 | /* 78 | test c1 -> label 79 | test c2 -> label 80 | */ 81 | 82 | if (!Get(0)->EmitBranch(b, label, condition)) return false; 83 | if (!Get(1)->EmitBranch(b, label, condition)) return false; 84 | } 85 | else 86 | { 87 | /* 88 | test !c1 -> out 89 | test c2->label 90 | out: 91 | */ 92 | int out = b.NewLabel(); 93 | 94 | if (!Get(0)->EmitBranch(b, out, !condition)) return false; 95 | if (!Get(1)->EmitBranch(b, label, condition)) return false; 96 | 97 | b.SetLabel(out); 98 | } 99 | 100 | return true; 101 | } 102 | -------------------------------------------------------------------------------- /compiler/LogicalExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __LogicalExpr_h 15 | #define __LogicalExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class LogicalExpr : public NodeExpr 20 | { 21 | public: 22 | enum 23 | { 24 | kLogicalOr = 0, 25 | kLogicalAnd 26 | }; 27 | 28 | LogicalExpr(Expr *lhs, int op, Expr *rhs); 29 | 30 | virtual bool Evaluate(int &value) const; 31 | virtual Expr* Clone(Mapping *b) const; 32 | 33 | virtual bool EmitBranch_(Bytecode &b, int label, bool condition) const; 34 | 35 | // glue to emit values based on EmitBranch_() 36 | virtual RCX_Value EmitAny_(Bytecode &b) const { return EmitBoolAny(b); } 37 | virtual bool EmitTo_(Bytecode &b, int dst) const { return EmitBoolTo(b, dst); } 38 | 39 | 40 | private: 41 | int fOp; 42 | }; 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /compiler/Macro.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | using std::memcpy; 16 | 17 | #include "Macro.h" 18 | 19 | 20 | Macro::Macro(const Token *tokens, int count, int argCount) 21 | { 22 | fTokenCount = count; 23 | 24 | if (count) { 25 | fTokens = new Token[count]; 26 | memcpy(fTokens, tokens, sizeof(Token) * count); 27 | } 28 | else { 29 | fTokens = nil; 30 | } 31 | 32 | fArgCount = argCount; 33 | fMark = false; 34 | } 35 | 36 | 37 | Macro::~Macro() 38 | { 39 | delete [] fTokens; 40 | } 41 | -------------------------------------------------------------------------------- /compiler/Macro.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Macro_h 15 | #define __Macro_h 16 | 17 | #ifndef __Token_h 18 | #include "Token.h" 19 | #endif 20 | 21 | #ifndef __PTypes_h 22 | #include "PTypes.h" 23 | #endif 24 | 25 | class Macro 26 | { 27 | public: 28 | enum { 29 | kNoArgs = -1 30 | }; 31 | 32 | Macro(const Token *tokens, int tokenCount, int argCount); 33 | ~Macro(); 34 | 35 | int GetTokenCount() const { return fTokenCount; } 36 | const Token* GetTokens() const { return fTokens; } 37 | int GetArgCount() const { return fArgCount; } 38 | 39 | bool IsMarked() const { return fMark; } 40 | void SetMark() { fMark = true; } 41 | void ClearMark() { fMark = false; } 42 | 43 | private: 44 | Token* fTokens; 45 | int fTokenCount; 46 | int fArgCount; 47 | bool fMark; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /compiler/Mapping.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "Mapping.h" 15 | #include "Expr.h" 16 | 17 | Mapping::Mapping() 18 | { 19 | } 20 | 21 | Mapping::~Mapping() 22 | { 23 | for(size_t i=0; iGetLValue(); 50 | } 51 | -------------------------------------------------------------------------------- /compiler/Mapping.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __Mapping_h 16 | #define __Mapping_h 17 | 18 | #ifndef __Variable_h 19 | #include "Variable.h" 20 | #endif 21 | 22 | #include 23 | 24 | using std::vector; 25 | 26 | class Expr; 27 | 28 | class Mapping 29 | { 30 | public: 31 | Mapping(); 32 | ~Mapping(); 33 | 34 | void Add(int var, Expr *value); 35 | const Expr* Get(int var) const; 36 | 37 | int GetMappedVar(int var) const; 38 | 39 | private: 40 | struct Pair { 41 | Pair(int v, Expr *e) : fVar(v), fValue(e) {} 42 | 43 | int fVar; 44 | Expr* fValue; 45 | }; 46 | 47 | vector fPairs; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /compiler/ModExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __ModExpr_h 15 | #define __ModExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class ModExpr : public NodeExpr 20 | { 21 | public: 22 | ModExpr(Expr *lhs, Expr *rhs) : NodeExpr(lhs, rhs) {}; 23 | 24 | virtual bool Evaluate(int &value) const; 25 | virtual Expr* Clone(Mapping *b) const; 26 | 27 | virtual RCX_Value EmitAny_(Bytecode &b) const; 28 | virtual bool EmitTo_(Bytecode &b, int dst) const; 29 | 30 | private: 31 | bool EmitCalculation(Bytecode &b, int dst) const; 32 | }; 33 | 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /compiler/MonitorStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __MonitorStmt_h 15 | #define __MonitorStmt_h 16 | 17 | #ifndef __BlockStmt_h 18 | #include "BlockStmt.h" 19 | #endif 20 | 21 | #ifndef __LexLocation_h 22 | #include "LexLocation.h" 23 | #endif 24 | 25 | class MonitorStmt : public BinaryStmt 26 | { 27 | public: 28 | MonitorStmt(Expr *events, Stmt *body, BlockStmt *handlers, const LexLocation &loc); 29 | ~MonitorStmt(); 30 | 31 | void GetExprs(vector & v) const; 32 | 33 | void EmitActual(Bytecode &b); 34 | Stmt* CloneActual(Mapping *b) const; 35 | 36 | private: 37 | Expr* fEvents; 38 | LexLocation fLocation; 39 | }; 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /compiler/NegateExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "NegateExpr.h" 15 | 16 | 17 | 18 | Expr* NegateExpr::Clone(Mapping *b) const 19 | { 20 | return new NegateExpr(Get(0)->Clone(b)); 21 | } 22 | 23 | 24 | bool NegateExpr::EmitBranch_(Bytecode &b, int label, bool condition) const 25 | { 26 | return Get(0)->EmitBranch(b, label, !condition); 27 | } 28 | 29 | 30 | bool NegateExpr::Evaluate(int &value) const 31 | { 32 | if (!Get(0)->Evaluate(value)) return false; 33 | 34 | value = value ? 0 : 1; 35 | 36 | return true; 37 | } 38 | -------------------------------------------------------------------------------- /compiler/NegateExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | 16 | #ifndef __NegateExpr_h 17 | #define __NegateExpr_h 18 | 19 | #include "NodeExpr.h" 20 | 21 | 22 | class NegateExpr : public NodeExpr 23 | { 24 | public: 25 | NegateExpr(Expr *e) : NodeExpr(e) {} 26 | 27 | virtual Expr* Clone(Mapping *b) const; 28 | 29 | virtual bool EmitBranch_(Bytecode &b, int label, bool condition) const; 30 | virtual bool Evaluate(int & value) const; 31 | 32 | // glue to emit values based on EmitBranch_() 33 | virtual RCX_Value EmitAny_(Bytecode &b) const { return EmitBoolAny(b); } 34 | virtual bool EmitTo_(Bytecode &b, int dst) const { return EmitBoolTo(b, dst); } 35 | }; 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /compiler/NodeExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "NodeExpr.h" 15 | 16 | NodeExpr::NodeExpr(Expr *e) 17 | : Expr(e->GetLoc()) 18 | { 19 | fExprs[0] = e; 20 | fCount = 1; 21 | } 22 | 23 | 24 | NodeExpr::NodeExpr(Expr *a, Expr *b) 25 | : Expr(b->GetLoc()) 26 | { 27 | fExprs[0] = a; 28 | fExprs[1] = b; 29 | fCount = 2; 30 | } 31 | 32 | 33 | NodeExpr::NodeExpr(Expr *a, Expr *b, Expr *c) 34 | : Expr(b->GetLoc()) 35 | { 36 | fExprs[0] = a; 37 | fExprs[1] = b; 38 | fExprs[2] = c; 39 | fCount = 3; 40 | } 41 | 42 | 43 | NodeExpr::~NodeExpr() 44 | { 45 | for(int i=0; iContains(var)) return true; 54 | return false; 55 | } 56 | 57 | 58 | bool NodeExpr::PromiseConstant() const 59 | { 60 | for(int i=0; iPromiseConstant()) return false; 62 | return true; 63 | } 64 | 65 | 66 | void NodeExpr::GetExprs(vector &v) const 67 | { 68 | for(int i=0; i & /* v */) const; 32 | 33 | protected: 34 | Expr* Get(int i) { return fExprs[i]; } 35 | const Expr* Get(int i) const { return fExprs[i]; } 36 | 37 | private: 38 | static const int MAX_EXPRS = 3; 39 | Expr* fExprs[MAX_EXPRS]; 40 | int fCount; 41 | }; 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /compiler/RelExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RelExpr_h 15 | #define __RelExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class RelExpr : public NodeExpr 20 | { 21 | public: 22 | enum 23 | { 24 | kLessOrEqual = 0, 25 | kGreaterOrEqual, 26 | kNotEqualTo, 27 | kEqualTo, 28 | kGreaterThan, 29 | kLessThan 30 | }; 31 | 32 | RelExpr(Expr *lhs, int relation, Expr *rhs); 33 | 34 | virtual bool Evaluate(int &value) const; 35 | virtual Expr* Clone(Mapping *b) const; 36 | 37 | // glue to emit values based on EmitBranch_() 38 | virtual RCX_Value EmitAny_(Bytecode &b) const { return EmitBoolAny(b); } 39 | virtual bool EmitTo_(Bytecode &b, int dst) const { return EmitBoolTo(b, dst); } 40 | 41 | 42 | virtual bool EmitBranch_(Bytecode &b, int label, bool condition) const; 43 | 44 | private: 45 | int fRelation; 46 | }; 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /compiler/RepeatStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RepeatStmt_h 15 | #define __RepeatStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | class Expr; 22 | 23 | class RepeatStmt : public ChainStmt 24 | { 25 | public: 26 | RepeatStmt(Expr *c, Stmt *s); 27 | ~RepeatStmt(); 28 | 29 | void EmitActual(Bytecode &b); 30 | Stmt* CloneActual(Mapping *b) const; 31 | 32 | virtual void GetExprs(vector & v) const; 33 | 34 | private: 35 | void EmitRCXLoop(Bytecode &b); 36 | void EmitRCXVar(Bytecode &b); 37 | void EmitDecJump(Bytecode &b); 38 | int EmitCountToTemp(Bytecode &b); 39 | 40 | Expr* fCount; 41 | 42 | // flag to indicate if rcx loop counter can be used 43 | static bool sRCXLoopInUse; 44 | }; 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /compiler/Resource.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "Resource.h" 15 | #include "Expr.h" 16 | 17 | Resource::Resource() 18 | { 19 | } 20 | 21 | 22 | Resource::~Resource() 23 | { 24 | } 25 | 26 | 27 | void Resource::SetInfo(RCX_ChunkType type, Symbol *name) 28 | { 29 | fType = type; 30 | fName = name; 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /compiler/Resource.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Resource_h 15 | #define __Resource_h 16 | 17 | #ifndef __PTypes_h 18 | #include "PTypes.h" 19 | #endif 20 | 21 | #ifndef __PListS_h 22 | #include "PListS.h" 23 | #endif 24 | 25 | #ifndef __AutoFree_h 26 | #include "AutoFree.h" 27 | #endif 28 | 29 | #ifndef __RCX_Constants_h 30 | #include "RCX_Constants.h" 31 | #endif 32 | 33 | #ifndef __LocationNode_h 34 | #include "LocationNode.h" 35 | #endif 36 | 37 | #include 38 | 39 | using std::vector; 40 | 41 | class Symbol; 42 | class Expr; 43 | 44 | class Resource : public PLinkS, public AutoFree 45 | { 46 | public: 47 | Resource(); 48 | ~Resource(); 49 | 50 | void SetInfo(RCX_ChunkType type, Symbol *name); 51 | 52 | void SetNumber(int n) { fNumber = n; } 53 | int GetNumber() const { return fNumber; } 54 | Symbol* GetName() const { return fName; } 55 | RCX_ChunkType GetType() const { return fType; } 56 | 57 | void Add(int value) { fData.push_back((UByte)value); } 58 | int GetLength() const { return fData.size(); } 59 | const UByte* GetData() const { return &fData[0]; } 60 | 61 | private: 62 | RCX_ChunkType fType; 63 | Symbol* fName; 64 | int fNumber; 65 | vector fData; 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /compiler/Scope.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #include "Scope.h" 16 | #include "Variable.h" 17 | 18 | Scope::Scope() 19 | { 20 | } 21 | 22 | Scope::~Scope() 23 | { 24 | } 25 | 26 | 27 | bool Scope::Define(const Symbol *name, int var, bool array, bool ptr, bool stack) 28 | { 29 | if (Contains(name)) 30 | return false; 31 | 32 | fBindings.push_back(ScopeData(name, var, array, ptr, stack)); 33 | return true; 34 | } 35 | 36 | 37 | int Scope::Lookup(const Symbol *name, bool &array, bool &ptr, bool &stack) 38 | { 39 | Scope *s; 40 | 41 | for(s=this; s; s=s->GetNext()) 42 | { 43 | int var = s->Lookup1(name, &array, &ptr, &stack); 44 | if (var != kIllegalVar) return var; 45 | } 46 | 47 | return kIllegalVar; 48 | } 49 | 50 | 51 | int Scope::Lookup1(const Symbol *name, bool *array, bool *ptr, bool *stack) 52 | { 53 | for(size_t i=0; i 31 | 32 | using std::vector; 33 | 34 | class Symbol; 35 | 36 | 37 | class Scope : public PLinkS, public AutoFree 38 | { 39 | public: 40 | Scope(); 41 | ~Scope(); 42 | 43 | bool Define(const Symbol *name, int var, bool array, bool ptr, bool stack); 44 | int Lookup(const Symbol *name, bool &array, bool &ptr, bool &stack); 45 | bool Contains(const Symbol *name) { return Lookup1(name)!= kIllegalVar; } 46 | 47 | private: 48 | struct ScopeData 49 | { 50 | ScopeData() : fName(0), fVar(kIllegalVar), fArray(0), fPtr(0), fStack(0) {} 51 | ScopeData(const Symbol *name, int var, bool a, bool ptr, bool stack) : 52 | fName(name), fVar(var), fArray(a), fPtr(ptr), fStack(stack) {} 53 | 54 | const Symbol * fName; 55 | int fVar; 56 | bool fArray; 57 | bool fPtr; 58 | bool fStack; 59 | }; 60 | 61 | int Lookup1(const Symbol *name, bool *array = 0, bool *ptr = 0, bool *stack = 0); 62 | 63 | vector fBindings; 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /compiler/ScopeStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "ScopeStmt.h" 15 | #include "VarTranslator.h" 16 | #include "Bytecode.h" 17 | 18 | ScopeStmt::ScopeStmt(Stmt *s) : 19 | ChainStmt(s) 20 | { 21 | } 22 | 23 | 24 | ScopeStmt::~ScopeStmt() 25 | { 26 | } 27 | 28 | 29 | void ScopeStmt::EmitActual(Bytecode &b) 30 | { 31 | GetBody()->EmitActual(b); 32 | 33 | // release vars in the reverse order they were allocated 34 | for(int i=fVariables.size()-1; i>=0; --i) 35 | b.GetVarAllocator().Release(fVariables[i]); 36 | } 37 | 38 | 39 | Stmt* ScopeStmt::CloneActual(Mapping *b) const 40 | { 41 | return new ScopeStmt(GetBody()->Clone(b)); 42 | } 43 | 44 | 45 | void ScopeStmt::RemapVar(int from, int to, int count) 46 | { 47 | VarTranslator vt(from, to); 48 | Apply(this, vt); 49 | while(count--) 50 | fVariables.push_back(to++); 51 | } 52 | -------------------------------------------------------------------------------- /compiler/ScopeStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __ScopeStmt_h 15 | #define __ScopeStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #include 22 | 23 | using std::vector; 24 | 25 | class ScopeStmt : public ChainStmt 26 | { 27 | public: 28 | ScopeStmt(Stmt *s); 29 | ~ScopeStmt(); 30 | 31 | void EmitActual(Bytecode &b); 32 | Stmt* CloneActual(Mapping *b) const; 33 | 34 | void RemapVar(int from, int to, int count); 35 | private: 36 | vector fVariables; 37 | }; 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /compiler/SensorExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "SensorExpr.h" 15 | #include "Bytecode.h" 16 | #include "Error.h" 17 | 18 | 19 | Expr* SensorExpr::Clone(Mapping *b) const 20 | { 21 | return new SensorExpr(Get(0)->Clone(b)); 22 | } 23 | 24 | 25 | RCX_Value SensorExpr::EmitAny_(Bytecode &) const 26 | { 27 | int v; 28 | 29 | if (!Evaluate(v)) 30 | return kIllegalEA; 31 | 32 | return RCX_VALUE(kRCX_ConstantType, v); 33 | } 34 | 35 | 36 | bool SensorExpr::Evaluate(int &v) const 37 | { 38 | RCX_Value ea; 39 | 40 | ea = Get(0)->GetStaticEA(); 41 | v = -1; 42 | 43 | switch(RCX_VALUE_TYPE(ea)) 44 | { 45 | case kRCX_ConstantType: 46 | case kRCX_InputValueType: 47 | v = RCX_VALUE_DATA(ea) & 0xff; 48 | return true; 49 | default: 50 | Error(kErr_ParamType, "sensor").Raise(&GetLoc()); 51 | return false; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /compiler/SensorExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | /* 15 | * This class is a kludge that is necessary to allow SENSOR_x to be used 16 | * interchangeably with the index of the sensor. In these cases, a valid 17 | * expression is either a constant or an EA of source 2. The SensorExpr() 18 | * class checks for these conditions and evaluates to the actual index of 19 | * the sensor. 20 | */ 21 | 22 | #ifndef __SensorExpr_h 23 | #define __SensorExpr_h 24 | 25 | #include "NodeExpr.h" 26 | 27 | 28 | class SensorExpr : public NodeExpr 29 | { 30 | public: 31 | SensorExpr(Expr *e) : NodeExpr(e) {} 32 | 33 | virtual Expr* Clone(Mapping *b) const; 34 | 35 | virtual RCX_Value EmitAny_(Bytecode &b) const; 36 | 37 | virtual bool PromiseConstant() const { return true; } 38 | virtual bool Evaluate(int &) const; 39 | }; 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /compiler/ShiftExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __ShiftExpr_h 15 | #define __ShiftExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class ShiftExpr : public NodeExpr 20 | { 21 | public: 22 | enum { 23 | kRight = 0, 24 | kLeft 25 | }; 26 | 27 | ShiftExpr(Expr *lhs, Expr *rhs, int direction) : NodeExpr(lhs, rhs), fDirection(direction) {}; 28 | 29 | virtual bool Evaluate(int &value) const; 30 | virtual Expr* Clone(Mapping *b) const; 31 | 32 | virtual RCX_Value EmitAny_(Bytecode &b) const; 33 | virtual bool EmitTo_(Bytecode &b, int dst) const; 34 | 35 | private: 36 | int fDirection; 37 | }; 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /compiler/SwitchStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __SwitchStmt_h 15 | #define __SwitchStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __Expr_h 22 | #include "Expr.h" 23 | #endif 24 | 25 | #include 26 | 27 | using std::vector; 28 | 29 | class SwitchStmt : public ChainStmt 30 | { 31 | public: 32 | SwitchStmt(Expr *e, Stmt *s); 33 | ~SwitchStmt(); 34 | 35 | virtual void EmitActual(Bytecode &b); 36 | 37 | Stmt* CloneActual(Mapping *b) const; 38 | virtual void GetExprs(vector & v) const; 39 | 40 | private: 41 | Expr* fSelector; 42 | }; 43 | 44 | 45 | class SwitchState 46 | { 47 | public: 48 | SwitchState(RCX_Value selector, int defaultLabel) : 49 | fSelector(selector), 50 | fDefaultLabel(defaultLabel) 51 | {} 52 | 53 | bool ContainsCase(int v); 54 | void AddCase(int v) { fCases.push_back(v); } 55 | 56 | RCX_Value GetSelector() const { return fSelector; } 57 | int GetDefaultLabel() const { return fDefaultLabel; } 58 | 59 | private: 60 | RCX_Value fSelector; 61 | int fDefaultLabel; 62 | vector fCases; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /compiler/Symbol.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include 16 | #include "Symbol.h" 17 | #include "Macro.h" 18 | #include "Fragment.h" 19 | #include "Stmt.h" 20 | 21 | using std::printf; 22 | 23 | #define kHashSize 1023 24 | 25 | 26 | SymbolTable * Symbol::sSymbolTable = 0; 27 | 28 | Symbol::Symbol(const char *name) 29 | { 30 | fKey = new char[strlen(name) + 1]; 31 | strcpy(fKey, name); 32 | fDefinition = 0; 33 | } 34 | 35 | 36 | Symbol::~Symbol() 37 | { 38 | Undefine(); 39 | delete [] fKey; 40 | } 41 | 42 | 43 | void Symbol::Define(Macro *d) 44 | { 45 | delete fDefinition; 46 | fDefinition = d; 47 | } 48 | 49 | 50 | void Symbol::Undefine() 51 | { 52 | delete fDefinition; 53 | fDefinition = 0; 54 | } 55 | 56 | 57 | Symbol *Symbol::Get(const char *name) 58 | { 59 | if (!sSymbolTable) 60 | sSymbolTable = new SymbolTable(); 61 | 62 | Symbol *s; 63 | 64 | s = sSymbolTable->Find(name); 65 | if (!s) 66 | { 67 | s = new Symbol(name); 68 | sSymbolTable->Add(s); 69 | } 70 | 71 | return s; 72 | } 73 | 74 | 75 | SymbolTable *Symbol::GetSymbolTable() 76 | { 77 | if (!sSymbolTable) 78 | sSymbolTable = new SymbolTable(); 79 | 80 | return sSymbolTable; 81 | } 82 | 83 | 84 | SymbolTable::SymbolTable() 85 | : PHashTable(kHashSize) 86 | { 87 | } 88 | 89 | 90 | void SymbolTable::Dump() 91 | { 92 | int i; 93 | Symbol *s; 94 | const Macro *d; 95 | 96 | for(i=0; iGetNext()) 98 | { 99 | printf("%s = ", s->GetKey()); 100 | d = s->GetDefinition(); 101 | if (d) 102 | { 103 | printf("macro [%d]\n", d->GetArgCount()); 104 | } 105 | else 106 | { 107 | printf("no def\n"); 108 | } 109 | } 110 | } 111 | 112 | 113 | SymbolTable::~SymbolTable() 114 | { 115 | } 116 | -------------------------------------------------------------------------------- /compiler/Symbol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Symbol_h 15 | #define __Symbol_h 16 | 17 | #ifndef __PHashTable_h 18 | #include "PHashTable.h" 19 | #endif 20 | 21 | class Macro; 22 | class Fragment; 23 | class Stmt; 24 | 25 | class SymbolTable; 26 | 27 | class Symbol : public PHashable 28 | { 29 | public: 30 | Symbol(const char *name); 31 | ~Symbol(); 32 | 33 | bool IsDefined() const { return fDefinition ? true : false; } 34 | Macro* GetDefinition() { return fDefinition; } 35 | void Define(Macro *d); 36 | void Undefine(); 37 | 38 | static Symbol* Get(const char *name); 39 | static SymbolTable* GetSymbolTable(); 40 | 41 | private: 42 | Macro* fDefinition; 43 | static SymbolTable* sSymbolTable; 44 | }; 45 | 46 | 47 | class SymbolTable : public PHashTable 48 | { 49 | public: 50 | SymbolTable(); 51 | ~SymbolTable(); 52 | 53 | void Dump(); 54 | 55 | private: 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /compiler/TaskIdExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "TaskIdExpr.h" 15 | #include "Error.h" 16 | #include "Stmt.h" 17 | 18 | 19 | Expr* TaskIdExpr::Clone(Mapping *) const 20 | { 21 | return new TaskIdExpr(GetLoc(), fTaskId); 22 | } 23 | 24 | 25 | RCX_Value TaskIdExpr::EmitAny_(Bytecode &) const 26 | { 27 | int v; 28 | 29 | if (!Evaluate(v)) 30 | return kIllegalEA; 31 | 32 | return RCX_VALUE(kRCX_ConstantType, v); 33 | } 34 | 35 | 36 | bool TaskIdExpr::Evaluate(int &v) const 37 | { 38 | if (fTaskId < 0) 39 | { 40 | // during the parse check phases, ids have not been 41 | // assigned, so the constant has not yet been 42 | // determined 43 | return false; 44 | } 45 | 46 | v = fTaskId; 47 | return true; 48 | } 49 | 50 | 51 | bool TaskIdExpr::Patcher::operator()(Stmt *s) 52 | { 53 | vector v; 54 | s->GetExprs(v); 55 | 56 | int n = v.size(); 57 | int i; 58 | 59 | for(i=0; i(e)) 71 | { 72 | te->fTaskId = fId; 73 | if (fId < 0) 74 | { 75 | // the error must be raised here rather than in Evaluate() 76 | // since Evaluate() is called prior to task ids being assigned 77 | Error(kErr_TaskIdUnknown).Raise(&te->GetLoc()); 78 | } 79 | } 80 | 81 | return true; 82 | } 83 | -------------------------------------------------------------------------------- /compiler/TaskIdExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __TaskIdExpr_h 16 | #define __TaskIdExpr_h 17 | 18 | #include "Expr.h" 19 | #include "Fragment.h" 20 | 21 | class TaskIdExpr : public Expr 22 | { 23 | public: 24 | TaskIdExpr(const LexLocation &loc, int id=Fragment::kNoTaskID) : Expr(loc), fTaskId(id) {} 25 | 26 | virtual Expr* Clone(Mapping *b) const; 27 | 28 | virtual RCX_Value EmitAny_(Bytecode &b) const; 29 | 30 | virtual bool Evaluate(int &) const; 31 | 32 | class Patcher 33 | { 34 | public: 35 | Patcher(int id) { fId = id; } 36 | 37 | bool operator()(Stmt *s); 38 | bool operator()(Expr *e); 39 | 40 | private: 41 | int fId; 42 | }; 43 | 44 | friend class Patcher; 45 | 46 | private: 47 | int fTaskId; 48 | }; 49 | 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /compiler/TaskStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "TaskStmt.h" 15 | #include "Bytecode.h" 16 | #include "Symbol.h" 17 | #include "Fragment.h" 18 | #include "Program.h" 19 | #include "Error.h" 20 | #include "RCX_Cmd.h" 21 | 22 | TaskStmt::TaskStmt(UByte opcode, Symbol *name, const LexLocation &loc) 23 | { 24 | fOpcode = opcode; 25 | fName = name; 26 | fLocation = loc; 27 | } 28 | 29 | 30 | void TaskStmt::EmitActual(Bytecode &b) 31 | { 32 | Fragment *f = gProgram->GetTask(fName); 33 | RCX_Cmd cmd; 34 | 35 | if (!f) 36 | { 37 | Error(kErr_UndefinedTask, fName->GetKey()).Raise(&fLocation); 38 | return; 39 | } 40 | 41 | cmd.Set(fOpcode, (UByte)f->GetNumber()); 42 | b.Add(cmd); 43 | } 44 | 45 | 46 | Stmt* TaskStmt::CloneActual(Mapping *) const 47 | { 48 | return new TaskStmt(fOpcode, fName, fLocation); 49 | } 50 | -------------------------------------------------------------------------------- /compiler/TaskStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __TaskStmt_h 15 | #define __TaskStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __parser_h 22 | #include "parser.h" 23 | #endif 24 | 25 | class Symbol; 26 | 27 | class TaskStmt : public LeafStmt 28 | { 29 | public: 30 | TaskStmt(UByte opcode, Symbol *name, const LexLocation &loc); 31 | 32 | void EmitActual(Bytecode &b); 33 | Stmt* CloneActual(Mapping *b) const; 34 | 35 | private: 36 | Symbol* fName; 37 | UByte fOpcode; 38 | LexLocation fLocation; 39 | }; 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /compiler/TernaryExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "TernaryExpr.h" 15 | #include "Bytecode.h" 16 | #include "RCX_Cmd.h" 17 | 18 | 19 | Expr* TernaryExpr::Clone(Mapping *b) const 20 | { 21 | return new TernaryExpr(Get(0)->Clone(b), Get(1)->Clone(b), Get(2)->Clone(b)); 22 | } 23 | 24 | 25 | bool TernaryExpr::Evaluate(int &value) const 26 | { 27 | int test; 28 | const Expr *e; 29 | 30 | if (!Get(0)->Evaluate(test)) return false; 31 | 32 | e = test ? Get(1) : Get(2); 33 | 34 | if (!e->Evaluate(value)) return false; 35 | 36 | return true; 37 | } 38 | 39 | 40 | 41 | RCX_Value TernaryExpr::EmitAny_(Bytecode &b) const 42 | { 43 | int test; 44 | 45 | if (Get(0)->Evaluate(test)) { 46 | if (test) 47 | return Get(1)->EmitAny(b); 48 | else 49 | return Get(2)->EmitAny(b); 50 | } 51 | 52 | int dst = GetTempVar(b); 53 | if (dst < 0) return kIllegalEA; 54 | 55 | EmitConditionTo(b, dst); 56 | 57 | return RCX_VALUE(kRCX_VariableType, dst); 58 | } 59 | 60 | 61 | bool TernaryExpr::EmitTo_(Bytecode &b, int dst) const 62 | { 63 | int test; 64 | 65 | if (Get(0)->Evaluate(test)) { 66 | if (test) 67 | return Get(1)->EmitTo(b, dst); 68 | else 69 | return Get(2)->EmitTo(b, dst); 70 | } 71 | 72 | return EmitConditionTo(b, dst); 73 | } 74 | 75 | 76 | bool TernaryExpr::EmitConditionTo(Bytecode &b, int dst) const 77 | { 78 | int elseLabel = b.NewLabel(); 79 | int doneLabel = b.NewLabel(); 80 | 81 | if (!Get(0)->EmitBranch(b, elseLabel, false)) return false; 82 | 83 | if (!Get(1)->EmitTo(b, dst)) return false; 84 | b.AddJump(doneLabel); 85 | 86 | b.SetLabel(elseLabel); 87 | if (!Get(2)->EmitTo(b, dst)) return false; 88 | b.SetLabel(doneLabel); 89 | 90 | return true; 91 | } 92 | 93 | 94 | -------------------------------------------------------------------------------- /compiler/TernaryExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __TernaryExpr_h 15 | #define __TernaryExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class TernaryExpr : public NodeExpr 20 | { 21 | public: 22 | TernaryExpr(Expr *a, Expr *b, Expr *c) : NodeExpr(a, b, c) {} 23 | 24 | virtual bool Evaluate(int &value) const; 25 | virtual Expr* Clone(Mapping *b) const; 26 | 27 | virtual RCX_Value EmitAny_(Bytecode &b) const; 28 | virtual bool EmitTo_(Bytecode &b, int dst) const; 29 | 30 | 31 | private: 32 | virtual bool EmitConditionTo(Bytecode &b, int dst) const; 33 | }; 34 | 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /compiler/Token.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __Token_h 15 | #define __Token_h 16 | 17 | #ifndef __parser_h 18 | #include "parser.h" 19 | #endif 20 | 21 | typedef YYSTYPE TokenVal; 22 | 23 | class Token 24 | { 25 | public: 26 | int fType; 27 | TokenVal fValue; 28 | }; 29 | 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /compiler/TypeExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "TypeExpr.h" 15 | 16 | 17 | 18 | Expr* TypeExpr::Clone(Mapping *b) const 19 | { 20 | return new TypeExpr(Get(0)->Clone(b)); 21 | } 22 | 23 | 24 | RCX_Value TypeExpr::EmitAny_(Bytecode &) const 25 | { 26 | return kIllegalEA; 27 | } 28 | 29 | 30 | bool TypeExpr::Evaluate(int &value) const 31 | { 32 | int v; 33 | 34 | if (Get(0)->Evaluate(v)) 35 | value = 2; 36 | else 37 | value = 0; 38 | 39 | return true; 40 | } 41 | -------------------------------------------------------------------------------- /compiler/TypeExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | /* This class is only partially implemented. Constant expressions always 16 | * evalutae correctly to type 2. All other expressions evaluate to type 0 17 | * even if they are from some other non-constant source (sensor, random, etc) 18 | * 19 | * Fixing this would require either adding RTTI code, or preferably adding 20 | * another virtual method to the Expr hierarchy that returns the typecode of 21 | * the expression. 22 | */ 23 | 24 | 25 | #ifndef __TypeExpr_h 26 | #define __TypeExpr_h 27 | 28 | #include "NodeExpr.h" 29 | 30 | 31 | class TypeExpr : public NodeExpr 32 | { 33 | public: 34 | TypeExpr(Expr *e) : NodeExpr(e) {} 35 | 36 | virtual Expr* Clone(Mapping *b) const; 37 | 38 | virtual RCX_Value EmitAny_(Bytecode &b) const; 39 | 40 | virtual bool PromiseConstant() const { return true; } 41 | virtual bool Evaluate(int & value) const; 42 | }; 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /compiler/UnaryExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __UnaryExpr_h 15 | #define __UnaryExpr_h 16 | 17 | #include "NodeExpr.h" 18 | 19 | class UnaryExpr : public NodeExpr 20 | { 21 | public: 22 | UnaryExpr(int op, Expr *e) : NodeExpr(e), fOp(op) {} 23 | 24 | virtual bool Evaluate(int &value) const; 25 | virtual Expr* Clone(Mapping *b) const; 26 | 27 | virtual RCX_Value EmitAny_(Bytecode &b) const; 28 | virtual bool EmitTo_(Bytecode &b, int dst) const; 29 | virtual bool EmitSide_(Bytecode &b) const; 30 | 31 | static bool NeedsConstant(int op); 32 | 33 | private: 34 | int fOp; 35 | }; 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /compiler/ValueExpr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "ValueExpr.h" 15 | 16 | 17 | 18 | Expr* ValueExpr::Clone(Mapping *b) const 19 | { 20 | return new ValueExpr(Get(0)->Clone(b)); 21 | } 22 | 23 | 24 | RCX_Value ValueExpr::EmitAny_(Bytecode &) const 25 | { 26 | int v; 27 | 28 | if (!Get(0)->Evaluate(v)) return kIllegalEA; 29 | 30 | return (RCX_Value)v; 31 | } 32 | 33 | 34 | RCX_Value ValueExpr::GetStaticEA_() const 35 | { 36 | int v; 37 | 38 | if (!Get(0)->Evaluate(v)) return kIllegalEA; 39 | 40 | return v; 41 | } 42 | -------------------------------------------------------------------------------- /compiler/ValueExpr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __ValueExpr_h 16 | #define __ValueExpr_h 17 | 18 | #include "NodeExpr.h" 19 | 20 | class ValueExpr : public NodeExpr 21 | { 22 | public: 23 | ValueExpr(Expr *e) : NodeExpr(e) {} 24 | 25 | virtual Expr* Clone(Mapping *b) const; 26 | virtual bool PromiseConstant() const { return false; } 27 | 28 | virtual bool PotentialLValue() const { return true; } 29 | 30 | virtual RCX_Value EmitAny_(Bytecode &b) const; 31 | virtual RCX_Value GetStaticEA_() const; 32 | }; 33 | 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /compiler/VarAllocator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __VarAllocator_h 16 | #define __VarAllocator_h 17 | 18 | class VarAllocator 19 | { 20 | public: 21 | // allocation modes 22 | enum 23 | { 24 | kGlobalMode, 25 | kTaskMode, 26 | kSingleSubMode, 27 | kMultiSubMode 28 | }; 29 | 30 | VarAllocator(int maxGlobals, int maxTaskVars); 31 | ~VarAllocator(); 32 | 33 | bool Reserve(int v); // permenant reservation 34 | 35 | int Allocate(bool temp, bool canUseLocals, int count); 36 | void Release(int v); 37 | 38 | bool IsTemp(int v) const; 39 | void ReleaseTemp(int v); 40 | 41 | void Begin(int mode); 42 | int End(); 43 | 44 | bool CheckLocalMask(int localMask) const; 45 | 46 | private: 47 | enum VarState { 48 | // these states are available for variable allocation 49 | kFree = 0, 50 | kDirty, 51 | 52 | // states from here up are considered "used" 53 | kUsed, 54 | kTemp, 55 | kReserved 56 | }; 57 | 58 | bool IsLegal(int v) const { return (v >= 0 && v < fMaxVars); } 59 | int FindUnused(int start, int end, int count); 60 | bool CheckAvailable(int first, int count) const; 61 | 62 | int fMode; 63 | int fMaxVars; 64 | int fLocalStart; 65 | VarState* fStates; 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /compiler/VarTranslator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "VarTranslator.h" 15 | #include "Expr.h" 16 | #include "Stmt.h" 17 | #include "AssignStmt.h" 18 | #include "AtomExpr.h" 19 | #include "IncDecExpr.h" 20 | 21 | 22 | bool VarTranslator::operator()(Stmt *s) 23 | { 24 | vector v; 25 | s->GetExprs(v); 26 | 27 | int n = v.size(); 28 | int i; 29 | 30 | for(i=0; i(s)) 37 | t->Translate(fFrom, fTo); 38 | 39 | return true; 40 | } 41 | 42 | 43 | bool VarTranslator::operator()(Expr *e) 44 | { 45 | if (Translatable *t = dynamic_cast(e)) 46 | t->Translate(fFrom, fTo); 47 | 48 | return true; 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/VarTranslator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __VarTranslator_h 15 | #define __VarTranslator_h 16 | 17 | class Stmt; 18 | class Expr; 19 | 20 | class VarTranslator 21 | { 22 | public: 23 | VarTranslator(int from, int to) : fFrom(from), fTo(to) {} 24 | 25 | bool operator()(Stmt *s); 26 | bool operator()(Expr *e); 27 | private: 28 | int fFrom; 29 | int fTo; 30 | }; 31 | 32 | 33 | class Translatable 34 | { 35 | public: 36 | virtual void Translate(int from, int to) = 0; 37 | }; 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /compiler/Variable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | 15 | #ifndef __Variable_h 16 | #define __Variable_h 17 | 18 | enum Variable { 19 | // error values 20 | kIllegalVar = -1, 21 | kPhysicalVarBase = 0, 22 | kVirtualVarBase = 0x8000, 23 | kVirtualConstantFlag = 0x4000, // for vars that are always constant 24 | kVirtualReadOnlyFlag = 0x2000, // for vars that are not lvalues 25 | kPointerFlag = 0x1000, // for pointers 26 | kVirtualVarMask = 0x0fff 27 | }; 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /compiler/WhileStmt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "WhileStmt.h" 15 | #include "Bytecode.h" 16 | 17 | WhileStmt::WhileStmt(Expr *e, Stmt *s) : 18 | ChainStmt(s) 19 | { 20 | fCondition = e; 21 | } 22 | 23 | 24 | WhileStmt::~WhileStmt() 25 | { 26 | delete fCondition; 27 | } 28 | 29 | 30 | 31 | void WhileStmt::EmitActual(Bytecode &b) 32 | { 33 | bool optimized = false; 34 | int startLabel; 35 | int value; 36 | 37 | int cLabel = b.PushFlow(Bytecode::kContinueFlow); 38 | int bLabel = b.PushFlow(Bytecode::kBreakFlow); 39 | 40 | if (fCondition->Evaluate(value)) 41 | { 42 | if (value) 43 | { 44 | /* 45 | cPos: 46 | body 47 | jump -> 1 48 | */ 49 | b.SetLabel(cLabel); 50 | GetBody()->Emit(b); 51 | b.AddJump(cLabel); 52 | optimized = true; 53 | } 54 | else if (!GetBody()->GetMustEmit()) 55 | { 56 | // don't need to emit anything 57 | optimized = true; 58 | } 59 | } 60 | 61 | if (!optimized) 62 | { 63 | /* jump -> cPos 64 | startLabel: 65 | body 66 | cPos: 67 | test C -> startLabel 68 | */ 69 | 70 | int initialPosition = b.GetLength(); 71 | b.AddJump(cLabel); 72 | startLabel = b.NewLabel(); 73 | GetBody()->Emit(b); 74 | 75 | // if no body code has been emitted, then remove the 76 | // initial jump 77 | if (b.GetLabelPosition(startLabel) == b.GetLength()) { 78 | b.Truncate(initialPosition); 79 | b.SetLabel(startLabel); 80 | } 81 | 82 | b.SetLabel(cLabel); 83 | fCondition->EmitBranch(b, startLabel, true); 84 | } 85 | 86 | b.SetLabel(bLabel); 87 | b.PopFlow(Bytecode::kContinueFlow); 88 | b.PopFlow(Bytecode::kBreakFlow); 89 | } 90 | 91 | 92 | Stmt* WhileStmt::CloneActual(Mapping *b) const 93 | { 94 | return new WhileStmt(fCondition->Clone(b), GetBody()->Clone(b)); 95 | } 96 | -------------------------------------------------------------------------------- /compiler/WhileStmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __WhileStmt_h 15 | #define __WhileStmt_h 16 | 17 | #ifndef __Stmt_h 18 | #include "Stmt.h" 19 | #endif 20 | 21 | #ifndef __Expr_h 22 | #include "Expr.h" 23 | #endif 24 | 25 | class WhileStmt : public ChainStmt 26 | { 27 | public: 28 | WhileStmt(Expr *c, Stmt *s); 29 | ~WhileStmt(); 30 | 31 | void EmitActual(Bytecode &b); 32 | Stmt* CloneActual(Mapping *b) const; 33 | virtual void GetExprs(vector & v) const { v.push_back(fCondition); } 34 | 35 | private: 36 | Expr* fCondition; 37 | }; 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /compiler/build.bat: -------------------------------------------------------------------------------- 1 | @set BISON_SIMPLE=c:\cbuilderx\bin\bison.simple 2 | @bison -y -d parse.y 3 | @copy y_tab.c parse.cpp 4 | @copy y_tab.h parse.tab.h 5 | -------------------------------------------------------------------------------- /compiler/parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __parser_h 15 | #define __parser_h 16 | 17 | // forward class declarations for YYSTYPE 18 | class Fragment; 19 | class Resource; 20 | class Stmt; 21 | class BlockStmt; 22 | class Symbol; 23 | class Condition; 24 | class Expr; 25 | class Buffer; 26 | class FunctionDef; 27 | class CallStmt; 28 | class AsmStmt; 29 | class Field; 30 | class CaseStmt; 31 | class DeclareStmt; 32 | class LocationNode; 33 | 34 | // get definition of YYSTYPE, yylval, etc 35 | #ifndef __PARSE_TAB_H 36 | #include "parse.tab.h" 37 | int yylex(YYSTYPE &tokenVal); 38 | #endif 39 | 40 | #ifndef _STDIO_H 41 | #include 42 | #endif 43 | 44 | // from parse.tab.c 45 | int yyparse(void); 46 | 47 | #ifndef __LexLocation_h 48 | #include "LexLocation.h" 49 | #endif 50 | 51 | void LexCurrentLocation(LexLocation &loc); 52 | int LexFindAndPushFile(const char *name); 53 | int LexPush(Buffer *buf); 54 | void LexReturnWhitespace(int mode); 55 | void LexReset(); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /compiler/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __version_h 15 | #define __version_h 16 | 17 | #define MAJOR_VERSION 4 18 | #define MINOR_VERSION 0 19 | #define PATCH_VERSION 0 20 | 21 | // 0=alpha, 2=release 22 | #define RELEASE_STAGE "r" 23 | #define RELEASE_STAGE_INDEX 0 24 | 25 | #define RELEASE_BUILD 2 26 | 27 | #define STR_(x) #x 28 | #define STR(x) STR_(x) 29 | 30 | #define VERSION_STRING STR(MAJOR_VERSION) "." STR(MINOR_VERSION) "." STR(PATCH_VERSION) "\0" 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /default/fastdl.srec: -------------------------------------------------------------------------------- 1 | S02100006275696C642F68383330302F7263786C69622F66617374646C2E7372656331 2 | S11380007906000F6B86EE807906EE646DF67906CC 3 | S1138010EE745E003B9A0B877906EE5E5E0006887E 4 | S11380207FD87250FE673ED918EE6A8EEF51FE0279 5 | S11380306A8EEF06FE0D6A8EEE5E5470446F2079F0 6 | S11380406F7520627974652C207768656E204920ED 7 | S10B80506B6E6F636B3F0000CF 8 | S90380007C 9 | -------------------------------------------------------------------------------- /default/fastdl_odd.srec: -------------------------------------------------------------------------------- 1 | S02500006275696C642F68383330302F7263786C69622F66617374646C5F6F64642E7372656397 2 | S11380007906000F6B86EE807906EE646DF67906CC 3 | S1138010EE745E003B9A0B877906EE5E5E0006887E 4 | S1138020FE673ED918EE6A8EEF51FE026A8EEF06A5 5 | S1138030FE0D6A8EEE5E5470446F20796F75206277 6 | S11380407974652C207768656E2049206B6E6F63A8 7 | S10780506B3F00007E 8 | S90380007C 9 | -------------------------------------------------------------------------------- /doc/NQC_Guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrickBot/nqc/1b2378d9d3814f0f6e8346bba5fd3a9add67ca09/doc/NQC_Guide.pdf -------------------------------------------------------------------------------- /doc/NQC_Manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrickBot/nqc/1b2378d9d3814f0f6e8346bba5fd3a9add67ca09/doc/NQC_Manual.pdf -------------------------------------------------------------------------------- /doc/rcx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrickBot/nqc/1b2378d9d3814f0f6e8346bba5fd3a9add67ca09/doc/rcx.png -------------------------------------------------------------------------------- /mkdata.cbx.local: -------------------------------------------------------------------------------- 1 | buildhistory.win32b_Preprocess[0]=Target settings 2 | buildhistory.win32b_bcc32[0]=Paths and Defines 3 | buildhistory.win32b_brcc32[0]=Paths and Defines 4 | buildhistory.win32b_ilink32[0]=Target settings 5 | buildtool_history_project.win32b[0]=ilink32 6 | -------------------------------------------------------------------------------- /mkdata/mkdata.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SRecord.h" 4 | 5 | using std::fopen; 6 | using std::strcmp; 7 | 8 | //static char *sTestArgs[]={"mkdata", "rcx.nqh", "rcx_data.h", "rcxData" }; 9 | static const char *sTestArgs[] = {"mkdata", "-s", "fastdl.srec", "RCX_nub.h", "rcxNub" }; 10 | 11 | 12 | int main(int argc, const char *argv[]) 13 | { 14 | FILE *src; 15 | FILE *dst; 16 | int n; 17 | bool srecMode = false; 18 | SRecord srec; 19 | 20 | if (argc == 0) 21 | { 22 | // special case for debugging under the Metrowerks console 23 | argv = sTestArgs; 24 | argc = sizeof(sTestArgs) / sizeof(char *); 25 | } 26 | 27 | if (argc == 5 && (strcmp(argv[1],"-s")==0)) 28 | { 29 | for(int i=1; i<4; i++) 30 | argv[i]=argv[i+1]; 31 | argc--; 32 | srecMode = true; 33 | } 34 | 35 | if (argc != 4) 36 | { 37 | fprintf(stderr, "Usage: mkdata [-s] sourceFile destFile arrayName\n"); 38 | return -1; 39 | } 40 | 41 | src = fopen(argv[1], "r"); 42 | if (!src) 43 | { 44 | fprintf(stderr, "Error: could not open %s\n", argv[1]); 45 | return -1; 46 | } 47 | if (srecMode && !srec.Read(src, 65536)) 48 | { 49 | fprintf(stderr, "Error: %s is not a valid S-Record file\n", argv[1]); 50 | return -1; 51 | } 52 | 53 | dst = fopen(argv[2], "w"); 54 | if (!dst) 55 | { 56 | fprintf(stderr, "Error: could not create %s\n", argv[2]); 57 | fclose(src); 58 | return -1; 59 | } 60 | 61 | fprintf(dst,"static const %s char %s[]={\n", srecMode ? "unsigned" : "", argv[3]); 62 | 63 | n = 0; 64 | 65 | if (srecMode) 66 | { 67 | const UByte *ptr = srec.GetData(); 68 | int length = srec.GetLength(); 69 | 70 | while(length--) 71 | { 72 | fprintf(dst,"0x%02x,", *ptr++); 73 | if (++n == 16) 74 | { 75 | n = 0; 76 | fputs("\n", dst); 77 | } 78 | } 79 | } 80 | else 81 | { 82 | int c; 83 | while((c=fgetc(src)) != EOF) 84 | { 85 | if (c=='\n') 86 | fputs("\'\\n\',", dst); 87 | else 88 | fprintf(dst,"%d,", c); 89 | 90 | if (++n == 16) 91 | { 92 | n = 0; 93 | fputs("\n", dst); 94 | } 95 | } 96 | } 97 | 98 | fprintf(dst,"};\n"); 99 | 100 | fclose(src); 101 | fclose(dst); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /nqc.cbx.local: -------------------------------------------------------------------------------- 1 | buildhistory.win32b_Preprocess[0]=Target settings 2 | buildhistory.win32b_bcc32[0]=Paths and Defines 3 | buildhistory.win32b_brcc32[0]=Paths and Defines 4 | buildhistory.win32b_ilink32[0]=Paths and Defines 5 | buildtool_history_project.win32b[0]=bcc32 6 | debugger.PreviousBreakpointConditions.1[0]=computedLen != argLen 7 | debugger.PreviousBreakpointConditions.2[0]=argLen == 7 8 | debugger.breakpointgroups.1[0]=Breakpoints 9 | debugger.breakpoints.1[0]=1 023 compiler/parse_util.cpp3 2551 11 10 1 10 11 Breakpoints0 0 0 -1 10 | debugger.breakpoints.2[0]=1 023 compiler/parse_util.cpp3 2341 11 10 1 10 11 Breakpoints0 0 0 -1 11 | debugger.breakpoints.3[0]=1 011 nqc/nqc.cpp3 6241 11 10 1 10 11 Breakpoints0 0 0 -1 12 | debugger.gdbAssemblyFlavor[0]=ATT disassembly 13 | debugger.previouswatches.1[0]=data 14 | debugger.previouswatches.2[0]=*(fData+3) 15 | debugger.previouswatches.3[0]=*(fData+2) 16 | debugger.previouswatches.4[0]=*(fData+1) 17 | debugger.previouswatches.5[0]=*fData 18 | editor.search.file_filter_history.1[0]=*.cpp; *.c; *.cc; *.hpp; *.h; *.hh 19 | editor.search.file_filter_history.2[0]=*.* 20 | history.files.active[0]=compiler/Error.h 21 | history.files.open.1[0]=compiler/Error.h,F;77,1741 22 | history.files.open.2[0]=nqc/nqc.cpp,F;1079,24002 23 | team.ActiveVCS[0]=CVS 24 | team.CvsAccessMethod[0]=local 25 | team.CvsAutosaveEnabled[0]=1 26 | team.CvsModule[0]=nqc 27 | team.CvsPruneEmptyDirs[0]=1 28 | team.CvsRemoteShell[0]= 29 | team.CvsRepository[0]=c:\winapps\projects\nqc 30 | team.CvsServer[0]= 31 | team.CvsShowConsole[0]=0 32 | team.CvsUsername[0]=jhansen 33 | -------------------------------------------------------------------------------- /nqc/Carbon.r: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrickBot/nqc/1b2378d9d3814f0f6e8346bba5fd3a9add67ca09/nqc/Carbon.r -------------------------------------------------------------------------------- /nqc/CmdLine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __CmdLine_h 15 | #define __CmdLine_h 16 | 17 | #include 18 | #include 19 | 20 | using std::atoi; 21 | using std::vector; 22 | 23 | class CmdLine 24 | { 25 | public: 26 | CmdLine() : fPos(0) {}; 27 | ~CmdLine(); 28 | 29 | void Add(const char *a); // make copy and add to arglist 30 | void Add(int argc, const char * const *argv); // add multiple args 31 | 32 | void Parse(const char *line, int skip = 0); // parse line and add args 33 | 34 | int Remain() const { return fArgs.size() - fPos; } 35 | const char *Next() { return fArgs[fPos++]; } 36 | int NextInt() { return atoi(Next()); } 37 | 38 | private: 39 | vector fArgs; 40 | int fPos; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /nqc/DirList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "StrlUtil.h" 16 | 17 | #if defined(__MWERKS__) && (!__MACH__) 18 | #include 19 | #else 20 | #include 21 | #endif 22 | #include "DirList.h" 23 | 24 | using std::strcat; 25 | using std::strlen; 26 | using std::size_t; 27 | 28 | #ifdef DEBUG 29 | using std::strcpy; 30 | #endif 31 | 32 | DirList::~DirList() 33 | { 34 | Entry *e; 35 | 36 | while((e=fEntries.RemoveHead()) != 0) 37 | delete e; 38 | } 39 | 40 | 41 | void DirList::Add(const char *path) 42 | { 43 | // ignore NULL or empty path 44 | if (path==0 || *path==0) return; 45 | 46 | Entry *e = new Entry(path); 47 | fEntries.InsertTail(e); 48 | } 49 | 50 | 51 | bool DirList::Find(const char *filename, char *pathname) 52 | { 53 | struct stat stat_buf; 54 | 55 | size_t len = sizeof(pathname); 56 | if (StrlUtil::strlcpy(pathname, filename, len) >= len) { 57 | return false; 58 | } 59 | 60 | if (stat(pathname, &stat_buf) == 0) { 61 | return true; 62 | } 63 | 64 | for(Entry *e = fEntries.GetHead(); e; e=e->GetNext()) { 65 | if (StrlUtil::strlcpy(pathname, e->GetPath(), len) < len) { 66 | if (StrlUtil::strlcat(pathname, filename, len) < len) { 67 | if (stat(pathname, &stat_buf) == 0) { 68 | return true; 69 | } 70 | } 71 | } 72 | } 73 | 74 | return false; 75 | } 76 | 77 | 78 | DirList::Entry::Entry(const char *path) 79 | { 80 | size_t length = strlen(path); 81 | 82 | fPath = new char[length+2]; // leave room for terminating delimiter 83 | strcpy(fPath, path); 84 | 85 | // should we append the delimiter? 86 | if (path[length-1] != DIR_DELIMITER) { 87 | fPath[length] = DIR_DELIMITER; 88 | fPath[length+1] = 0; 89 | } 90 | } 91 | 92 | 93 | DirList::Entry::~Entry() 94 | { 95 | delete [] fPath; 96 | } 97 | -------------------------------------------------------------------------------- /nqc/DirList.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __DirList_h 15 | #define __DirList_h 16 | 17 | #ifndef __PListS_h 18 | #include "PListS.h" 19 | #endif 20 | 21 | class DirList 22 | { 23 | public: 24 | 25 | enum { 26 | kMaxPathname = 1024 27 | }; 28 | 29 | ~DirList(); 30 | 31 | void Add(const char *dirspec); 32 | bool Find(const char *filename, char *pathname); 33 | 34 | private: 35 | class Entry : public PLinkS 36 | { 37 | public: 38 | Entry(const char *path); 39 | ~Entry(); 40 | 41 | const char* GetPath() const { return fPath; } 42 | 43 | private: 44 | char* fPath; 45 | }; 46 | 47 | PListS fEntries; 48 | }; 49 | 50 | 51 | /* 52 | * the following conditions attempt to set DIR_DELIMITER base 53 | * on the intended target. They may be overridden by defining 54 | * DIR_DELIMITER prior to compilation. 55 | */ 56 | 57 | #if !defined(DIR_DELIMITER) 58 | #if defined(macintosh) 59 | #define DIR_DELIMITER ':' 60 | #elif defined(WIN32) 61 | #define DIR_DELIMITER '\\' 62 | #else 63 | // assume unix 64 | #define DIR_DELIMITER '/' 65 | #endif 66 | #endif 67 | 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /nqc/SRecord.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __SRecord_h 15 | #define __SRecord_h 16 | 17 | #ifndef __PTypes_h 18 | #include "PTypes.h" 19 | #endif 20 | 21 | #include 22 | 23 | using std::FILE; 24 | 25 | class SRecord 26 | { 27 | public: 28 | SRecord() : fLength(0), fStart(0), fData(0) {} 29 | ~SRecord() { delete [] fData; } 30 | 31 | int GetLength() const { return fLength; } 32 | const UByte* GetData() const { return fData; } 33 | int GetStart() const { return fStart; } 34 | 35 | bool Read(FILE *fp, int maxLength); 36 | 37 | static int ReadHexByte(const char *ptr); 38 | 39 | private: 40 | int fLength; 41 | int fStart; 42 | UByte* fData; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /nqc/debug_env.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | /* 15 | * This file is strictly for the Macintosh debug build and can be used 16 | * to test the effects of different environment variable settings by 17 | * adding the appropriate name/value pair to sEnvironment. Most release 18 | * builds of NQC rely on a real version of getenv() that actually reads 19 | * environment variables. 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | #if defined(__cplusplus) && defined(_MSL_USING_NAMESPACE) 26 | namespace std { 27 | #endif 28 | 29 | static char *sEnvironment[]= 30 | { 31 | // "NQC_INCLUDE", "::nqc_src:", 32 | 33 | // "NQC_OPTIONS", "-Trcx2 -v -raw 120e00", 34 | "NQC_OPTIONS", "-Susb -Tspy -l foo.nqc", 35 | // "NQC_OPTIONS", "-firmfast firm0330.lgo", 36 | // "RCX_PORT", "/dev/cu.USA19QI21P1.1", 37 | // "RCX_PORT", "/dev/cu.IrDA-IrCOMMch-b", 38 | "RCX_PORT", "usb", 39 | }; 40 | 41 | 42 | #define kEnvCount (sizeof(sEnvironment) / (2 * sizeof(const char *))) 43 | 44 | // hack to disable SIOUX delays 45 | #ifdef SIOUX 46 | extern "C" Boolean SIOUXUseWaitNextEvent; /* Can we use WaitNextEvent? ...*/ 47 | #endif 48 | 49 | char* getenv(const char *name) 50 | { 51 | #ifdef SIOUX 52 | static bool inited = false; 53 | if (!inited) { 54 | printf("\n"); 55 | SIOUXUseWaitNextEvent = false; 56 | inited = true; 57 | } 58 | #endif 59 | int i; 60 | 61 | for(i=0; i 15 | #define DEBUG 16 | #define PDEBUG 17 | -------------------------------------------------------------------------------- /nqc/version.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrickBot/nqc/1b2378d9d3814f0f6e8346bba5fd3a9add67ca09/nqc/version.rc -------------------------------------------------------------------------------- /platform/PDebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __PDebug_h 15 | #define __PDebug_h 16 | 17 | #define OUTFILE stderr 18 | #define NCQ_LOGFILE "NQC_LOGFILE" 19 | 20 | #ifdef PDEBUG 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | #if TARGET_API_MAC_OS8==1 25 | #define _p_debugstr(msg) debugstr(msg) 26 | #else 27 | extern void _p_debugstr(const char *text); 28 | extern void _p_debugvar(const char* varname, const int); 29 | #endif // macintosh 30 | 31 | // normal debugging 32 | extern void _p_require(const char* file, int line, const char* label); 33 | extern void _p_requirenot(const char* file, int line, int val, const char* label); 34 | extern void _p_assert(const char *file, int line); 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | // debugging 39 | 40 | #define PASSERT(expr) do { if (!(expr)) _p_assert(__FILE__, __LINE__); } while(0) 41 | #define PDEBUGSTR(mesg) _p_debugstr(mesg) 42 | #define PDEBUGVAR(varname, var) _p_debugvar(varname, var) 43 | #define PREQUIRE(expr, label) do { if (!(expr)) { _p_require(__FILE__, __LINE__, #label); goto label; }} while (0) 44 | #define PREQUIRENOT(expr, label) do { if (expr) { _p_requirenot(__FILE__, __LINE__, (int)(expr), #label); goto label; }} while (0) 45 | 46 | #else 47 | // no debugging 48 | 49 | #define PASSERT(expr) ((void)0) 50 | #define PDEBUGSTR(mesg) ((void)0) 51 | #define PDEBUGVAR(varname, var) ((void)0) 52 | #define PREQUIRE(expr, label) do { if (!(expr)) goto label; } while (0) 53 | #define PREQUIRENOT(expr, label) do { if (expr) goto label; } while (0) 54 | 55 | #endif // PDEBUG 56 | 57 | 58 | #endif // __PDebug_h 59 | -------------------------------------------------------------------------------- /platform/PHashTable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "PHashTable.h" 16 | 17 | #ifdef DEBUG 18 | using std::strcmp; 19 | #endif 20 | 21 | PHashable::~PHashable() { 22 | } 23 | 24 | 25 | bool PHashable::MatchKey(const char *key) { 26 | return strcmp(key, fKey)==0 ? true : false; 27 | } 28 | 29 | 30 | P_HashTable::P_HashTable(int size) { 31 | fBuckets = new PListSS[size]; 32 | fSize = size; 33 | } 34 | 35 | 36 | P_HashTable::~P_HashTable() { 37 | delete [] fBuckets; 38 | } 39 | 40 | 41 | PHashable* P_HashTable::_Find(const char *key) { 42 | PHashable *ptr; 43 | int hash = Hash(key); 44 | 45 | for (ptr=fBuckets[hash].GetHead(); ptr; ptr=ptr->GetNext()) { 46 | if (ptr->MatchKey(key)) return ptr; 47 | } 48 | 49 | return nil; 50 | } 51 | 52 | 53 | void P_HashTable::Add(PHashable *item) { 54 | int hash = Hash(item->GetKey()); 55 | 56 | fBuckets[hash].InsertHead(item); 57 | } 58 | 59 | 60 | bool P_HashTable::Remove(PHashable *item) { 61 | int hash = Hash(item->GetKey()); 62 | 63 | return fBuckets[hash].Remove(item); 64 | } 65 | 66 | 67 | void P_HashTable::DeleteAll() { 68 | int i; 69 | PHashable *item; 70 | 71 | for (i=0; i> 24); 86 | h = h ^ g; 87 | } 88 | } 89 | 90 | return (int)(h % fSize); 91 | } 92 | 93 | -------------------------------------------------------------------------------- /platform/PHashTable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __PHashTable_h 15 | #define __PHashTable_h 16 | 17 | #ifndef __PListS_h 18 | #include "PListS.h" 19 | #endif 20 | 21 | #ifndef __PTypes_h 22 | #include "PTypes.h" 23 | #endif 24 | 25 | 26 | class PHashable : public PLinkS { 27 | public: 28 | virtual ~PHashable(); 29 | 30 | bool MatchKey(const char *key); 31 | const char* GetKey() const { return fKey; } 32 | 33 | protected: 34 | char* fKey; 35 | }; 36 | 37 | 38 | class P_HashTable { 39 | public: 40 | P_HashTable(int size); 41 | ~P_HashTable(); 42 | 43 | bool Remove(PHashable *item); 44 | void Add(PHashable *item); 45 | 46 | void DeleteAll(); 47 | 48 | int Hash(const char *key); 49 | 50 | int GetBucketCount() const { return fSize; } 51 | 52 | protected: 53 | PHashable* _GetBucket(int i) { return fBuckets[i].GetHead(); } 54 | PHashable* _Find(const char *key); 55 | 56 | private: 57 | int fSize; 58 | PListSS* fBuckets; 59 | }; 60 | 61 | 62 | template class PHashTable : public P_HashTable 63 | { 64 | public: 65 | PHashTable(int size) : P_HashTable(size) {} 66 | 67 | T* Find(const char *key) { return (T*) _Find(key); } 68 | T* GetBucket(int i) { return (T*) _GetBucket(i); } 69 | }; 70 | 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /platform/PListS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "PListS.h" 15 | 16 | void P_ListS::InsertHead(P_LinkS* link) 17 | { 18 | link->fNext = fHead; 19 | fHead = link; 20 | if (!fTail) 21 | fTail = link; 22 | } 23 | 24 | 25 | void P_ListS::InsertTail(P_LinkS* link) 26 | { 27 | link->fNext = nil; 28 | if (fTail) 29 | fTail->fNext = link; 30 | else 31 | fHead = link; 32 | fTail = link; 33 | } 34 | 35 | 36 | P_LinkS *P_ListS::_RemoveHead() 37 | { 38 | P_LinkS *link = fHead; 39 | 40 | if (link) 41 | { 42 | fHead = link->fNext; 43 | link->fNext = nil; 44 | if (fHead==nil) 45 | fTail = nil; 46 | } 47 | 48 | return link; 49 | } 50 | 51 | 52 | P_LinkS* P_ListSS::_RemoveHead() 53 | { 54 | P_LinkS *link = fHead; 55 | 56 | if (link) 57 | { 58 | fHead = link->fNext; 59 | link->fNext = nil; 60 | } 61 | 62 | return link; 63 | } 64 | 65 | 66 | void P_ListSS::InsertHead(P_LinkS* link) 67 | { 68 | link->fNext = fHead; 69 | fHead = link; 70 | } 71 | 72 | 73 | bool P_ListSS::Remove(P_LinkS *link) 74 | { 75 | P_LinkS* prev = nil; 76 | P_LinkS* l; 77 | 78 | for(l=fHead; l; l=l->fNext) 79 | { 80 | if (l==link) 81 | { 82 | // unlink and return 83 | if (prev) 84 | prev->fNext = l->fNext; 85 | else 86 | fHead = l->fNext; 87 | l->fNext = nil; 88 | return true; 89 | } 90 | prev = l; 91 | } 92 | 93 | return false; 94 | } 95 | 96 | 97 | bool P_ListS::Remove(P_LinkS *link) 98 | { 99 | P_LinkS* prev = nil; 100 | P_LinkS* l; 101 | 102 | for(l=fHead; l; l=l->fNext) 103 | { 104 | if (l==link) 105 | { 106 | // unlink and return 107 | if (prev) 108 | prev->fNext = l->fNext; 109 | else 110 | fHead = l->fNext; 111 | if (fTail==link) 112 | fTail = prev; 113 | l->fNext = nil; 114 | return true; 115 | } 116 | prev = l; 117 | } 118 | 119 | return false; 120 | } 121 | -------------------------------------------------------------------------------- /platform/PSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __PSerial_h 15 | #define __PSerial_h 16 | 17 | #ifndef __PTypes_h 18 | #include "PTypes.h" 19 | #endif 20 | 21 | #ifndef __PStream_h 22 | #include "PStream.h" 23 | #endif 24 | 25 | 26 | // serial port settings 27 | #define kPSerial_DataMask 0x03 28 | #define kPSerial_DataShift 0 29 | #define kPSerial_Data8 (0 << kPSerial_DataShift) 30 | #define kPSerial_Data7 (1 << kPSerial_DataShift) 31 | #define kPSerial_Data6 (2 << kPSerial_DataShift) 32 | #define kPSerial_Data5 (3 << kPSerial_DataShift) 33 | 34 | #define kPSerial_ParityMask 0x0c 35 | #define kPSerial_ParityShift 2 36 | #define kPSerial_ParityNone (0 << kPSerial_ParityShift) 37 | #define kPSerial_ParityOdd (1 << kPSerial_ParityShift) 38 | #define kPSerial_ParityEven (2 << kPSerial_ParityShift) 39 | 40 | #define kPSerial_StopMask 0x30 41 | #define kPSerial_StopShift 4 42 | #define kPSerial_Stop1 (0 << kPSerial_StopShift) 43 | #define kPSerial_Stop1_5 (1 << kPSerial_StopShift) 44 | #define kPSerial_Stop2 (2 << kPSerial_StopShift) 45 | 46 | 47 | class PSerial : public PStream 48 | { 49 | public: 50 | virtual bool Open(const char *name) = 0; 51 | 52 | virtual bool SetSpeed(int speed, int opts = 0) = 0; 53 | virtual bool SetDTR(bool /* assert */) { return false; } 54 | virtual bool SetRTS(bool /* assert */) { return false; } 55 | void SetIsSpy(bool value) { fSpy = value; } 56 | bool GetIsSpy(void) { return fSpy; } 57 | 58 | static PSerial* NewSerial(); 59 | static const char* GetDefaultName(); 60 | 61 | protected: 62 | bool fSpy; 63 | }; 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /platform/PSerial_none.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "PSerial.h" 15 | 16 | class PSerial_none : public PSerial 17 | { 18 | public: 19 | virtual bool Open(const char *) { return false; } 20 | virtual void Close() {} 21 | virtual long Write(const void *, long ) { return 0; } 22 | virtual long Read(void *, long ) { return 0; } 23 | virtual bool SetSpeed(int , int) { return false; } 24 | 25 | private: 26 | }; 27 | 28 | 29 | PSerial* PSerial::NewSerial() 30 | { 31 | return new PSerial_none(); 32 | } 33 | 34 | 35 | const char *PSerial::GetDefaultName() 36 | { 37 | return "none"; 38 | } 39 | -------------------------------------------------------------------------------- /platform/PStream.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "PStream.h" 15 | 16 | bool PStream::ReadLine(char *ptr, int max) 17 | { 18 | bool ok = false; 19 | 20 | while(max > 1) { 21 | if (Read(ptr, 1) != 1) break; 22 | max--; 23 | if (*ptr == '\n') { 24 | ptr++; 25 | ok = true; 26 | break; 27 | } 28 | ptr++; 29 | } 30 | 31 | *ptr = 0; 32 | return ok; 33 | } 34 | 35 | 36 | bool PStream::SetTimeout(long timeout_ms) 37 | { 38 | return (timeout_ms == kPStream_NeverTimeout); 39 | } 40 | 41 | 42 | bool PStream::SetBlocking(bool blocking) 43 | { 44 | return SetTimeout(blocking ? kPStream_NeverTimeout : 0); 45 | } 46 | 47 | 48 | void PStream::FlushWrite() 49 | { 50 | } 51 | -------------------------------------------------------------------------------- /platform/PStream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __PStream_h 15 | #define __PStream_h 16 | 17 | #ifndef __PTypes_h 18 | #include "PTypes.h" 19 | #endif 20 | 21 | // timeout modes 22 | #define kPStream_NeverTimeout -1 23 | 24 | 25 | class PStream 26 | { 27 | public: 28 | PStream() { fOpen = false; } 29 | virtual ~PStream() { Close(); } 30 | 31 | // Open(...) methods declared in subclasses 32 | 33 | virtual void Close() { fOpen = false; } 34 | virtual long Read(void *ptr, long count) = 0; 35 | virtual long Write(const void *ptr, long count) = 0; 36 | 37 | virtual void FlushWrite(); 38 | virtual bool SetTimeout(long timeout_ms = kPStream_NeverTimeout); 39 | 40 | virtual bool ReadLine(char *ptr, int max); 41 | 42 | bool SetBlocking(bool blocking = true); // backwards compat. call to SetTimeout() 43 | 44 | protected: 45 | bool fOpen; 46 | }; 47 | 48 | class PSeekStream : public PStream 49 | { 50 | public: 51 | virtual long GetSize() const = 0; 52 | virtual long GetPos() const = 0; 53 | virtual bool SetPos(long pos) = 0; 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /platform/PTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __PTypes_h 15 | #define __PTypes_h 16 | 17 | typedef unsigned long ULong; 18 | typedef unsigned short UShort; 19 | typedef unsigned char UByte; 20 | 21 | #ifndef nil 22 | #define nil 0 23 | #endif 24 | 25 | #ifdef PNEED_BOOL 26 | typedef char bool; 27 | 28 | #ifndef false 29 | #define false ((bool)0) 30 | #endif 31 | 32 | #ifndef true 33 | #define true ((bool)1) 34 | #endif 35 | #endif 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /platform/StrlUtil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | */ 6 | #ifndef __StrlUtil_h 7 | #define __StrlUtil_h 8 | 9 | class StrlUtil 10 | { 11 | public: 12 | static size_t strlcpy(char *dst, const char *src, size_t siz); 13 | static size_t strlcat(char *dst, const char *src, size_t siz); 14 | 15 | }; 16 | 17 | #endif // __StrlUtil_h 18 | -------------------------------------------------------------------------------- /rcxlib/RCX_GhostTransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | /* 15 | * This Pipe is layered on Lego's Ghost API. It has certain limitations 16 | * which make the RCX_SerialPipe preferable when serial port is being used. 17 | * However, for USB support on Mac OS 9, Ghost is the only viable option. 18 | * 19 | * Limitations of the GhostPipe: 20 | * - fast mode is not supported (no fast firmware download) 21 | * - always uses the USB Port for Ghost (no Ghost serial support) 22 | * - deviceName is ignored, first USB device is always selected 23 | * - user specified Rx Timeout is ignored (default Ghost timing always used) 24 | */ 25 | 26 | #ifndef __RCX_GhostTransport_h 27 | #define __RCX_GhostTransport_h 28 | 29 | #ifndef __RCX_Transport_h 30 | #include "RCX_Transport.h" 31 | #endif 32 | 33 | #ifndef __GHOSTAPI_H__ 34 | #include "GhostAPI.h" 35 | #endif 36 | 37 | #include 38 | 39 | using std::vector; 40 | 41 | class RCX_GhostTransport : public RCX_Transport 42 | { 43 | public: 44 | RCX_GhostTransport(); 45 | virtual ~RCX_GhostTransport(); 46 | 47 | virtual RCX_Result Open(RCX_TargetType target, const char *deviceName, ULong options); 48 | virtual void Close(); 49 | 50 | virtual RCX_Result Send(const UByte *txData, int txLength, UByte *rxData, int rxExpected, int rxMax, bool retry); 51 | 52 | private: 53 | GHQUEUE CreateQueue(const UByte *txData, int txLength, int rxExpected); 54 | RCX_Result ExtractReply(GHQUEUE queue, UByte *rxData, int rxMax); 55 | 56 | GHSTACK fStack; 57 | bool fVerbose; 58 | bool fOpen; 59 | int32 fExRetries; 60 | int32 fDownRetries; 61 | }; 62 | 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /rcxlib/RCX_Log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_Log_h 15 | #define __RCX_Log_h 16 | 17 | #ifndef __RCX_Result_h 18 | #include "RCX_Result.h" 19 | #endif 20 | 21 | #ifndef __PTypes_h 22 | #include "PTypes.h" 23 | #endif 24 | 25 | 26 | class RCX_Link; 27 | 28 | class RCX_Log 29 | { 30 | public: 31 | RCX_Log(); 32 | ~RCX_Log(); 33 | 34 | RCX_Result Upload(RCX_Link *link); 35 | 36 | void SetLength(int length); 37 | 38 | int GetLength() const { return fLength; } 39 | UByte GetType(int index) const { return fTypes[index]; } 40 | short GetData(int index) const { return fData[index]; } 41 | void SPrintEntry(char *buf, int index, bool verbose) const; 42 | 43 | private: 44 | int fLength; 45 | UByte* fTypes; 46 | short* fData; 47 | }; 48 | 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /rcxlib/RCX_Pipe.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | /** 15 | * @file RCX_Pipe.cpp 16 | * @brief Superclass implementation of LEGO IR USB tower pipe 17 | */ 18 | 19 | #include "RCX_Pipe.h" 20 | #include 21 | 22 | void RCX_Pipe::FlushRead(int delay) 23 | { 24 | const int BUFFSIZE = 512; 25 | UByte * buff = new UByte[BUFFSIZE]; 26 | 27 | while (Read(buff, BUFFSIZE, delay) > 0) {;} 28 | 29 | delete[] buff; 30 | } 31 | -------------------------------------------------------------------------------- /rcxlib/RCX_Pipe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | /** 15 | * @file RCX_Pipe.h 16 | * @brief Virtual superclass for all platform specific USB IR tower pipe 17 | * implementations 18 | */ 19 | 20 | #ifndef __RCX_Pipe_h 21 | #define __RCX_Pipe_h 22 | 23 | #ifndef __RCX_Result_h 24 | #include "RCX_Result.h" 25 | #endif 26 | 27 | #include "PTypes.h" 28 | 29 | /** 30 | * Superclass for all platform specific USB IR tower pipes 31 | */ 32 | class RCX_Pipe 33 | { 34 | public: 35 | /// modes and flags 36 | enum { 37 | kNormalIrMode = 1 << 0, ///< 2400 baud, odd parity, etc 38 | kFastIrMode = 1 << 1, ///< 4800 baud (odd or no parity) 39 | kCyberMasterMode = 1 << 2, ///< normal IR + DTR/RTS for CyberMaster 40 | kSpyboticsMode = 1 << 3, ///< 4800 baud, odd parity, DTR/RTS 41 | 42 | kFastOddParityFlag = 1 << 28, ///< set if kFastIrMode uses odd parity 43 | kTxEchoFlag = 1 << 29, ///< set if Tx data gets echoed 44 | kAbsorb55Flag = 1 << 30 ///< set if pipe absorbs leading 0x55 45 | }; 46 | 47 | RCX_Pipe() {} 48 | virtual ~RCX_Pipe() {} 49 | 50 | 51 | virtual RCX_Result Open(const char *name, int mode) = 0; 52 | virtual void Close() = 0; 53 | 54 | virtual int GetCapabilities() const = 0; 55 | virtual RCX_Result SetMode(int mode) = 0; 56 | 57 | virtual long Read(void *ptr, long count, long timeout_ms) = 0; 58 | virtual long Write(const void *ptr, long count) = 0; 59 | virtual void FlushRead(int delay); 60 | virtual bool IsUSB() const { return false; } 61 | 62 | protected: 63 | private: 64 | }; 65 | 66 | 67 | /** 68 | * This is an operating system dependent factory method for creating 69 | * a pipe for a USB IR tower. 70 | */ 71 | RCX_Pipe* RCX_NewUSBTowerPipe(); 72 | 73 | /* 74 | * This is an operating system dependent factory method for creating 75 | * a pipe for TCP. 76 | */ 77 | RCX_Pipe* RCX_NewTcpPipe(); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /rcxlib/RCX_Result.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_Result_h 15 | #define __RCX_Result_h 16 | 17 | /** 18 | * @file RCX_Result.h 19 | * @brief Result and error codes for target actions 20 | */ 21 | typedef int RCX_Result; 22 | 23 | // Result codes 24 | #define kRCX_OK 0 ///< Success 25 | #define kRCX_OpenSerialError -1 ///< serial port could not be opened and/or configured 26 | #define kRCX_IREchoError -2 ///< no echo receieved from IR tower 27 | #define kRCX_ReplyError -3 ///< no (or invalid) reply from RCX 28 | #define kRCX_RequestError -4 ///< attempt to send too much data 29 | #define kRCX_FileError -5 ///< could not open/read/write file 30 | #define kRCX_FormatError -6 ///< unknown file format 31 | #define kRCX_AbortError -7 ///< canceled by RCX_Link::DownloadProgress 32 | #define kRCX_MemFullError -8 ///< not enough room in RCX program memory 33 | 34 | // New error codes as of 2.4a3 35 | #define kRCX_PipeModeError -9 ///< feature not supported by the pipe (e.g. CM target on IR tower) 36 | #define kRCX_USBUnsupportedError -10 ///< USB is not yet supported (depends on the OS) 37 | #define kRCX_GhostNotFoundError -11 ///< USB requires Ghost, which is not installed 38 | 39 | // new error codes for TCP support 40 | #define kRCX_TcpUnsupportedError -12 ///< TCP is not yet supported 41 | #define kRCX_UnknownTcpHostError -13 ///< unknown TCP host 42 | #define kRCX_OpenSocketError -14 ///< failure opening a TCP socket 43 | #define kRCX_BindPortError -15 ///< unable to bind port 44 | #define kRCX_TcpConnectError -16 ///< cannot connect 45 | 46 | #define kRCX_LastError -16 ///< Last error code so classes can hook their own codes in 47 | 48 | /// Convenience define to check if a code is an error condition. 49 | #define RCX_ERROR(result) ((result) < 0) 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /rcxlib/RCX_SerialPipe.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include "RCX_SerialPipe.h" 15 | 16 | 17 | RCX_SerialPipe::RCX_SerialPipe() : fSerial(0) 18 | { 19 | } 20 | 21 | 22 | RCX_SerialPipe::~RCX_SerialPipe() 23 | { 24 | Close(); 25 | } 26 | 27 | 28 | RCX_Result RCX_SerialPipe::Open(const char *name, int mode) 29 | { 30 | PSerial* s; 31 | 32 | s = PSerial::NewSerial(); 33 | if (! s->Open(name)) 34 | { 35 | delete s; 36 | return kRCX_OpenSerialError; 37 | } 38 | fSerial = s; 39 | 40 | RCX_Result err; 41 | err = SetMode(mode); 42 | if (RCX_ERROR(err)) 43 | { 44 | Close(); 45 | return err; 46 | } 47 | 48 | return err; 49 | } 50 | 51 | 52 | void RCX_SerialPipe::Close() 53 | { 54 | if (!fSerial) return; 55 | 56 | fSerial->Close(); 57 | delete fSerial; 58 | fSerial = 0; 59 | } 60 | 61 | 62 | int RCX_SerialPipe::GetCapabilities() const 63 | { 64 | return kNormalIrMode + kFastIrMode + kCyberMasterMode + kSpyboticsMode + kTxEchoFlag; 65 | } 66 | 67 | 68 | RCX_Result RCX_SerialPipe::SetMode(int mode) 69 | { 70 | fSerial->SetIsSpy(false); 71 | switch(mode) 72 | { 73 | case kNormalIrMode: 74 | fSerial->SetSpeed(2400, kPSerial_ParityOdd); 75 | return kRCX_OK; 76 | case kFastIrMode: 77 | fSerial->SetSpeed(4800); 78 | return kRCX_OK; 79 | case kCyberMasterMode: 80 | fSerial->SetSpeed(2400, kPSerial_ParityOdd); 81 | fSerial->SetDTR(true); 82 | fSerial->SetRTS(false); 83 | return kRCX_OK; 84 | case kSpyboticsMode: 85 | fSerial->SetSpeed(4800, kPSerial_ParityOdd); 86 | fSerial->SetDTR(true); 87 | fSerial->SetRTS(false); 88 | fSerial->SetIsSpy(true); 89 | return kRCX_OK; 90 | default: 91 | return kRCX_PipeModeError; 92 | } 93 | } 94 | 95 | 96 | long RCX_SerialPipe::Read(void *ptr, long count, long timeout_ms) 97 | { 98 | fSerial->SetTimeout(timeout_ms); 99 | return fSerial->Read(ptr, count); 100 | } 101 | 102 | 103 | long RCX_SerialPipe::Write(const void *ptr, long count) 104 | { 105 | long n = fSerial->Write(ptr, count); 106 | fSerial->FlushWrite(); 107 | return n; 108 | } 109 | -------------------------------------------------------------------------------- /rcxlib/RCX_SerialPipe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_SerialPipe_h 15 | #define __RCX_SerialPipe_h 16 | 17 | #ifndef __RCX_Pipe_h 18 | #include "RCX_Pipe.h" 19 | #endif 20 | 21 | #ifndef __PSerial_h 22 | #include "PSerial.h" 23 | #endif 24 | 25 | 26 | class RCX_SerialPipe : public RCX_Pipe 27 | { 28 | public: 29 | RCX_SerialPipe(); 30 | virtual ~RCX_SerialPipe(); 31 | 32 | virtual RCX_Result Open(const char *name, int mode); 33 | virtual void Close(); 34 | 35 | virtual int GetCapabilities() const; 36 | virtual RCX_Result SetMode(int mode); 37 | 38 | virtual long Read(void *ptr, long count, long timeout_ms); 39 | virtual long Write(const void *ptr, long count); 40 | 41 | private: 42 | PSerial* fSerial; 43 | }; 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /rcxlib/RCX_SourceTag.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_SourceTag_h 15 | #define __RCX_SourceTag_h 16 | 17 | /// A single tag that relates bytecode offset to source file/offset 18 | class RCX_SourceTag 19 | { 20 | public: 21 | /// Types of source tags 22 | enum { 23 | kBegin, ///< start of fragment source 24 | kBeginNoList, ///< begin a no-list fragment 25 | kNormal, ///< most tags are this type 26 | kEnd ///< end of fragment source 27 | }; 28 | 29 | int fType; 30 | short fAddress; 31 | short fSrcIndex; 32 | long fSrcOffset; 33 | }; 34 | 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /rcxlib/RCX_SpyboticsLinker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_SpyboticsLinker_h 15 | #define __RCX_SpyboticsLinker_h 16 | 17 | #ifndef __PTypes_h 18 | #include "PTypes.h" 19 | #endif 20 | 21 | #ifndef __RCX_Target_h 22 | #include "RCX_Target.h" 23 | #endif 24 | 25 | #ifndef __RCX_Image_h 26 | #include "RCX_Image.h" 27 | #endif 28 | 29 | #include 30 | 31 | using std::vector; 32 | 33 | class RCX_Image; 34 | 35 | class RCX_SpyboticsLinker 36 | { 37 | public: 38 | RCX_SpyboticsLinker(); 39 | ~RCX_SpyboticsLinker(); 40 | 41 | void Generate(const RCX_Image &image, vector &output); 42 | 43 | private: 44 | struct Slot 45 | { 46 | RCX_ChunkType fType; 47 | const RCX_Image::Chunk* fChunk; 48 | }; 49 | 50 | void PlaceChunks(const RCX_Image &image); 51 | void GenerateSlots(); 52 | 53 | void AddEntry(const UByte *data=0, int length=0); 54 | void AddData(const UByte *data, int length); 55 | 56 | const RCX_Target* fTarget; 57 | 58 | int fSlotCount; // number of chunks in TOC 59 | Slot* fSlots; 60 | int fSectionStarts[kRCX_ChunkTypeCount]; 61 | 62 | vector* fOutput; 63 | int fTocOffset; // current offset into TOC 64 | int fContentOffset; // current offset for new data 65 | }; 66 | 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /rcxlib/RCX_Target.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #ifndef __RCX_Target_h 15 | #define __RCX_Target_h 16 | 17 | #ifndef __RCX_Constants_h 18 | #include "RCX_Constants.h" 19 | #endif 20 | 21 | /** 22 | * @file RCX_Target.h 23 | * @brief LEGO brick target type definition 24 | */ 25 | 26 | /// Enumeration of all supported programmable brick targets 27 | typedef enum { 28 | kRCX_RCXTarget = 0, ///< Original RCX (i.e., RCX bricks with v.0309 or earlier firmwares) 29 | kRCX_CMTarget, ///< CyberMaster 30 | kRCX_ScoutTarget, ///< Scout 31 | kRCX_RCX2Target, ///< RCX 2.0 (i.e., RCX bricks with v.0328 or later firmwares) 32 | kRCX_SpyboticsTarget, ///< Spybotics 33 | kRCX_SwanTarget ///< Dick Swan's alternate firmware 34 | } RCX_TargetType; 35 | 36 | /// LEGO brick target type definition 37 | class RCX_Target 38 | { 39 | public: 40 | struct Range { 41 | int fBase; 42 | int fCount; 43 | }; 44 | 45 | RCX_TargetType fType; 46 | const char* fName; 47 | const char* fDefine; 48 | const char* fDefValue; 49 | 50 | Range fRanges[kRCX_ChunkTypeCount]; 51 | 52 | int fMaxGlobalVars; 53 | int fMaxTaskVars; 54 | bool fResources; 55 | bool fEvents; 56 | bool fArrays; 57 | bool fRestrictedMath; 58 | bool fSubParams; 59 | bool fHasExtendedMathOps; 60 | 61 | bool SourceWritable(int source) const; 62 | int GetChunkLimit(RCX_ChunkType type) const; 63 | }; 64 | 65 | extern const RCX_Target *getTarget(RCX_TargetType type); 66 | 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /rcxlib/RCX_TcpPipe_none.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The contents of this file are subject to the Mozilla Public License 3 | * Version 1.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.mozilla.org/MPL/ 6 | * 7 | * Software distributed under the License is distributed on an "AS IS" 8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 9 | * License for the specific language governing rights and limitations 10 | * under the License. 11 | * 12 | * The Initial Developer of this code is David Baum. 13 | * Portions created by David Baum are Copyright (C) 1998 David Baum. 14 | * All Rights Reserved. 15 | * 16 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 17 | * All Rights Reserved. 18 | * 19 | */ 20 | 21 | #include "RCX_Pipe.h" 22 | 23 | /* 24 | * This file should be added if TCP is not supported on the platform 25 | */ 26 | 27 | RCX_Pipe* RCX_NewTcpPipe() 28 | { 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /rcxlib/RCX_Transport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * The Initial Developer of this code is David Baum. 7 | * Portions created by David Baum are Copyright (C) 1999 David Baum. 8 | * All Rights Reserved. 9 | * 10 | * Portions created by John Hansen are Copyright (C) 2005 John Hansen. 11 | * All Rights Reserved. 12 | * 13 | */ 14 | #include 15 | #include "RCX_Transport.h" 16 | #include "RCX_Target.h" 17 | 18 | using std::printf; 19 | using std::putchar; 20 | 21 | void RCX_Transport::DumpData(const UByte *ptr, int length) 22 | { 23 | int i; 24 | 25 | for (i=0; i 24 | */ 25 | 26 | .lsym rom_init_handlers, 0x0688 27 | .lsym init_timer, 0x3b9a 28 | 29 | .lsym rom_main_loop_state, 0xee5e 30 | .lsym rom_dispatch_struct, 0xee64 31 | .lsym rom_counter_struct, 0xee74 32 | .lsym rom_power_off_minutes, 0xee80 33 | .lsym rom_update_function_state, 0xef06 34 | .lsym rom_use_complements_flag, 0xef51 35 | 36 | .global __start 37 | 38 | __start: 39 | 40 | ; Set up as if ROM were waking RCX up 41 | 42 | mov.w #15,r6 43 | mov.w r6,@rom_power_off_minutes 44 | 45 | mov.w #rom_dispatch_struct,r6 46 | push r6 47 | mov.w #rom_counter_struct,r6 48 | jsr @init_timer 49 | adds #2,r7 50 | 51 | mov.w #rom_main_loop_state,r6 52 | jsr @rom_init_handlers 53 | 54 | .ifndef odd 55 | 56 | ; Turn off parity 57 | 58 | bclr #5,@0xd8:8 59 | 60 | .endif 61 | 62 | ; Double the baud rate 63 | 64 | mov.b #103,r6l 65 | mov.b r6l,@0xd9:8 66 | 67 | ; Turn off complements 68 | 69 | sub.b r6l,r6l 70 | mov.b r6l,@rom_use_complements_flag 71 | 72 | ; Hack the update function state so we don't hear two beeps 73 | 74 | mov.b #2,r6l 75 | mov.b r6l,@rom_update_function_state 76 | 77 | ; Hack the main loop state so we reenter ROM main loop correctly 78 | 79 | mov.b #13,r6l 80 | mov.b r6l,@rom_main_loop_state 81 | 82 | ; Return control back to ROM 83 | 84 | rts 85 | 86 | ; String needed for new firmware 87 | 88 | .string "Do you byte, when I knock?" 89 | -------------------------------------------------------------------------------- /rcxlib/nqc_rcxlib.cpp: -------------------------------------------------------------------------------- 1 | /* C/C++ ABI wrapper for NQC's rcxlib */ 2 | 3 | #include 4 | 5 | #include "RCX_Pipe.h"; 6 | 7 | #ifdef USE_TRANSPORT 8 | #include "RCX_Link.h" 9 | #include "RCX_PipeTransport.h" 10 | static RCX_PipeTransport *rcx_pipe_transport = 0; 11 | 12 | #define BUFFERSIZE 4096 13 | extern int __comm_debug; 14 | #endif 15 | 16 | #include "nqc_rcxlib.h" 17 | 18 | void *rcx_pipe_init() 19 | { 20 | RCX_Pipe *pipe = RCX_NewUSBTowerPipe(); 21 | #ifdef USE_TRANSPORT 22 | rcx_pipe_transport = new RCX_PipeTransport(pipe); 23 | if (rcx_pipe_transport->Open((RCX_TargetType) 0, "short", (__comm_debug ? RCX_Link::kVerboseMode : 0)) != kRCX_OK) { 24 | delete rcx_pipe_transport; 25 | rcx_pipe_transport = 0; 26 | exit(1); 27 | } 28 | #else 29 | pipe->Open("short", RCX_Pipe::kNormalIrMode); 30 | // pipe->SetMode(RCX_Pipe::kFastIrMode); 31 | #endif 32 | return (void *) pipe; 33 | } 34 | 35 | void rcx_pipe_close(void *pipe) 36 | { 37 | return ((RCX_Pipe *) pipe)->Close(); 38 | } 39 | 40 | long rcx_pipe_read(void *pipe, void *ptr, long count, long timeout_ms) 41 | { 42 | return ((RCX_Pipe *) pipe)->Read(ptr, count, timeout_ms); 43 | } 44 | 45 | long rcx_pipe_write(void *pipe, const void *ptr, long count) 46 | { 47 | return ((RCX_Pipe *) pipe)->Write(ptr, count); 48 | } 49 | 50 | #ifdef USE_TRANSPORT 51 | long rcx_pipe_send(void *pipe, void *send, int slen, void *recv, int rlen, 52 | int timeout, int retries, int use_comp) 53 | { 54 | long result = rcx_pipe_transport->Send((const UByte*) send, slen, (UByte*) recv, rlen, BUFFERSIZE, 1); 55 | if (!RCX_ERROR(result)) 56 | return rlen; 57 | else 58 | return result; 59 | } 60 | #endif 61 | -------------------------------------------------------------------------------- /rcxlib/nqc_rcxlib.h: -------------------------------------------------------------------------------- 1 | /* C/C++ ABI wrapper for NQC's rcxlib */ 2 | 3 | #if defined(__cplusplus) 4 | extern "C" { 5 | #endif 6 | 7 | void *rcx_pipe_init(); 8 | void rcx_pipe_close(void *); 9 | long rcx_pipe_read(void *, void *, long, long); 10 | long rcx_pipe_write(void *, const void *, long); 11 | long rcx_pipe_send(void *, void *, int, void *, int, int, int, int); 12 | 13 | #if defined(__cplusplus) 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /test.nqc: -------------------------------------------------------------------------------- 1 | #define BUTTON SENSOR_1 2 | #define MOTOR OUT_A 3 | 4 | task main() 5 | { 6 | #ifdef __RCX 7 | // RCX needs to be told what kind of sensor is used 8 | SetSensor(BUTTON, SENSOR_TOUCH); 9 | #endif 10 | 11 | On(MOTOR); 12 | 13 | until(BUTTON == 1); 14 | 15 | PlaySound(SOUND_CLICK); 16 | Off(MOTOR); 17 | } 18 | --------------------------------------------------------------------------------