00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __ASEBA_COMPILER_H
00025 #define __ASEBA_COMPILER_H
00026
00027 #include <vector>
00028 #include <deque>
00029 #include <string>
00030 #include <map>
00031 #include <set>
00032 #include <utility>
00033 #include <istream>
00034
00035 namespace Aseba
00036 {
00041
00042
00043 struct Node;
00044 struct ProgramNode;
00045 struct StatementNode;
00046 struct BinaryArithmeticNode;
00047
00049 struct TargetDescription
00050 {
00052 struct NamedVariable
00053 {
00054 NamedVariable(const std::string &name, unsigned size) : size(size), name(name) {}
00055 NamedVariable() {}
00056
00057 unsigned size;
00058 std::string name;
00059 };
00060
00062 struct LocalEvent
00063 {
00064 std::string name;
00065 std::string description;
00066 };
00067
00069 struct NativeFunctionParameter
00070 {
00071 int size;
00072 std::string name;
00073 };
00074
00076 struct NativeFunction
00077 {
00078 std::string name;
00079 std::string description;
00080 std::vector<NativeFunctionParameter> parameters;
00081 };
00082
00083 std::string name;
00084 unsigned protocolVersion;
00085
00086 unsigned bytecodeSize;
00087 unsigned variablesSize;
00088 unsigned stackSize;
00089
00090 std::vector<NamedVariable> namedVariables;
00091 std::vector<LocalEvent> localEvents;
00092 std::vector<NativeFunction> nativeFunctions;
00093
00094 TargetDescription() { variablesSize = bytecodeSize = stackSize = 0; }
00095 };
00096
00098 struct BytecodeElement
00099 {
00100 BytecodeElement();
00101 BytecodeElement(unsigned short bytecode) : bytecode(bytecode), line(0) { }
00102 BytecodeElement(unsigned short bytecode, unsigned short line) : bytecode(bytecode), line(line) { }
00103 operator unsigned short () const { return bytecode; }
00104
00105 unsigned short bytecode;
00106 unsigned short line;
00107 };
00108
00110 struct BytecodeVector: std::deque<BytecodeElement>
00111 {
00113 BytecodeVector() : maxStackDepth(0), callDepth(0), lastLine(0) { }
00114
00115 unsigned maxStackDepth;
00116 unsigned callDepth;
00117 unsigned lastLine;
00118
00119 void push_back(const BytecodeElement& be)
00120 {
00121 std::deque<BytecodeElement>::push_back(be);
00122 lastLine = be.line;
00123 }
00124 };
00125
00126
00127 struct PreLinkBytecode;
00128
00130 struct SourcePos
00131 {
00132 unsigned character;
00133 unsigned row;
00134 unsigned column;
00135 bool valid;
00136
00137 SourcePos(unsigned character, unsigned row, unsigned column) : character(character - 1), row(row), column(column - 1) { valid = true; }
00138 SourcePos() { valid = false; }
00139 std::string toString() const;
00140 };
00141
00143 struct Error
00144 {
00145 SourcePos pos;
00146 std::string message;
00147
00148 Error(const SourcePos& pos, const std::string& message) : pos(pos), message(message) { }
00150 Error() { message = "not defined"; }
00152 std::string toString() const;
00153 };
00154
00156 typedef std::vector<std::string> VariablesNamesVector;
00157
00159 struct NamedValue
00160 {
00162 NamedValue(const std::string& name, int value) : name(name), value(value) {}
00163
00164 std::string name;
00165 int value;
00166 };
00167
00169 typedef NamedValue EventDescription;
00170
00172 typedef NamedValue ConstantDefinition;
00173
00175 struct NamedValuesVector: public std::vector<NamedValue>
00176 {
00177 bool contains(const std::string& s, size_t* position = 0) const;
00178 };
00179
00181 typedef NamedValuesVector EventsDescriptionsVector;
00182
00184 typedef NamedValuesVector ConstantsDefinitions;
00185
00187 struct CommonDefinitions
00188 {
00190 EventsDescriptionsVector events;
00192 ConstantsDefinitions constants;
00193
00195 void clear() { events.clear(); constants.clear(); }
00196 };
00197
00199 typedef std::vector<short int> VariablesDataVector;
00200
00202 class Compiler
00203 {
00204 public:
00206 struct Token
00207 {
00208 enum Type
00209 {
00210 TOKEN_END_OF_STREAM = 0,
00211 TOKEN_STR_when,
00212 TOKEN_STR_emit,
00213 TOKEN_STR_for,
00214 TOKEN_STR_in,
00215 TOKEN_STR_step,
00216 TOKEN_STR_while,
00217 TOKEN_STR_do,
00218 TOKEN_STR_if,
00219 TOKEN_STR_then,
00220 TOKEN_STR_else,
00221 TOKEN_STR_elseif,
00222 TOKEN_STR_end,
00223 TOKEN_STR_var,
00224 TOKEN_STR_call,
00225 TOKEN_STR_sub,
00226 TOKEN_STR_callsub,
00227 TOKEN_STR_onevent,
00228 TOKEN_STR_abs,
00229 TOKEN_STRING_LITERAL,
00230 TOKEN_INT_LITERAL,
00231 TOKEN_PAR_OPEN,
00232 TOKEN_PAR_CLOSE,
00233 TOKEN_BRACKET_OPEN,
00234 TOKEN_BRACKET_CLOSE,
00235 TOKEN_COLON,
00236 TOKEN_COMMA,
00237 TOKEN_ASSIGN,
00238 TOKEN_OP_OR,
00239 TOKEN_OP_AND,
00240 TOKEN_OP_NOT,
00241 TOKEN_OP_BIT_OR,
00242 TOKEN_OP_BIT_XOR,
00243 TOKEN_OP_BIT_AND,
00244 TOKEN_OP_BIT_NOT,
00245 TOKEN_OP_EQUAL,
00246 TOKEN_OP_NOT_EQUAL,
00247 TOKEN_OP_BIGGER,
00248 TOKEN_OP_BIGGER_EQUAL,
00249 TOKEN_OP_SMALLER,
00250 TOKEN_OP_SMALLER_EQUAL,
00251 TOKEN_OP_SHIFT_LEFT,
00252 TOKEN_OP_SHIFT_RIGHT,
00253 TOKEN_OP_ADD,
00254 TOKEN_OP_NEG,
00255 TOKEN_OP_MULT,
00256 TOKEN_OP_DIV,
00257 TOKEN_OP_MOD
00258
00259 } type;
00260 std::string sValue;
00261 int iValue;
00262 SourcePos pos;
00263
00264 Token() : type(TOKEN_END_OF_STREAM), iValue(0) {}
00265 Token(Type type, SourcePos pos = SourcePos(), const std::string& value = "");
00266 const char* typeName() const;
00267 std::string toString() const;
00268 operator Type () const { return type; }
00269 };
00270
00272 typedef std::map<std::string, std::pair<unsigned, unsigned> > VariablesMap;
00274 typedef std::map<std::string, unsigned> FunctionsMap;
00276 struct SubroutineDescriptor
00277 {
00278 std::string name;
00279 unsigned address;
00280 unsigned line;
00281
00282 SubroutineDescriptor(const std::string& name, unsigned address, unsigned line) : name(name), address(address), line(line) {}
00283 };
00285 typedef std::vector<SubroutineDescriptor> SubroutineTable;
00287 typedef std::map<std::string, unsigned> SubroutineReverseTable;
00289 typedef std::set<unsigned> ImplementedEvents;
00290
00291 public:
00292 Compiler();
00293 void setTargetDescription(const TargetDescription *description);
00294 const TargetDescription *getTargetDescription() const { return targetDescription;}
00295 const VariablesMap *getVariablesMap() const { return &variablesMap; }
00296 void setCommonDefinitions(const CommonDefinitions *definitions);
00297 bool compile(std::istream& source, BytecodeVector& bytecode, unsigned& allocatedVariablesCount, Error &errorDescription, std::ostream* dump = 0);
00298
00299 protected:
00300 void internalCompilerError() const;
00301 void expect(const Token::Type& type) const;
00302 unsigned expectUInt12Literal() const;
00303 unsigned expectUInt16Literal() const;
00304 unsigned expectPositiveInt16Literal() const;
00305 int expectAbsoluteInt16Literal(bool negative) const;
00306 unsigned expectPositiveConstant() const;
00307 int expectConstant() const;
00308 unsigned expectPositiveInt16LiteralOrConstant() const;
00309 int expectInt16LiteralOrConstant();
00310 unsigned expectGlobalEventId() const;
00311 unsigned expectAnyEventId() const;
00312 std::string eventName(unsigned eventId) const;
00313 template <int length>
00314 bool isOneOf(const Token::Type types[length]) const;
00315 template <int length>
00316 void expectOneOf(const Token::Type types[length]) const;
00317 void buildMaps();
00318 void tokenize(std::istream& source);
00319 void dumpTokens(std::ostream &dest) const;
00320 bool verifyStackCalls(PreLinkBytecode& preLinkBytecode);
00321 bool link(const PreLinkBytecode& preLinkBytecode, BytecodeVector& bytecode);
00322 void disassemble(BytecodeVector& bytecode, const PreLinkBytecode& preLinkBytecode, std::ostream& dump) const;
00323
00324 protected:
00325 Node* parseProgram();
00326
00327 Node* parseStatement();
00328
00329 Node* parseBlockStatement();
00330
00331 Node* parseVarDef();
00332 Node* parseAssignment();
00333 Node* parseIfWhen(bool edgeSensitive);
00334 Node* parseFor();
00335 Node* parseWhile();
00336 Node* parseOnEvent();
00337 Node* parseEmit();
00338 Node* parseSubDecl();
00339 Node* parseCallSub();
00340
00341 Node* parseOr();
00342 Node* parseAnd();
00343 Node* parseNot();
00344
00345 Node* parseCondition();
00346
00347 Node *parseBinaryOrExpression();
00348 Node *parseBinaryXorExpression();
00349 Node *parseBinaryAndExpression();
00350
00351 Node* parseShiftExpression();
00352 Node* parseAddExpression();
00353 Node* parseMultExpression();
00354 Node* parseUnaryExpression();
00355 Node* parseFunctionCall();
00356
00357 void parseReadVarArrayAccess(unsigned* addr, unsigned* size);
00358
00359 protected:
00360 std::deque<Token> tokens;
00361 VariablesMap variablesMap;
00362 ImplementedEvents implementedEvents;
00363 FunctionsMap functionsMap;
00364 SubroutineTable subroutineTable;
00365 SubroutineReverseTable subroutineReverseTable;
00366 unsigned freeVariableIndex;
00367 const TargetDescription *targetDescription;
00368 const CommonDefinitions *commonDefinitions;
00369 };
00370
00372 struct PreLinkBytecode
00373 {
00375 typedef std::map<unsigned, BytecodeVector> EventsBytecode;
00376 EventsBytecode events;
00377
00379 typedef std::map<unsigned, BytecodeVector> SubroutinesBytecode;
00380 SubroutinesBytecode subroutines;
00381
00382 BytecodeVector *current;
00383
00384 PreLinkBytecode();
00385
00386 void fixup(const Compiler::SubroutineTable &subroutineTable);
00387 };
00388
00391 };
00392
00393 #endif
00394