The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2298 lines
87KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #if JUCE_MSVC
  18. #pragma warning (push)
  19. #pragma warning (disable: 4702)
  20. #endif
  21. namespace littlefoot
  22. {
  23. /**
  24. This class compiles littlefoot source code into a littlefoot::Program object
  25. which can be executed by a littlefoot::Runner.
  26. @tags{Blocks}
  27. */
  28. struct Compiler
  29. {
  30. Compiler() = default;
  31. /** Gives the compiler a zero-terminated list of native function prototypes to
  32. use when parsing function calls.
  33. */
  34. void addNativeFunctions (const char* const* functionPrototypes)
  35. {
  36. for (; *functionPrototypes != nullptr; ++functionPrototypes)
  37. nativeFunctions.add (NativeFunction (*functionPrototypes, nullptr));
  38. }
  39. /** Tells the compiler to use the list of native function prototypes from
  40. this littlefoot::Runner object.
  41. */
  42. template <typename RunnerType>
  43. void addNativeFunctions (const RunnerType& runner)
  44. {
  45. for (int i = 0; i < runner.getNumNativeFunctions(); ++i)
  46. nativeFunctions.add (runner.getNativeFunction (i));
  47. }
  48. /** Compiles a littlefoot program.
  49. If there's an error, this returns it, otherwise the compiled bytecode is
  50. placed in the compiledObjectCode member.
  51. */
  52. Result compile (const String& sourceCode, uint32 defaultHeapSize, const Array<File>& searchPaths = {})
  53. {
  54. try
  55. {
  56. SyntaxTreeBuilder stb (sourceCode, nativeFunctions, defaultHeapSize, searchPaths);
  57. stb.compile();
  58. stb.simplify();
  59. compiledObjectCode.clear();
  60. CodeGenerator codeGen (compiledObjectCode, stb);
  61. codeGen.generateCode (stb.blockBeingParsed, stb.heapSizeRequired);
  62. return Result::ok();
  63. }
  64. catch (String error)
  65. {
  66. return Result::fail (error);
  67. }
  68. }
  69. /** After a successful compilation, this returns the finished Program. */
  70. Program getCompiledProgram() const noexcept
  71. {
  72. return Program (compiledObjectCode.begin(), (uint32) compiledObjectCode.size());
  73. }
  74. /** After a successful call to compile(), this contains the bytecode generated.
  75. A littlefoot::Program object can be created directly from this array.
  76. */
  77. Array<uint8> compiledObjectCode;
  78. private:
  79. #ifndef DOXYGEN
  80. struct Statement;
  81. struct Expression;
  82. struct BlockStatement;
  83. struct Function;
  84. struct AllocatedObject { virtual ~AllocatedObject() noexcept {} };
  85. using StatementPtr = Statement*;
  86. using ExpPtr = Expression*;
  87. using BlockPtr = BlockStatement*;
  88. using TokenType = const char*;
  89. #define LITTLEFOOT_KEYWORDS(X) \
  90. X(if_, "if") X(else_, "else") X(do_, "do") \
  91. X(while_, "while") X(for_, "for") X(break_, "break") X(continue_, "continue") \
  92. X(void_, "void") X(int_, "int") X(float_, "float") X(bool_, "bool") \
  93. X(return_, "return") X(true_, "true") X(false_, "false") X(const_, "const")
  94. #define LITTLEFOOT_OPERATORS(X) \
  95. X(semicolon, ";") X(dot, ".") X(comma, ",") X(hash, "#") \
  96. X(openParen, "(") X(closeParen, ")") X(openBrace, "{") X(closeBrace, "}") \
  97. X(openBracket, "[") X(closeBracket, "]") X(colon, ":") X(question, "?") \
  98. X(equals, "==") X(assign, "=") X(notEquals, "!=") X(logicalNot, "!") \
  99. X(plusEquals, "+=") X(plusplus, "++") X(plus, "+") \
  100. X(minusEquals, "-=") X(minusminus, "--") X(minus, "-") \
  101. X(timesEquals, "*=") X(times, "*") X(divideEquals, "/=") X(divide, "/") \
  102. X(moduloEquals, "%=") X(modulo, "%") X(xorEquals, "^=") X(bitwiseXor, "^") X(bitwiseNot, "~") \
  103. X(andEquals, "&=") X(logicalAnd, "&&") X(bitwiseAnd, "&") \
  104. X(orEquals, "|=") X(logicalOr, "||") X(bitwiseOr, "|") \
  105. X(leftShiftEquals, "<<=") X(lessThanOrEqual, "<=") X(leftShift, "<<") X(lessThan, "<") \
  106. X(rightShiftUnsigned, ">>>") X(rightShiftEquals, ">>=") X(rightShift, ">>") X(greaterThanOrEqual, ">=") X(greaterThan, ">")
  107. struct Token
  108. {
  109. #define DECLARE_LITTLEFOOT_TOKEN(name, str) static constexpr const char* name = str;
  110. LITTLEFOOT_KEYWORDS (DECLARE_LITTLEFOOT_TOKEN)
  111. LITTLEFOOT_OPERATORS (DECLARE_LITTLEFOOT_TOKEN)
  112. DECLARE_LITTLEFOOT_TOKEN (eof, "$eof")
  113. DECLARE_LITTLEFOOT_TOKEN (literal, "$literal")
  114. DECLARE_LITTLEFOOT_TOKEN (identifier, "$identifier")
  115. };
  116. Array<NativeFunction> nativeFunctions;
  117. //==============================================================================
  118. struct CodeLocation
  119. {
  120. CodeLocation (const String& code, const File& srcFile) noexcept : program (code), location (program.getCharPointer()), sourceFile (srcFile) {}
  121. CodeLocation (const CodeLocation& other) noexcept : program (other.program), location (other.location), sourceFile (other.sourceFile) {}
  122. [[noreturn]] void throwError (const String& message) const
  123. {
  124. int col = 1, line = 1;
  125. for (auto i = program.getCharPointer(); i < location && ! i.isEmpty(); ++i)
  126. {
  127. ++col;
  128. if (*i == '\n') { col = 1; ++line; }
  129. }
  130. auto filePath = sourceFile == File() ? String() : (sourceFile.getFullPathName() + ": ");
  131. throw filePath + "Line " + String (line) + ", column " + String (col) + " : " + message;
  132. }
  133. String program;
  134. String::CharPointerType location;
  135. File sourceFile;
  136. };
  137. //==============================================================================
  138. struct TokenIterator
  139. {
  140. TokenIterator (const String& code) : location (code, {}), p (code.getCharPointer()) { skip(); }
  141. TokenType skip()
  142. {
  143. skipWhitespaceAndComments();
  144. location.location = p;
  145. auto last = currentType;
  146. currentType = matchNextToken();
  147. return last;
  148. }
  149. void match (TokenType expected)
  150. {
  151. if (currentType != expected)
  152. throwErrorExpecting (getTokenDescription (expected));
  153. skip();
  154. }
  155. bool matchIf (TokenType expected) { if (currentType == expected) { skip(); return true; } return false; }
  156. template <typename... Args>
  157. bool matchesAny (TokenType t1, Args... others) const noexcept { return currentType == t1 || matchesAny (others...); }
  158. bool matchesAny (TokenType t1) const noexcept { return currentType == t1; }
  159. void throwErrorExpecting (const String& expected) { location.throwError ("Found " + getTokenDescription (currentType) + " when expecting " + expected); }
  160. CodeLocation location;
  161. TokenType currentType;
  162. var currentValue;
  163. protected:
  164. String::CharPointerType p;
  165. private:
  166. static bool isIdentifierStart (juce_wchar c) noexcept { return CharacterFunctions::isLetter (c) || c == '_'; }
  167. static bool isIdentifierBody (juce_wchar c) noexcept { return CharacterFunctions::isLetterOrDigit (c) || c == '_'; }
  168. TokenType matchNextToken()
  169. {
  170. if (isIdentifierStart (*p))
  171. {
  172. auto end = p;
  173. while (isIdentifierBody (*++end)) {}
  174. const size_t len = (size_t) (end - p);
  175. #define LITTLEFOOT_COMPARE_KEYWORD(name, str) if (len == sizeof (str) - 1 && matchToken (Token::name, len)) return Token::name;
  176. LITTLEFOOT_KEYWORDS (LITTLEFOOT_COMPARE_KEYWORD)
  177. currentValue = String (p, end); p = end;
  178. return Token::identifier;
  179. }
  180. if (p.isDigit())
  181. {
  182. if (parseHexLiteral() || parseFloatLiteral() || parseOctalLiteral() || parseDecimalLiteral())
  183. return Token::literal;
  184. location.throwError ("Syntax error in numeric constant");
  185. }
  186. if (parseStringLiteral (*p) || (*p == '.' && parseFloatLiteral()))
  187. return Token::literal;
  188. #define LITTLEFOOT_COMPARE_OPERATOR(name, str) if (matchToken (Token::name, sizeof (str) - 1)) return Token::name;
  189. LITTLEFOOT_OPERATORS (LITTLEFOOT_COMPARE_OPERATOR)
  190. if (! p.isEmpty())
  191. location.throwError ("Unexpected character '" + String::charToString (*p) + "' in source");
  192. return Token::eof;
  193. }
  194. bool matchToken (TokenType name, const size_t len) noexcept
  195. {
  196. if (p.compareUpTo (CharPointer_ASCII (name), (int) len) != 0) return false;
  197. p += (int) len; return true;
  198. }
  199. void skipWhitespaceAndComments()
  200. {
  201. for (;;)
  202. {
  203. p = p.findEndOfWhitespace();
  204. if (*p == '/')
  205. {
  206. auto c2 = p[1];
  207. if (c2 == '/') { p = CharacterFunctions::find (p, (juce_wchar) '\n'); continue; }
  208. if (c2 == '*')
  209. {
  210. location.location = p;
  211. p = CharacterFunctions::find (p + 2, CharPointer_ASCII ("*/"));
  212. if (p.isEmpty()) location.throwError ("Unterminated '/*' comment");
  213. p += 2; continue;
  214. }
  215. }
  216. break;
  217. }
  218. }
  219. bool parseStringLiteral (juce_wchar quoteType)
  220. {
  221. if (quoteType != '"' && quoteType != '\'')
  222. return false;
  223. auto r = JSON::parseQuotedString (p, currentValue);
  224. if (r.failed()) location.throwError (r.getErrorMessage());
  225. return true;
  226. }
  227. bool parseHexLiteral()
  228. {
  229. if (*p != '0' || (p[1] != 'x' && p[1] != 'X')) return false;
  230. auto t = ++p;
  231. auto v = CharacterFunctions::getHexDigitValue (*++t);
  232. if (v < 0) return false;
  233. for (;;)
  234. {
  235. auto digit = CharacterFunctions::getHexDigitValue (*++t);
  236. if (digit < 0) break;
  237. v = v * 16 + digit;
  238. }
  239. currentValue = v; p = t;
  240. return true;
  241. }
  242. bool parseFloatLiteral()
  243. {
  244. int numDigits = 0;
  245. auto t = p;
  246. while (t.isDigit()) { ++t; ++numDigits; }
  247. const bool hasPoint = (*t == '.');
  248. if (hasPoint)
  249. while ((++t).isDigit()) ++numDigits;
  250. if (numDigits == 0)
  251. return false;
  252. auto c = *t;
  253. const bool hasExponent = (c == 'e' || c == 'E');
  254. if (hasExponent)
  255. {
  256. c = *++t;
  257. if (c == '+' || c == '-') ++t;
  258. if (! t.isDigit()) return false;
  259. while ((++t).isDigit()) {}
  260. }
  261. if (! (hasExponent || hasPoint)) return false;
  262. currentValue = CharacterFunctions::getDoubleValue (p); p = t;
  263. return true;
  264. }
  265. bool parseOctalLiteral()
  266. {
  267. auto t = p;
  268. int64 v = *t - '0';
  269. if (v != 0) return false; // first digit of octal must be 0
  270. for (;;)
  271. {
  272. auto digit = (int) (*++t - '0');
  273. if (isPositiveAndBelow (digit, 8)) v = v * 8 + digit;
  274. else if (isPositiveAndBelow (digit, 10)) location.throwError ("Decimal digit in octal constant");
  275. else break;
  276. }
  277. currentValue = v; p = t;
  278. return true;
  279. }
  280. bool parseDecimalLiteral()
  281. {
  282. int64 v = 0;
  283. for (;; ++p)
  284. {
  285. auto digit = (int) (*p - '0');
  286. if (isPositiveAndBelow (digit, 10)) v = v * 10 + digit;
  287. else break;
  288. }
  289. currentValue = v;
  290. return true;
  291. }
  292. };
  293. //==============================================================================
  294. //==============================================================================
  295. struct SyntaxTreeBuilder : private TokenIterator
  296. {
  297. SyntaxTreeBuilder (const String& code, const Array<NativeFunction>& nativeFns, uint32 defaultHeapSize, const Array<File>& searchPathsToUse)
  298. : TokenIterator (code), searchPaths (searchPathsToUse), nativeFunctions (nativeFns), heapSizeRequired (defaultHeapSize) {}
  299. void compile()
  300. {
  301. blockBeingParsed = allocate<BlockStatement> (location, nullptr, nullptr, false);
  302. parseCode();
  303. }
  304. void parseCode()
  305. {
  306. const auto programHash = location.program.hashCode64();
  307. if (includedSourceCode.contains (programHash))
  308. return;
  309. includedSourceCode.add (programHash);
  310. while (currentType != Token::eof)
  311. {
  312. if (matchIf (Token::hash))
  313. {
  314. parseCompilerDirective();
  315. continue;
  316. }
  317. const bool isConstVariable = matchIf (Token::const_);
  318. if (! matchesAnyTypeOrVoid())
  319. throwErrorExpecting ("a global variable or function");
  320. auto type = tokenToType (skip());
  321. auto name = parseIdentifier();
  322. if (matchIf (Token::openParen))
  323. {
  324. if (isConstVariable)
  325. location.throwError ("Return type of a function cannot be const");
  326. parseFunctionDeclaration (type, name);
  327. continue;
  328. }
  329. if (type == Type::void_)
  330. location.throwError ("A variable type cannot be 'void'");
  331. parseGlobalVariableDeclaraion (isConstVariable, type, name);
  332. }
  333. }
  334. void simplify()
  335. {
  336. for (auto f : functions)
  337. f->block->simplify (*this);
  338. }
  339. const Function* findFunction (FunctionID functionID) const noexcept
  340. {
  341. for (auto f : functions)
  342. if (f->functionID == functionID)
  343. return f;
  344. return nullptr;
  345. }
  346. const NativeFunction* findNativeFunction (FunctionID functionID) const noexcept
  347. {
  348. for (auto& f : nativeFunctions)
  349. if (f.functionID == functionID)
  350. return &f;
  351. return nullptr;
  352. }
  353. //==============================================================================
  354. BlockPtr blockBeingParsed = nullptr;
  355. Array<Function*> functions;
  356. Array<File> searchPaths;
  357. Array<int64> includedSourceCode;
  358. const Array<NativeFunction>& nativeFunctions;
  359. uint32 heapSizeRequired;
  360. template <typename Type, typename... Args>
  361. Type* allocate (Args... args) { auto o = new Type (args...); allAllocatedObjects.add (o); return o; }
  362. private:
  363. OwnedArray<AllocatedObject> allAllocatedObjects;
  364. //==============================================================================
  365. void parseCompilerDirective()
  366. {
  367. auto name = parseIdentifier();
  368. if (name == "heapsize")
  369. {
  370. match (Token::colon);
  371. heapSizeRequired = (((uint32) parseIntegerLiteral()) + 3) & ~3u;
  372. }
  373. else if (name == "include")
  374. {
  375. parseIncludeDirective();
  376. }
  377. else
  378. {
  379. location.throwError ("Unknown compiler directive");
  380. }
  381. }
  382. void parseIncludeDirective()
  383. {
  384. match (Token::literal);
  385. if (! currentValue.isString())
  386. {
  387. location.throwError ("Expected file path");
  388. return;
  389. }
  390. juce::File fileToInclude = resolveIncludePath (currentValue.toString());
  391. if (fileToInclude == File())
  392. return;
  393. searchPaths.add (fileToInclude);
  394. auto codeToInclude = fileToInclude.loadFileAsString();
  395. auto locationToRestore = location;
  396. auto currentTypeToRestore = currentType;
  397. auto currentValueToRestore = currentValue;
  398. auto pToRestore = p;
  399. location = CodeLocation (codeToInclude, fileToInclude);
  400. p = codeToInclude.getCharPointer();
  401. skip();
  402. parseCode();
  403. location = locationToRestore;
  404. currentType = currentTypeToRestore;
  405. currentValue = currentValueToRestore;
  406. p = pToRestore;
  407. }
  408. File resolveIncludePath (String include)
  409. {
  410. if (include.substring (include.length() - 11) != ".littlefoot")
  411. {
  412. location.throwError ("File extension must be .littlefoot");
  413. return {};
  414. }
  415. if (File::isAbsolutePath (include) && File (include).existsAsFile())
  416. return { include };
  417. auto fileName = include.fromLastOccurrenceOf ("/", false, false);
  418. for (auto path : searchPaths)
  419. {
  420. if (path == File())
  421. continue;
  422. if (! path.isDirectory())
  423. path = path.getParentDirectory();
  424. if (path.getChildFile (include).existsAsFile())
  425. return path.getChildFile (include);
  426. if (path.getChildFile (fileName).existsAsFile())
  427. return path.getChildFile (fileName);
  428. }
  429. location.throwError ("File not found: " + include);
  430. }
  431. void parseGlobalVariableDeclaraion (bool isConst, Type type, String name)
  432. {
  433. for (;;)
  434. {
  435. int arraySize = matchIf (Token::openBracket) ? parseIntegerLiteral() : 0;
  436. if (arraySize > 0)
  437. location.throwError ("Arrays not yet implemented!");
  438. var constantInitialiser;
  439. if (isConst)
  440. constantInitialiser = parseConstantExpressionInitialiser (type);
  441. blockBeingParsed->addVariable ({ name, type, true, isConst, constantInitialiser }, location);
  442. if (matchIf (Token::comma))
  443. {
  444. name = parseIdentifier();
  445. continue;
  446. }
  447. match (Token::semicolon);
  448. break;
  449. }
  450. }
  451. var parseConstantExpressionInitialiser (Type expectedType)
  452. {
  453. var result;
  454. match (Token::assign);
  455. auto e = parseExpression();
  456. if (auto literal = dynamic_cast<LiteralValue*> (e->simplify (*this)))
  457. {
  458. result = literal->value;
  459. if (getTypeOfVar (result) != expectedType)
  460. location.throwError ("Expected a constant expression of type " + getTypeName (expectedType));
  461. }
  462. else
  463. {
  464. location.throwError ("Expected a constant expression");
  465. }
  466. return result;
  467. }
  468. void parseFunctionDeclaration (Type returnType, const String& name)
  469. {
  470. auto f = allocate<Function>();
  471. while (matchesAnyType())
  472. {
  473. auto type = tokenToType (skip());
  474. f->arguments.add ({ parseIdentifier(), type, false, false, 0 });
  475. if (f->arguments.size() > 127)
  476. location.throwError ("Too many function arguments");
  477. if (currentType == Token::closeParen)
  478. break;
  479. match (Token::comma);
  480. }
  481. match (Token::closeParen);
  482. f->functionID = createFunctionID (name, returnType, f->getArgumentTypes());
  483. if (findFunction (f->functionID) != nullptr || findNativeFunction (f->functionID) != nullptr)
  484. location.throwError ("Duplicate function declaration");
  485. functions.add (f);
  486. f->block = parseBlock (true);
  487. f->returnType = returnType;
  488. if (! f->block->alwaysReturns())
  489. {
  490. if (returnType != Type::void_)
  491. location.throwError ("This function must return a value");
  492. f->block->statements.add (allocate<ReturnStatement> (location, f->block, nullptr));
  493. }
  494. }
  495. int parseIntegerLiteral()
  496. {
  497. auto e = parseExpression();
  498. if (auto literal = dynamic_cast<LiteralValue*> (e->simplify (*this)))
  499. {
  500. if (literal->value.isInt() || literal->value.isInt64())
  501. {
  502. auto value = static_cast<int> (literal->value);
  503. if (value > 0)
  504. return value;
  505. }
  506. }
  507. location.throwError ("Expected an integer constant");
  508. return 0;
  509. }
  510. BlockPtr parseBlock (bool isMainBlockOfFunction)
  511. {
  512. match (Token::openBrace);
  513. auto b = allocate<BlockStatement> (location, blockBeingParsed, functions.getLast(), isMainBlockOfFunction);
  514. auto lastBlock = blockBeingParsed;
  515. blockBeingParsed = b;
  516. while (! matchIf (Token::closeBrace))
  517. b->statements.add (parseStatement());
  518. blockBeingParsed = lastBlock;
  519. return b;
  520. }
  521. StatementPtr parseStatement()
  522. {
  523. if (currentType == Token::openBrace) return parseBlock (false);
  524. if (matchIf (Token::if_)) return parseIf();
  525. if (matchIf (Token::while_)) return parseDoOrWhileLoop (false);
  526. if (matchIf (Token::do_)) return parseDoOrWhileLoop (true);
  527. if (matchIf (Token::for_)) return parseForLoop();
  528. if (matchIf (Token::return_)) return parseReturn();
  529. if (matchIf (Token::break_)) return matchEndOfStatement (allocate<BreakStatement> (location, blockBeingParsed));
  530. if (matchIf (Token::continue_)) return matchEndOfStatement (allocate<ContinueStatement> (location, blockBeingParsed));
  531. if (matchIf (Token::semicolon)) return matchEndOfStatement (allocate<Statement> (location, blockBeingParsed));
  532. if (matchIf (Token::plusplus)) return matchEndOfStatement (parsePreIncDec (Token::plus));
  533. if (matchIf (Token::minusminus)) return matchEndOfStatement (parsePreIncDec (Token::minus));
  534. if (matchesAny (Token::openParen)) return matchEndOfStatement (parseFactor());
  535. if (matchIf (Token::const_)) return parseVariableDeclaration (true);
  536. if (matchesAnyType()) return parseVariableDeclaration (false);
  537. if (matchesAny (Token::identifier, Token::literal, Token::minus))
  538. return matchEndOfStatement (parseExpression());
  539. throwErrorExpecting ("a statement");
  540. return nullptr;
  541. }
  542. ExpPtr parseExpression()
  543. {
  544. auto lhs = parseLogicOperator();
  545. if (matchIf (Token::question)) return parseTernaryOperator (lhs);
  546. if (matchIf (Token::plusEquals)) return parseInPlaceOpExpression (lhs, Token::plus);
  547. if (matchIf (Token::minusEquals)) return parseInPlaceOpExpression (lhs, Token::minus);
  548. if (matchIf (Token::timesEquals)) return parseInPlaceOpExpression (lhs, Token::times);
  549. if (matchIf (Token::divideEquals)) return parseInPlaceOpExpression (lhs, Token::divide);
  550. if (matchIf (Token::moduloEquals)) return parseInPlaceOpExpression (lhs, Token::modulo);
  551. if (matchIf (Token::leftShiftEquals)) return parseInPlaceOpExpression (lhs, Token::leftShift);
  552. if (matchIf (Token::rightShiftEquals)) return parseInPlaceOpExpression (lhs, Token::rightShift);
  553. if (matchIf (Token::assign))
  554. {
  555. auto loc = location;
  556. return allocate<Assignment> (loc, blockBeingParsed, getIdentifierFromExpression (lhs), parseExpression(), false);
  557. }
  558. return lhs;
  559. }
  560. ExpPtr parseTernaryOperator (ExpPtr condition)
  561. {
  562. auto e = allocate<TernaryOp> (location, blockBeingParsed);
  563. e->condition = condition;
  564. e->trueBranch = parseExpression();
  565. match (Token::colon);
  566. e->falseBranch = parseExpression();
  567. return e;
  568. }
  569. ExpPtr parseLogicOperator()
  570. {
  571. for (auto a = parseComparator();;)
  572. {
  573. if (! matchesAny (Token::logicalAnd, Token::logicalOr, Token::bitwiseOr,
  574. Token::bitwiseAnd, Token::bitwiseXor))
  575. return a;
  576. auto loc = location;
  577. auto type = skip();
  578. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseComparator(), type);
  579. }
  580. }
  581. ExpPtr parseComparator()
  582. {
  583. for (auto a = parseShiftOperator();;)
  584. {
  585. if (! matchesAny (Token::equals, Token::notEquals, Token::lessThan,
  586. Token::lessThanOrEqual, Token::greaterThan, Token::greaterThanOrEqual))
  587. return a;
  588. auto loc = location;
  589. auto type = skip();
  590. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseShiftOperator(), type);
  591. }
  592. }
  593. ExpPtr parseShiftOperator()
  594. {
  595. for (auto a = parseAdditionSubtraction();;)
  596. {
  597. if (! matchesAny (Token::leftShift, Token::rightShift, Token::rightShiftUnsigned))
  598. return a;
  599. auto loc = location;
  600. auto type = skip();
  601. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseExpression(), type);
  602. }
  603. }
  604. ExpPtr parseAdditionSubtraction()
  605. {
  606. for (auto a = parseMultiplyDivide();;)
  607. {
  608. if (! matchesAny (Token::plus, Token::minus))
  609. return a;
  610. auto loc = location;
  611. auto type = skip();
  612. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseMultiplyDivide(), type);
  613. }
  614. }
  615. ExpPtr parseMultiplyDivide()
  616. {
  617. for (auto a = parseUnary();;)
  618. {
  619. if (! matchesAny (Token::times, Token::divide, Token::modulo))
  620. return a;
  621. auto loc = location;
  622. auto type = skip();
  623. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseUnary(), type);
  624. }
  625. }
  626. ExpPtr parseUnary()
  627. {
  628. if (matchIf (Token::plusplus)) return parsePreIncDec (Token::plus);
  629. if (matchIf (Token::minusminus)) return parsePreIncDec (Token::minus);
  630. if (matchesAny (Token::minus, Token::logicalNot, Token::bitwiseNot))
  631. {
  632. auto loc = location;
  633. auto type = skip();
  634. return allocate<UnaryOp> (loc, blockBeingParsed, parseUnary(), type);
  635. }
  636. return parseFactor();
  637. }
  638. ExpPtr parseFactor()
  639. {
  640. if (currentType == Token::identifier) return parseSuffixes (allocate<Identifier> (location, blockBeingParsed, parseIdentifier()));
  641. if (matchIf (Token::openParen)) return parseSuffixes (matchCloseParen (parseExpression()));
  642. if (matchIf (Token::true_)) return parseSuffixes (allocate<LiteralValue> (location, blockBeingParsed, true));
  643. if (matchIf (Token::false_)) return parseSuffixes (allocate<LiteralValue> (location, blockBeingParsed, false));
  644. if (currentType == Token::literal)
  645. {
  646. auto lit = allocate<LiteralValue> (location, blockBeingParsed, currentValue);
  647. skip();
  648. return parseSuffixes (lit);
  649. }
  650. if (matchesAny (Token::int_, Token::float_, Token::bool_))
  651. return parseSuffixes (parseFunctionCall (skip()));
  652. throwErrorExpecting ("an expression");
  653. return nullptr;
  654. }
  655. ExpPtr parseSuffixes (ExpPtr input)
  656. {
  657. if (currentType == Token::openParen)
  658. {
  659. if (auto functionName = dynamic_cast<Identifier*> (input))
  660. return parseSuffixes (parseFunctionCall (functionName->name));
  661. location.throwError ("Malformed function call");
  662. return {};
  663. }
  664. if (matchIf (Token::openBracket))
  665. {
  666. auto s = allocate<ArraySubscript> (location, blockBeingParsed);
  667. s->object = input;
  668. s->index = parseExpression();
  669. match (Token::closeBracket);
  670. return parseSuffixes (s);
  671. }
  672. if (matchIf (Token::plusplus)) return parsePostIncDec (input, Token::plus);
  673. if (matchIf (Token::minusminus)) return parsePostIncDec (input, Token::minus);
  674. return input;
  675. }
  676. ExpPtr parseInPlaceOpExpression (ExpPtr lhs, TokenType opType)
  677. {
  678. auto loc = location;
  679. auto rhs = parseExpression();
  680. return allocate<Assignment> (loc, blockBeingParsed, getIdentifierFromExpression (lhs),
  681. allocate<BinaryOperator> (location, blockBeingParsed, lhs, rhs, opType), false);
  682. }
  683. ExpPtr parsePreIncDec (TokenType opType)
  684. {
  685. auto lhs = parseFactor();
  686. auto one = allocate<LiteralValue> (location, blockBeingParsed, (int) 1);
  687. return allocate<Assignment> (location, blockBeingParsed, getIdentifierFromExpression (lhs),
  688. allocate<BinaryOperator> (location, blockBeingParsed, lhs, one, opType), false);
  689. }
  690. ExpPtr parsePostIncDec (ExpPtr lhs, TokenType opType)
  691. {
  692. auto one = allocate<LiteralValue> (location, blockBeingParsed, (int) 1);
  693. return allocate<Assignment> (location, blockBeingParsed, getIdentifierFromExpression (lhs),
  694. allocate<BinaryOperator> (location, blockBeingParsed, lhs, one, opType), true);
  695. }
  696. StatementPtr parseIf()
  697. {
  698. auto s = allocate<IfStatement> (location, blockBeingParsed);
  699. match (Token::openParen);
  700. s->condition = matchCloseParen (parseExpression());
  701. s->trueBranch = parseStatement();
  702. s->falseBranch = matchIf (Token::else_) ? parseStatement() : nullptr;
  703. return s;
  704. }
  705. StatementPtr parseReturn()
  706. {
  707. auto value = matchIf (Token::semicolon) ? nullptr : parseExpression();
  708. auto returnStatement = allocate<ReturnStatement> (location, blockBeingParsed, value);
  709. matchIf (Token::semicolon);
  710. return returnStatement;
  711. }
  712. StatementPtr parseVariableDeclaration (bool isConst)
  713. {
  714. if (isConst && ! matchesAnyType())
  715. throwErrorExpecting ("a type");
  716. auto type = tokenToType (skip());
  717. for (StatementPtr result = nullptr;;)
  718. {
  719. auto name = parseIdentifier();
  720. auto loc = location;
  721. if (isConst)
  722. {
  723. auto constantValue = parseConstantExpressionInitialiser (type);
  724. blockBeingParsed->addVariable ({ name, type, false, true, constantValue }, loc);
  725. }
  726. else
  727. {
  728. blockBeingParsed->addVariable ({ name, type, false, false, {} }, loc);
  729. auto assignedValue = matchIf (Token::assign) ? parseExpression() : nullptr;
  730. if (auto literal = dynamic_cast<LiteralValue*> (assignedValue))
  731. if (static_cast<double> (literal->value) == 0)
  732. assignedValue = nullptr;
  733. if (assignedValue != nullptr || ! blockBeingParsed->isMainBlockOfFunction) // no need to assign 0 for variables in the outer scope
  734. {
  735. if (assignedValue == nullptr)
  736. assignedValue = allocate<LiteralValue> (loc, blockBeingParsed, (int) 0);
  737. auto assignment = allocate<Assignment> (loc, blockBeingParsed, name, assignedValue, false);
  738. if (result == nullptr)
  739. {
  740. result = assignment;
  741. }
  742. else
  743. {
  744. auto block = dynamic_cast<BlockPtr> (result);
  745. if (block == nullptr)
  746. {
  747. block = allocate<BlockStatement> (loc, blockBeingParsed, functions.getLast(), false);
  748. block->statements.add (result);
  749. result = block;
  750. }
  751. block->statements.add (assignment);
  752. }
  753. }
  754. }
  755. if (matchIf (Token::semicolon))
  756. return result != nullptr ? result : allocate<Statement> (location, blockBeingParsed);
  757. match (Token::comma);
  758. }
  759. }
  760. StatementPtr parseForLoop()
  761. {
  762. auto oldBlock = blockBeingParsed;
  763. auto block = allocate<BlockStatement> (location, oldBlock, functions.getLast(), false);
  764. blockBeingParsed = block;
  765. auto loopStatement = allocate<LoopStatement> (location, blockBeingParsed, false);
  766. block->statements.add (loopStatement);
  767. match (Token::openParen);
  768. loopStatement->initialiser = parseStatement();
  769. loopStatement->condition = matchIf (Token::semicolon) ? allocate<LiteralValue> (location, blockBeingParsed, true)
  770. : matchEndOfStatement (parseExpression());
  771. loopStatement->iterator = matchIf (Token::closeParen) ? allocate<Statement> (location, blockBeingParsed)
  772. : matchCloseParen (parseExpression());
  773. loopStatement->body = parseStatement();
  774. blockBeingParsed = oldBlock;
  775. return block;
  776. }
  777. StatementPtr parseDoOrWhileLoop (bool isDoLoop)
  778. {
  779. auto loopStatement = allocate<LoopStatement> (location, blockBeingParsed, isDoLoop);
  780. loopStatement->initialiser = allocate<Statement> (location, blockBeingParsed);
  781. loopStatement->iterator = allocate<Statement> (location, blockBeingParsed);
  782. if (isDoLoop)
  783. {
  784. loopStatement->body = parseBlock (false);
  785. match (Token::while_);
  786. }
  787. match (Token::openParen);
  788. loopStatement->condition = matchCloseParen (parseExpression());
  789. if (! isDoLoop)
  790. loopStatement->body = parseStatement();
  791. return loopStatement;
  792. }
  793. String parseIdentifier()
  794. {
  795. auto name = currentValue.toString();
  796. match (Token::identifier);
  797. return name;
  798. }
  799. String getIdentifierFromExpression (ExpPtr e)
  800. {
  801. if (auto i = dynamic_cast<Identifier*> (e))
  802. return i->name;
  803. location.throwError ("This operator requires an assignable variable");
  804. return {};
  805. }
  806. ExpPtr parseFunctionCall (const String& name)
  807. {
  808. auto call = allocate<FunctionCall> (location, blockBeingParsed);
  809. call->functionName = name;
  810. match (Token::openParen);
  811. while (currentType != Token::closeParen)
  812. {
  813. call->arguments.add (parseExpression());
  814. if (currentType == Token::closeParen)
  815. break;
  816. match (Token::comma);
  817. }
  818. return matchCloseParen (call);
  819. }
  820. bool matchesAnyType() const noexcept { return matchesAny (Token::int_, Token::float_, Token::bool_); }
  821. bool matchesAnyTypeOrVoid() const noexcept { return matchesAnyType() || currentType == Token::void_; }
  822. ExpPtr matchCloseParen (ExpPtr e) { match (Token::closeParen); return e; }
  823. template<typename ExpType> ExpType matchEndOfStatement (ExpType e) { match (Token::semicolon); return e; }
  824. };
  825. //==============================================================================
  826. //==============================================================================
  827. struct CodeGenerator
  828. {
  829. CodeGenerator (Array<uint8>& output, const SyntaxTreeBuilder& stb)
  830. : outputCode (output), syntaxTree (stb) {}
  831. void generateCode (BlockPtr outerBlock, uint32 heapSizeBytesRequired)
  832. {
  833. for (auto f : syntaxTree.functions)
  834. {
  835. f->address = createMarker();
  836. f->unwindAddress = createMarker();
  837. }
  838. emit ((int16) 0); // checksum
  839. emit ((int16) 0); // size
  840. emit ((int16) syntaxTree.functions.size());
  841. emit ((int16) outerBlock->variables.size());
  842. emit ((int16) heapSizeBytesRequired);
  843. for (auto f : syntaxTree.functions)
  844. emit (f->functionID, f->address);
  845. auto codeStart = outputCode.size();
  846. for (auto f : syntaxTree.functions)
  847. f->emit (*this);
  848. removeJumpsToNextInstruction (codeStart);
  849. resolveMarkers();
  850. Program::writeInt16 (outputCode.begin() + 2, (int16) outputCode.size());
  851. const Program program (outputCode.begin(), (uint32) outputCode.size());
  852. Program::writeInt16 (outputCode.begin(), (int16) program.calculateChecksum());
  853. jassert (program.checksumMatches());
  854. }
  855. //==============================================================================
  856. Array<uint8>& outputCode;
  857. const SyntaxTreeBuilder& syntaxTree;
  858. struct Marker { int index = 0; };
  859. struct MarkerAndAddress { Marker marker; int address; };
  860. int nextMarker = 0;
  861. Array<MarkerAndAddress> markersToResolve, resolvedMarkers;
  862. Marker createMarker() noexcept { Marker m; m.index = ++nextMarker; return m; }
  863. void attachMarker (Marker m) { resolvedMarkers.add ({ m, outputCode.size() }); }
  864. int getResolvedMarkerAddress (Marker marker) const
  865. {
  866. for (auto m : resolvedMarkers)
  867. if (m.marker.index == marker.index)
  868. return m.address;
  869. jassertfalse;
  870. return 0;
  871. }
  872. Marker getMarkerAtAddress (int address) const noexcept
  873. {
  874. for (auto m : markersToResolve)
  875. if (m.address == address)
  876. return m.marker;
  877. jassertfalse;
  878. return {};
  879. }
  880. void resolveMarkers()
  881. {
  882. for (auto m : markersToResolve)
  883. Program::writeInt16 (outputCode.begin() + m.address, (int16) getResolvedMarkerAddress (m.marker));
  884. }
  885. void removeCode (int address, int size)
  886. {
  887. outputCode.removeRange (address, size);
  888. for (int i = markersToResolve.size(); --i >= 0;)
  889. {
  890. auto& m = markersToResolve.getReference (i);
  891. if (m.address >= address + size)
  892. m.address -= size;
  893. else if (m.address >= address)
  894. markersToResolve.remove (i);
  895. }
  896. for (auto& m : resolvedMarkers)
  897. if (m.address >= address + size)
  898. m.address -= size;
  899. }
  900. void removeJumpsToNextInstruction (int address)
  901. {
  902. while (address < outputCode.size())
  903. {
  904. auto op = (OpCode) outputCode.getUnchecked (address);
  905. auto opSize = 1 + Program::getNumExtraBytesForOpcode (op);
  906. if (op == OpCode::jump)
  907. {
  908. auto marker = getMarkerAtAddress (address + 1);
  909. if (marker.index != 0)
  910. {
  911. if (getResolvedMarkerAddress (marker) == address + opSize)
  912. {
  913. removeCode (address, opSize);
  914. continue;
  915. }
  916. }
  917. }
  918. address += opSize;
  919. }
  920. }
  921. Marker breakTarget, continueTarget;
  922. //==============================================================================
  923. void emit (OpCode op) { emit ((int8) op); }
  924. void emit (Marker m) { markersToResolve.add ({ m, outputCode.size() }); emit ((int16) 0); }
  925. void emit (int8 value) { outputCode.add ((uint8) value); }
  926. void emit (int16 value) { uint8 d[2]; Program::writeInt16 (d, value); outputCode.insertArray (-1, d, (int) sizeof (d)); }
  927. void emit (int32 value) { uint8 d[4]; Program::writeInt32 (d, value); outputCode.insertArray (-1, d, (int) sizeof (d)); }
  928. template <typename Arg1, typename... Args>
  929. void emit (Arg1 arg1, Args... args)
  930. {
  931. emit (arg1);
  932. emit (args...);
  933. }
  934. void emitPush (const var& value)
  935. {
  936. if (value.isDouble())
  937. {
  938. const float v = value;
  939. if (v == 0) emit (OpCode::push0);
  940. else emit (OpCode::push32, Program::floatToInt (v));
  941. }
  942. else
  943. {
  944. const int v = value;
  945. if (v == 0) emit (OpCode::push0);
  946. else if (v == 1) emit (OpCode::push1);
  947. else if (v > 0 && v < 128) emit (OpCode::push8, (int8) v);
  948. else if (v > 0 && v < 32768) emit (OpCode::push16, (int16) v);
  949. else emit (OpCode::push32, (int32) v);
  950. }
  951. }
  952. void emitCast (Type source, Type dest, const CodeLocation& location)
  953. {
  954. if (dest == source) return;
  955. if (dest == Type::void_) return emit (OpCode::drop);
  956. if (source == Type::bool_ && dest == Type::int_) return;
  957. if (source == Type::int_ && dest == Type::bool_) return emit (OpCode::testNZ_int32);
  958. if ((source == Type::int_ || source == Type::bool_) && dest == Type::float_) return emit (OpCode::int32ToFloat);
  959. location.throwError ("Cannot cast from " + getTypeName (source) + " to " + getTypeName (dest));
  960. }
  961. void emitVariableRead (Type sourceType, Type requiredType, int stackDepth, int index, const CodeLocation& location)
  962. {
  963. if (index < 0)
  964. {
  965. emit (OpCode::dupFromGlobal, (int16) ((-index) - 1));
  966. }
  967. else
  968. {
  969. index += stackDepth;
  970. if (index == 0)
  971. emit (OpCode::dup);
  972. else if (index < 8)
  973. emit ((OpCode) ((int) OpCode::dupOffset_01 + index - 1));
  974. else if (index >= 128)
  975. emit (OpCode::dupOffset16, (int16) index);
  976. else
  977. emit (OpCode::dupOffset, (int8) index);
  978. }
  979. emitCast (sourceType, requiredType, location);
  980. }
  981. };
  982. //==============================================================================
  983. //==============================================================================
  984. struct Statement : public AllocatedObject
  985. {
  986. struct Visitor
  987. {
  988. virtual ~Visitor() {}
  989. virtual void operator()(StatementPtr) = 0;
  990. };
  991. Statement (const CodeLocation& l, BlockPtr parent) noexcept : location (l), parentBlock (parent) {}
  992. virtual void emit (CodeGenerator&, Type, int /*stackDepth*/) const {}
  993. virtual bool alwaysReturns() const { return false; }
  994. virtual void visitSubStatements (Visitor&) const {}
  995. virtual Statement* simplify (SyntaxTreeBuilder&) { return this; }
  996. CodeLocation location;
  997. BlockPtr parentBlock;
  998. };
  999. struct Expression : public Statement
  1000. {
  1001. Expression (const CodeLocation& l, BlockPtr parent) noexcept : Statement (l, parent) {}
  1002. virtual Type getType (CodeGenerator&) const = 0;
  1003. virtual ExpPtr simplify (SyntaxTreeBuilder&) override { return this; }
  1004. };
  1005. struct Variable
  1006. {
  1007. String name;
  1008. Type type;
  1009. bool isGlobal, isConst;
  1010. var constantValue;
  1011. };
  1012. //==============================================================================
  1013. struct Function : public AllocatedObject
  1014. {
  1015. FunctionID functionID;
  1016. Type returnType;
  1017. Array<Variable> arguments;
  1018. BlockPtr block;
  1019. CodeGenerator::Marker address, unwindAddress;
  1020. void emit (CodeGenerator& cg) const
  1021. {
  1022. cg.attachMarker (address);
  1023. const int numLocals = getNumLocals();
  1024. for (int num = numLocals; num > 0;)
  1025. {
  1026. if (num == 1)
  1027. {
  1028. cg.emit (OpCode::push0);
  1029. --num;
  1030. }
  1031. else
  1032. {
  1033. int numToDo = jmin (127, num);
  1034. cg.emit (OpCode::pushMultiple0, (int8) numToDo);
  1035. num -= numToDo;
  1036. }
  1037. }
  1038. block->emit (cg, Type::void_, 0);
  1039. cg.attachMarker (unwindAddress);
  1040. const bool keepTop = returnType != Type::void_;
  1041. for (int num = numLocals; num > 0;)
  1042. {
  1043. if (num == 1 && ! keepTop)
  1044. {
  1045. cg.emit (OpCode::drop);
  1046. --num;
  1047. }
  1048. else
  1049. {
  1050. int numToDo = jmin (127, num);
  1051. cg.emit (OpCode::dropMultiple, (int8) (keepTop ? -numToDo : numToDo));
  1052. num -= numToDo;
  1053. }
  1054. }
  1055. cg.emit (keepTop ? OpCode::retValue : OpCode::retVoid, (int8) arguments.size());
  1056. }
  1057. Array<Type> getArgumentTypes() const
  1058. {
  1059. Array<Type> argTypes;
  1060. for (auto& arg : arguments)
  1061. argTypes.add (arg.type);
  1062. return argTypes;
  1063. }
  1064. int getNumLocals() const noexcept
  1065. {
  1066. return countMaxNumLocalVariables (block);
  1067. }
  1068. static int countMaxNumLocalVariables (StatementPtr s) noexcept
  1069. {
  1070. struct Counter : Statement::Visitor
  1071. {
  1072. void operator() (StatementPtr sub) override
  1073. {
  1074. num = jmax (num, countMaxNumLocalVariables (sub));
  1075. }
  1076. int num = 0;
  1077. };
  1078. Counter counter;
  1079. if (s != nullptr)
  1080. s->visitSubStatements (counter);
  1081. if (auto block = dynamic_cast<BlockPtr> (s))
  1082. counter.num += block->variables.size();
  1083. return counter.num;
  1084. }
  1085. };
  1086. //==============================================================================
  1087. struct BlockStatement : public Statement
  1088. {
  1089. BlockStatement (const CodeLocation& l, BlockPtr parent, Function* f, bool isMainFunctionBlock)
  1090. : Statement (l, parent), function (f), isMainBlockOfFunction (isMainFunctionBlock) {}
  1091. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1092. {
  1093. jassert (requiredType == Type::void_); ignoreUnused (requiredType);
  1094. jassert (function != nullptr);
  1095. for (auto s : statements)
  1096. s->emit (cg, Type::void_, stackDepth);
  1097. }
  1098. bool alwaysReturns() const override
  1099. {
  1100. return ! statements.isEmpty() && statements.getLast()->alwaysReturns();
  1101. }
  1102. void visitSubStatements (Statement::Visitor& visit) const override
  1103. {
  1104. for (auto s : statements)
  1105. visit (s);
  1106. }
  1107. Statement* simplify (SyntaxTreeBuilder& stb) override
  1108. {
  1109. for (int i = 0; i < statements.size(); ++i)
  1110. statements.set (i, statements.getReference(i)->simplify (stb));
  1111. return this;
  1112. }
  1113. // returns -ve values for globals
  1114. int getVariableDepth (const String& name, const CodeLocation& locationForError) const
  1115. {
  1116. int index = indexOf (variables, name);
  1117. if (index >= 0)
  1118. return getNumVariablesInParentBlocks() + index;
  1119. if (! isMainBlockOfFunction)
  1120. return parentBlock->getVariableDepth (name, locationForError);
  1121. if (function != nullptr)
  1122. for (int i = function->arguments.size(); --i >= 0;)
  1123. if (function->arguments.getReference(i).name == name)
  1124. return i + 1 + function->getNumLocals();
  1125. index = indexOf (getGlobalVariables(), name);
  1126. if (index >= 0)
  1127. return -(index + 1);
  1128. locationForError.throwError ("Unknown variable '" + name + "'");
  1129. return 0;
  1130. }
  1131. int getNumVariablesInParentBlocks() const noexcept
  1132. {
  1133. return isMainBlockOfFunction ? 0 : (parentBlock->getNumVariablesInParentBlocks()
  1134. + parentBlock->variables.size());
  1135. }
  1136. const Array<Variable>& getGlobalVariables() const noexcept { return parentBlock != nullptr ? parentBlock->getGlobalVariables() : variables; }
  1137. const Array<Variable>& getGlobalConstants() const noexcept { return parentBlock != nullptr ? parentBlock->getGlobalConstants() : constants; }
  1138. const Variable& getVariable (const String& name, const CodeLocation& locationForError) const
  1139. {
  1140. for (auto& v : constants)
  1141. if (v.name == name)
  1142. return v;
  1143. for (auto& v : variables)
  1144. if (v.name == name)
  1145. return v;
  1146. if (! isMainBlockOfFunction && parentBlock != nullptr)
  1147. return parentBlock->getVariable (name, locationForError);
  1148. if (function != nullptr)
  1149. for (auto& v : function->arguments)
  1150. if (v.name == name)
  1151. return v;
  1152. for (auto& v : getGlobalConstants())
  1153. if (v.name == name)
  1154. return v;
  1155. for (auto& v : getGlobalVariables())
  1156. if (v.name == name)
  1157. return v;
  1158. locationForError.throwError ("Unknown variable '" + name + "'");
  1159. }
  1160. void addVariable (Variable v, const CodeLocation& locationForError)
  1161. {
  1162. if (indexOf (variables, v.name) >= 0 || indexOf (constants, v.name) >= 0)
  1163. locationForError.throwError ("Variable '" + v.name + "' already exists");
  1164. (v.isConst ? constants : variables).add (v);
  1165. }
  1166. static int indexOf (const Array<Variable>& vars, const String& name) noexcept
  1167. {
  1168. for (int i = 0; i < vars.size(); ++i)
  1169. if (vars.getReference(i).name == name)
  1170. return i;
  1171. return -1;
  1172. }
  1173. Function* function;
  1174. Array<StatementPtr> statements;
  1175. Array<Variable> variables, constants;
  1176. bool isMainBlockOfFunction;
  1177. };
  1178. struct IfStatement : public Statement
  1179. {
  1180. IfStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1181. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1182. {
  1183. jassert (requiredType == Type::void_); ignoreUnused (requiredType);
  1184. condition->emit (cg, Type::bool_, stackDepth);
  1185. auto endOfStatement = cg.createMarker();
  1186. if (falseBranch == nullptr)
  1187. {
  1188. cg.emit (OpCode::jumpIfFalse, endOfStatement);
  1189. trueBranch->emit (cg, Type::void_, stackDepth);
  1190. }
  1191. else
  1192. {
  1193. auto elseTarget = cg.createMarker();
  1194. cg.emit (OpCode::jumpIfFalse, elseTarget);
  1195. trueBranch->emit (cg, Type::void_, stackDepth);
  1196. cg.emit (OpCode::jump, endOfStatement);
  1197. cg.attachMarker (elseTarget);
  1198. falseBranch->emit (cg, Type::void_, stackDepth);
  1199. }
  1200. cg.attachMarker (endOfStatement);
  1201. }
  1202. bool alwaysReturns() const override
  1203. {
  1204. return trueBranch->alwaysReturns() && falseBranch != nullptr && falseBranch->alwaysReturns();
  1205. }
  1206. void visitSubStatements (Statement::Visitor& visit) const override
  1207. {
  1208. visit (condition); visit (trueBranch); visit (falseBranch);
  1209. }
  1210. Statement* simplify (SyntaxTreeBuilder& stb) override
  1211. {
  1212. condition = condition->simplify (stb);
  1213. trueBranch = trueBranch->simplify (stb);
  1214. falseBranch = falseBranch != nullptr ? falseBranch->simplify (stb) : nullptr;
  1215. if (auto literal = dynamic_cast<LiteralValue*> (condition))
  1216. return literal->value ? trueBranch : (falseBranch != nullptr ? falseBranch : stb.allocate<Statement> (location, parentBlock));
  1217. return this;
  1218. }
  1219. ExpPtr condition;
  1220. StatementPtr trueBranch, falseBranch;
  1221. };
  1222. struct TernaryOp : public Expression
  1223. {
  1224. TernaryOp (const CodeLocation& l, BlockPtr parent) : Expression (l, parent) {}
  1225. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1226. {
  1227. condition->emit (cg, Type::bool_, stackDepth);
  1228. auto endOfStatement = cg.createMarker();
  1229. auto elseTarget = cg.createMarker();
  1230. cg.emit (OpCode::jumpIfFalse, elseTarget);
  1231. trueBranch->emit (cg, requiredType, stackDepth);
  1232. cg.emit (OpCode::jump, endOfStatement);
  1233. cg.attachMarker (elseTarget);
  1234. falseBranch->emit (cg, requiredType, stackDepth);
  1235. cg.attachMarker (endOfStatement);
  1236. }
  1237. Type getType (CodeGenerator& cg) const override
  1238. {
  1239. auto type = trueBranch->getType (cg);
  1240. if (type == Type::void_) location.throwError ("The ternary operator cannot take void arguments");
  1241. if (type != falseBranch->getType (cg)) location.throwError ("Expected both branches of this ternary operator to have the same type");
  1242. return type;
  1243. }
  1244. void visitSubStatements (Statement::Visitor& visit) const override
  1245. {
  1246. visit (condition); visit (trueBranch); visit (falseBranch);
  1247. }
  1248. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1249. {
  1250. condition = condition->simplify (stb);
  1251. trueBranch = trueBranch->simplify (stb);
  1252. falseBranch = falseBranch->simplify (stb);
  1253. if (auto literal = dynamic_cast<LiteralValue*> (condition))
  1254. return literal->value ? trueBranch : falseBranch;
  1255. return this;
  1256. }
  1257. ExpPtr condition, trueBranch, falseBranch;
  1258. };
  1259. struct LoopStatement : public Statement
  1260. {
  1261. LoopStatement (const CodeLocation& l, BlockPtr parent, bool isDo) noexcept : Statement (l, parent), isDoLoop (isDo) {}
  1262. void emit (CodeGenerator& cg, Type, int stackDepth) const override
  1263. {
  1264. initialiser->emit (cg, Type::void_, stackDepth);
  1265. auto loopStart = cg.createMarker();
  1266. cg.attachMarker (loopStart);
  1267. auto oldBreakTarget = cg.breakTarget;
  1268. auto oldContinueTarget = cg.continueTarget;
  1269. cg.breakTarget = cg.createMarker();
  1270. cg.continueTarget = cg.createMarker();
  1271. if (isDoLoop)
  1272. {
  1273. body->emit (cg, Type::void_, stackDepth);
  1274. cg.attachMarker (cg.continueTarget);
  1275. condition->emit (cg, Type::bool_, stackDepth);
  1276. cg.emit (OpCode::jumpIfTrue, loopStart);
  1277. }
  1278. else
  1279. {
  1280. condition->emit (cg, Type::bool_, stackDepth);
  1281. cg.emit (OpCode::jumpIfFalse, cg.breakTarget);
  1282. body->emit (cg, Type::void_, stackDepth);
  1283. cg.attachMarker (cg.continueTarget);
  1284. iterator->emit (cg, Type::void_, stackDepth);
  1285. cg.emit (OpCode::jump, loopStart);
  1286. }
  1287. cg.attachMarker (cg.breakTarget);
  1288. cg.breakTarget = oldBreakTarget;
  1289. cg.continueTarget = oldContinueTarget;
  1290. }
  1291. StatementPtr simplify (SyntaxTreeBuilder& stb) override
  1292. {
  1293. initialiser = initialiser->simplify (stb);
  1294. iterator = iterator->simplify (stb);
  1295. body = body->simplify (stb);
  1296. condition = condition->simplify (stb);
  1297. return this;
  1298. }
  1299. void visitSubStatements (Statement::Visitor& visit) const override
  1300. {
  1301. visit (condition); visit (initialiser); visit (iterator); visit (body);
  1302. }
  1303. StatementPtr initialiser, iterator, body;
  1304. ExpPtr condition;
  1305. bool isDoLoop;
  1306. };
  1307. struct ReturnStatement : public Statement
  1308. {
  1309. ReturnStatement (const CodeLocation& l, BlockPtr parent, ExpPtr v) noexcept : Statement (l, parent), returnValue (v) {}
  1310. void emit (CodeGenerator& cg, Type, int stackDepth) const override
  1311. {
  1312. if (auto fn = parentBlock->function)
  1313. {
  1314. if (returnValue != nullptr)
  1315. returnValue->emit (cg, fn->returnType, stackDepth);
  1316. else if (fn->returnType != Type::void_)
  1317. location.throwError ("Cannot return a value from a void function");
  1318. cg.emit (OpCode::jump, fn->unwindAddress);
  1319. return;
  1320. }
  1321. location.throwError ("The return statement can only be used inside a function");
  1322. }
  1323. bool alwaysReturns() const override { return true; }
  1324. StatementPtr simplify (SyntaxTreeBuilder& stb) override
  1325. {
  1326. if (returnValue != nullptr)
  1327. returnValue = returnValue->simplify (stb);
  1328. return this;
  1329. }
  1330. void visitSubStatements (Statement::Visitor& visit) const override
  1331. {
  1332. visit (returnValue);
  1333. }
  1334. ExpPtr returnValue;
  1335. };
  1336. struct BreakStatement : public Statement
  1337. {
  1338. BreakStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1339. void emit (CodeGenerator& cg, Type, int) const override
  1340. {
  1341. if (cg.breakTarget.index == 0)
  1342. location.throwError ("The break statement can only be used inside a loop");
  1343. cg.emit (OpCode::jump, cg.breakTarget);
  1344. }
  1345. };
  1346. struct ContinueStatement : public Statement
  1347. {
  1348. ContinueStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1349. void emit (CodeGenerator& cg, Type, int) const override
  1350. {
  1351. if (cg.continueTarget.index == 0)
  1352. location.throwError ("The continue statement can only be used inside a loop");
  1353. cg.emit (OpCode::jump, cg.continueTarget);
  1354. }
  1355. };
  1356. struct LiteralValue : public Expression
  1357. {
  1358. LiteralValue (const CodeLocation& l, BlockPtr parent, const var& v) noexcept : Expression (l, parent), value (v) {}
  1359. void emit (CodeGenerator& cg, Type requiredType, int) const override
  1360. {
  1361. if (requiredType != Type::void_)
  1362. {
  1363. auto type = getType (cg);
  1364. if (type != requiredType && value != var ((int) 0))
  1365. {
  1366. if (type == Type::int_ && requiredType == Type::bool_) return cg.emitPush (static_cast<bool> (value));
  1367. if (type == Type::int_ && requiredType == Type::float_) return cg.emitPush (static_cast<float> (value));
  1368. if (! (type == Type::bool_ && requiredType == Type::int_))
  1369. location.throwError ("Cannot cast from " + getTypeName (type) + " to " + getTypeName (requiredType));
  1370. }
  1371. cg.emitPush (value);
  1372. }
  1373. }
  1374. Type getType (CodeGenerator&) const override
  1375. {
  1376. auto t = getTypeOfVar (value);
  1377. if (t == Type::void_)
  1378. location.throwError ("Unsupported literal type");
  1379. return t;
  1380. }
  1381. var value;
  1382. };
  1383. struct Identifier : public Expression
  1384. {
  1385. Identifier (const CodeLocation& l, BlockPtr parent, const String& n) noexcept : Expression (l, parent), name (n) {}
  1386. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1387. {
  1388. cg.emitVariableRead (getType (cg), requiredType, stackDepth,
  1389. parentBlock->getVariableDepth (name, location), location);
  1390. }
  1391. Type getType (CodeGenerator&) const override
  1392. {
  1393. return parentBlock->getVariable (name, location).type;
  1394. }
  1395. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1396. {
  1397. auto& v = parentBlock->getVariable (name, location);
  1398. if (v.isConst)
  1399. return stb.allocate<LiteralValue> (location, parentBlock, v.constantValue);
  1400. return this;
  1401. }
  1402. String name;
  1403. };
  1404. struct UnaryOp : public Expression
  1405. {
  1406. UnaryOp (const CodeLocation& l, BlockPtr parent, ExpPtr a, TokenType op) noexcept
  1407. : Expression (l, parent), source (a), operation (op) {}
  1408. ExpPtr source;
  1409. TokenType operation;
  1410. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1411. {
  1412. auto sourceType = source->getType (cg);
  1413. if (operation == Token::minus)
  1414. {
  1415. cg.emitPush ((int) 0);
  1416. source->emit (cg, sourceType, stackDepth + 1);
  1417. cg.emit (sourceType == Type::float_ ? OpCode::sub_float : OpCode::sub_int32);
  1418. cg.emitCast (sourceType, requiredType, location);
  1419. }
  1420. else
  1421. {
  1422. if (sourceType == Type::float_)
  1423. location.throwError ("Cannot perform this operation on a float");
  1424. if (operation == Token::logicalNot)
  1425. {
  1426. source->emit (cg, sourceType, stackDepth);
  1427. cg.emit (OpCode::logicalNot);
  1428. cg.emitCast (Type::bool_, requiredType, location);
  1429. }
  1430. else if (operation == Token::bitwiseNot)
  1431. {
  1432. source->emit (cg, Type::int_, stackDepth);
  1433. cg.emit (OpCode::bitwiseNot);
  1434. cg.emitCast (Type::int_, requiredType, location);
  1435. }
  1436. }
  1437. }
  1438. Type getType (CodeGenerator& cg) const override
  1439. {
  1440. if (operation == Token::minus) return source->getType (cg);
  1441. if (operation == Token::logicalNot) return Type::bool_;
  1442. return Type::int_;
  1443. }
  1444. void visitSubStatements (Statement::Visitor& visit) const override
  1445. {
  1446. visit (source);
  1447. }
  1448. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1449. {
  1450. source = source->simplify (stb);
  1451. if (auto literal = dynamic_cast<LiteralValue*> (source))
  1452. {
  1453. auto type = getTypeOfVar (literal->value);
  1454. if (type == Type::int_ && operation == Token::minus) { literal->value = -static_cast<int> (literal->value); return literal; }
  1455. if (type == Type::int_ && operation == Token::bitwiseNot) { literal->value = ~static_cast<int> (literal->value); return literal; }
  1456. if (type == Type::int_ && operation == Token::logicalNot) { literal->value = ! static_cast<int> (literal->value); return literal; }
  1457. if (type == Type::bool_ && operation == Token::logicalNot) { literal->value = ! static_cast<bool> (literal->value); return literal; }
  1458. if (type == Type::float_ && operation == Token::minus) { literal->value = -static_cast<double> (literal->value); return literal; }
  1459. }
  1460. return this;
  1461. }
  1462. };
  1463. struct BinaryOperator : public Expression
  1464. {
  1465. BinaryOperator (const CodeLocation& l, BlockPtr parent, ExpPtr a, ExpPtr b, TokenType op) noexcept
  1466. : Expression (l, parent), lhs (a), rhs (b), operation (op) {}
  1467. ExpPtr lhs, rhs;
  1468. TokenType operation;
  1469. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1470. {
  1471. auto typeA = lhs->getType (cg);
  1472. auto typeB = rhs->getType (cg);
  1473. if (typeA == Type::float_ || typeB == Type::float_)
  1474. {
  1475. lhs->emit (cg, Type::float_, stackDepth);
  1476. rhs->emit (cg, Type::float_, stackDepth + 1);
  1477. if (operation == Token::plus) return emitOpAndCast (cg, requiredType, OpCode::add_float);
  1478. if (operation == Token::minus) return emitOpAndCast (cg, requiredType, OpCode::sub_float);
  1479. if (operation == Token::times) return emitOpAndCast (cg, requiredType, OpCode::mul_float);
  1480. if (operation == Token::divide) return emitOpAndCast (cg, requiredType, OpCode::div_float);
  1481. cg.emit (OpCode::sub_float);
  1482. if (operation == Token::equals) return emitOpAndCast (cg, requiredType, OpCode::testZE_float);
  1483. if (operation == Token::notEquals) return emitOpAndCast (cg, requiredType, OpCode::testNZ_float);
  1484. if (operation == Token::lessThan) return emitOpAndCast (cg, requiredType, OpCode::testLT_float);
  1485. if (operation == Token::lessThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testLE_float);
  1486. if (operation == Token::greaterThan) return emitOpAndCast (cg, requiredType, OpCode::testGT_float);
  1487. if (operation == Token::greaterThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testGE_float);
  1488. location.throwError ("The operator " + getTokenDescription (operation) + " cannot take floating point arguments");
  1489. }
  1490. auto type = (typeA == Type::bool_ || typeB == Type::bool_) ? Type::bool_
  1491. : Type::int_;
  1492. lhs->emit (cg, type, stackDepth);
  1493. rhs->emit (cg, type, stackDepth + 1);
  1494. if (operation == Token::plus) return emitOpAndCast (cg, requiredType, OpCode::add_int32);
  1495. if (operation == Token::minus) return emitOpAndCast (cg, requiredType, OpCode::sub_int32);
  1496. if (operation == Token::times) return emitOpAndCast (cg, requiredType, OpCode::mul_int32);
  1497. if (operation == Token::divide) return emitOpAndCast (cg, requiredType, OpCode::div_int32);
  1498. if (operation == Token::modulo) return emitOpAndCast (cg, requiredType, OpCode::mod_int32);
  1499. if (operation == Token::logicalOr) return emitOpAndCast (cg, requiredType, OpCode::logicalOr);
  1500. if (operation == Token::logicalAnd) return emitOpAndCast (cg, requiredType, OpCode::logicalAnd);
  1501. if (operation == Token::bitwiseOr) return emitOpAndCast (cg, requiredType, OpCode::bitwiseOr);
  1502. if (operation == Token::bitwiseAnd) return emitOpAndCast (cg, requiredType, OpCode::bitwiseAnd);
  1503. if (operation == Token::bitwiseXor) return emitOpAndCast (cg, requiredType, OpCode::bitwiseXor);
  1504. if (operation == Token::leftShift) return emitOpAndCast (cg, requiredType, OpCode::bitShiftLeft);
  1505. if (operation == Token::rightShift) return emitOpAndCast (cg, requiredType, OpCode::bitShiftRight);
  1506. cg.emit (OpCode::sub_int32);
  1507. if (operation == Token::equals) return emitOpAndCast (cg, requiredType, OpCode::testZE_int32);
  1508. if (operation == Token::notEquals) return emitOpAndCast (cg, requiredType, OpCode::testNZ_int32);
  1509. if (operation == Token::lessThan) return emitOpAndCast (cg, requiredType, OpCode::testLT_int32);
  1510. if (operation == Token::lessThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testLE_int32);
  1511. if (operation == Token::greaterThan) return emitOpAndCast (cg, requiredType, OpCode::testGT_int32);
  1512. if (operation == Token::greaterThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testGE_int32);
  1513. location.throwError ("Unsupported operator");
  1514. jassertfalse;
  1515. }
  1516. void emitOpAndCast (CodeGenerator& cg, Type requiredType, OpCode op) const
  1517. {
  1518. cg.emit (op);
  1519. cg.emitCast (getType (cg), requiredType, location);
  1520. }
  1521. Type getResultType (Type typeA, Type typeB) const noexcept
  1522. {
  1523. if (operation == Token::logicalOr || operation == Token::logicalAnd
  1524. || operation == Token::equals || operation == Token::notEquals
  1525. || operation == Token::lessThan || operation == Token::lessThanOrEqual
  1526. || operation == Token::greaterThan || operation == Token::greaterThanOrEqual)
  1527. return Type::bool_;
  1528. if (operation == Token::plus || operation == Token::minus
  1529. || operation == Token::times || operation == Token::divide)
  1530. {
  1531. if (typeA == Type::float_ || typeB == Type::float_)
  1532. return Type::float_;
  1533. }
  1534. return Type::int_;
  1535. }
  1536. Type getType (CodeGenerator& cg) const override
  1537. {
  1538. return getResultType (lhs->getType (cg), rhs->getType (cg));
  1539. }
  1540. void visitSubStatements (Statement::Visitor& visit) const override
  1541. {
  1542. visit (lhs); visit (rhs);
  1543. }
  1544. ExpPtr simplifyFloat (double a, double b, LiteralValue* literal)
  1545. {
  1546. if (operation == Token::plus) { literal->value = a + b; return literal; }
  1547. if (operation == Token::minus) { literal->value = a - b; return literal; }
  1548. if (operation == Token::times) { literal->value = a * b; return literal; }
  1549. if (operation == Token::divide) { literal->value = a / b; return literal; }
  1550. if (operation == Token::equals) { literal->value = a == b; return literal; }
  1551. if (operation == Token::notEquals) { literal->value = a != b; return literal; }
  1552. if (operation == Token::lessThan) { literal->value = a < b; return literal; }
  1553. if (operation == Token::lessThanOrEqual) { literal->value = a <= b; return literal; }
  1554. if (operation == Token::greaterThan) { literal->value = a > b; return literal; }
  1555. if (operation == Token::greaterThanOrEqual) { literal->value = a >= b; return literal; }
  1556. return this;
  1557. }
  1558. ExpPtr simplifyBool (bool a, bool b, LiteralValue* literal)
  1559. {
  1560. if (operation == Token::logicalOr) { literal->value = a || b; return literal; }
  1561. if (operation == Token::logicalAnd) { literal->value = a && b; return literal; }
  1562. return this;
  1563. }
  1564. ExpPtr simplifyInt (int a, int b, LiteralValue* literal)
  1565. {
  1566. if (operation == Token::plus) { literal->value = a + b; return literal; }
  1567. if (operation == Token::minus) { literal->value = a - b; return literal; }
  1568. if (operation == Token::times) { literal->value = a * b; return literal; }
  1569. if (operation == Token::divide) { literal->value = a / b; return literal; }
  1570. if (operation == Token::equals) { literal->value = a == b; return literal; }
  1571. if (operation == Token::notEquals) { literal->value = a != b; return literal; }
  1572. if (operation == Token::lessThan) { literal->value = a < b; return literal; }
  1573. if (operation == Token::lessThanOrEqual) { literal->value = a <= b; return literal; }
  1574. if (operation == Token::greaterThan) { literal->value = a > b; return literal; }
  1575. if (operation == Token::greaterThanOrEqual) { literal->value = a >= b; return literal; }
  1576. if (operation == Token::modulo) { literal->value = a % b; return literal; }
  1577. if (operation == Token::logicalOr) { literal->value = a || b; return literal; }
  1578. if (operation == Token::logicalAnd) { literal->value = a && b; return literal; }
  1579. if (operation == Token::bitwiseOr) { literal->value = a | b; return literal; }
  1580. if (operation == Token::bitwiseAnd) { literal->value = a & b; return literal; }
  1581. if (operation == Token::bitwiseXor) { literal->value = a ^ b; return literal; }
  1582. if (operation == Token::leftShift) { literal->value = a << b; return literal; }
  1583. if (operation == Token::rightShift) { literal->value = a >> b; return literal; }
  1584. return this;
  1585. }
  1586. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1587. {
  1588. lhs = lhs->simplify (stb);
  1589. rhs = rhs->simplify (stb);
  1590. if (auto literal1 = dynamic_cast<LiteralValue*> (lhs))
  1591. {
  1592. if (auto literal2 = dynamic_cast<LiteralValue*> (rhs))
  1593. {
  1594. auto resultType = getResultType (getTypeOfVar (literal1->value),
  1595. getTypeOfVar (literal2->value));
  1596. if (resultType == Type::bool_) return simplifyBool (literal1->value, literal2->value, literal1);
  1597. if (resultType == Type::int_) return simplifyInt (literal1->value, literal2->value, literal1);
  1598. if (resultType == Type::float_) return simplifyFloat (literal1->value, literal2->value, literal1);
  1599. }
  1600. }
  1601. return this;
  1602. }
  1603. };
  1604. struct Assignment : public Expression
  1605. {
  1606. Assignment (const CodeLocation& l, BlockPtr parent, const String& dest, ExpPtr source, bool isPost) noexcept
  1607. : Expression (l, parent), target (dest), newValue (source), isPostAssignment (isPost) {}
  1608. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1609. {
  1610. auto variableType = getType (cg);
  1611. if (isPostAssignment && requiredType != Type::void_)
  1612. {
  1613. cg.emitVariableRead (variableType, requiredType, stackDepth,
  1614. parentBlock->getVariableDepth (target, location), location);
  1615. ++stackDepth;
  1616. requiredType = Type::void_;
  1617. }
  1618. newValue->emit (cg, variableType, stackDepth);
  1619. auto index = parentBlock->getVariableDepth (target, location);
  1620. if (requiredType != Type::void_)
  1621. {
  1622. cg.emit (OpCode::dup);
  1623. ++stackDepth;
  1624. }
  1625. if (index < 0)
  1626. {
  1627. cg.emit (OpCode::dropToGlobal, (int16) ((-index) - 1));
  1628. }
  1629. else
  1630. {
  1631. index += stackDepth;
  1632. if (index >= 128)
  1633. cg.emit (OpCode::dropToStack16, (int16) index);
  1634. else
  1635. cg.emit (OpCode::dropToStack, (int8) index);
  1636. }
  1637. if (requiredType != Type::void_)
  1638. cg.emitCast (variableType, requiredType, location);
  1639. }
  1640. Type getType (CodeGenerator&) const override
  1641. {
  1642. return parentBlock->getVariable (target, location).type;
  1643. }
  1644. void visitSubStatements (Statement::Visitor& visit) const override
  1645. {
  1646. visit (newValue);
  1647. }
  1648. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1649. {
  1650. newValue = newValue->simplify (stb);
  1651. return this;
  1652. }
  1653. String target;
  1654. ExpPtr newValue;
  1655. bool isPostAssignment;
  1656. };
  1657. struct FunctionCall : public Expression
  1658. {
  1659. FunctionCall (const CodeLocation& l, BlockPtr parent) noexcept : Expression (l, parent) {}
  1660. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1661. {
  1662. if (functionName == Token::int_) return emitCast (cg, Type::int_, stackDepth);
  1663. if (functionName == Token::float_) return emitCast (cg, Type::float_, stackDepth);
  1664. if (functionName == Token::bool_) return emitCast (cg, Type::bool_, stackDepth);
  1665. auto functionID = getFunctionID (cg);
  1666. if (auto fn = cg.syntaxTree.findFunction (functionID))
  1667. {
  1668. emitArgs (cg, fn->getArgumentTypes(), stackDepth);
  1669. cg.emit (OpCode::call, fn->address);
  1670. cg.emitCast (fn->returnType, requiredType, location);
  1671. return;
  1672. }
  1673. if (auto nativeFn = cg.syntaxTree.findNativeFunction (functionID))
  1674. {
  1675. emitArgs (cg, getArgTypesFromFunctionName (nativeFn->nameAndArguments), stackDepth);
  1676. cg.emit (OpCode::callNative, nativeFn->functionID);
  1677. cg.emitCast (nativeFn->returnType, requiredType, location);
  1678. return;
  1679. }
  1680. if (auto b = findBuiltInFunction (functionID))
  1681. {
  1682. emitArgs (cg, getArgTypesFromFunctionName (b->name), stackDepth);
  1683. cg.emit (b->op);
  1684. cg.emitCast (b->returnType, requiredType, location);
  1685. return;
  1686. }
  1687. throwCannotFindFunctionError (cg, requiredType);
  1688. }
  1689. Type getType (CodeGenerator& cg) const override
  1690. {
  1691. if (arguments.size() == 1)
  1692. {
  1693. if (functionName == Token::float_) return Type::float_;
  1694. if (functionName == Token::int_) return Type::int_;
  1695. if (functionName == Token::bool_) return Type::bool_;
  1696. }
  1697. auto functionID = getFunctionID (cg);
  1698. if (auto fn = cg.syntaxTree.findFunction (functionID))
  1699. return fn->returnType;
  1700. if (auto nativeFn = cg.syntaxTree.findNativeFunction (functionID))
  1701. return nativeFn->returnType;
  1702. if (auto b = findBuiltInFunction (functionID))
  1703. return b->returnType;
  1704. throwCannotFindFunctionError (cg, Type::void_);
  1705. return {};
  1706. }
  1707. struct BuiltInFunction { OpCode op; Type returnType; const char* name; };
  1708. const BuiltInFunction* findBuiltInFunction (FunctionID functionID) const noexcept
  1709. {
  1710. static constexpr const BuiltInFunction builtIns[] =
  1711. {
  1712. { OpCode::getHeapByte, Type::int_, "getHeapByte/ii" },
  1713. { OpCode::getHeapInt, Type::int_, "getHeapInt/ii" },
  1714. { OpCode::getHeapBits, Type::int_, "getHeapBits/iii" },
  1715. { OpCode::setHeapByte, Type::void_, "setHeapByte/vii" },
  1716. { OpCode::setHeapInt, Type::void_, "setHeapInt/vii" }
  1717. };
  1718. for (auto& b : builtIns)
  1719. if (functionID == NativeFunction::createID (b.name))
  1720. return &b;
  1721. return nullptr;
  1722. }
  1723. void emitArgs (CodeGenerator& cg, const Array<Type>& argTypes, int stackDepth) const
  1724. {
  1725. for (int i = arguments.size(); --i >= 0;)
  1726. {
  1727. auto argType = argTypes[i];
  1728. auto argValue = arguments.getUnchecked(i);
  1729. if (argValue->getType (cg) != argType)
  1730. location.throwError ("Argument " + String (i + 1) + " requires an expression of type " + getTypeName (argType));
  1731. argValue->emit (cg, argType, stackDepth++);
  1732. }
  1733. }
  1734. void emitCast (CodeGenerator& cg, Type destType, int stackDepth) const
  1735. {
  1736. if (arguments.size() != 1)
  1737. location.throwError (getTypeName (destType) + " cast operation requires a single argument");
  1738. auto* arg = arguments.getReference (0);
  1739. const auto sourceType = arg->getType (cg);
  1740. arg->emit (cg, sourceType, stackDepth);
  1741. const bool sourceIsFloat = (sourceType == Type::float_);
  1742. const bool destIsFloat = (destType == Type::float_);
  1743. if (sourceIsFloat != destIsFloat)
  1744. cg.emit (destIsFloat ? OpCode::int32ToFloat : OpCode::floatToInt32);
  1745. }
  1746. FunctionID getFunctionID (CodeGenerator& cg) const
  1747. {
  1748. Array<Type> argTypes;
  1749. for (auto arg : arguments)
  1750. argTypes.add (arg->getType (cg));
  1751. return createFunctionID (functionName, Type::void_, argTypes); // NB: the ID ignores the return type so void is OK
  1752. }
  1753. void throwCannotFindFunctionError (CodeGenerator& cg, Type returnType) const
  1754. {
  1755. StringArray args;
  1756. for (auto arg : arguments)
  1757. args.add (getTypeName (arg->getType (cg)));
  1758. auto desc = getTypeName (returnType) + " " + functionName
  1759. + "(" + args.joinIntoString (", ") + ")";
  1760. location.throwError ("Cannot find matching function: " + desc.quoted());
  1761. }
  1762. void visitSubStatements (Statement::Visitor& visit) const override
  1763. {
  1764. for (auto& arg : arguments)
  1765. visit (arg);
  1766. }
  1767. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1768. {
  1769. for (auto& arg : arguments)
  1770. arg = arg->simplify (stb);
  1771. return this;
  1772. }
  1773. String functionName;
  1774. Array<ExpPtr> arguments;
  1775. };
  1776. struct ArraySubscript : public Expression
  1777. {
  1778. ArraySubscript (const CodeLocation& l, BlockPtr parent) noexcept : Expression (l, parent) {}
  1779. void emit (CodeGenerator&, Type /*requiredType*/, int /*stackDepth*/) const override
  1780. {
  1781. location.throwError ("Arrays are not implemented yet!");
  1782. }
  1783. void visitSubStatements (Statement::Visitor& visit) const override
  1784. {
  1785. visit (object); visit (index);
  1786. }
  1787. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1788. {
  1789. object = object->simplify (stb);
  1790. index = index->simplify (stb);
  1791. return this;
  1792. }
  1793. Type getType (CodeGenerator& cg) const override
  1794. {
  1795. return object->getType (cg);
  1796. }
  1797. ExpPtr object, index;
  1798. };
  1799. //==============================================================================
  1800. static Array<Type> getArgTypesFromFunctionName (const char* nameAndTypes)
  1801. {
  1802. Array<Type> types;
  1803. auto args = String (nameAndTypes).fromFirstOccurrenceOf ("/", false, false).substring (1);
  1804. for (int i = 0; i < args.length(); ++i)
  1805. types.add (static_cast<Type> (args[i]));
  1806. return types;
  1807. }
  1808. static FunctionID createFunctionID (String name, Type returnType, const Array<Type>& types)
  1809. {
  1810. name << "/" << (char) returnType;
  1811. for (auto t : types)
  1812. name << (char) t;
  1813. return NativeFunction::createID (name.toRawUTF8());
  1814. }
  1815. static String getTokenDescription (TokenType t) { return t[0] == '$' ? String (t + 1) : ("'" + String (t) + "'"); }
  1816. static String getTypeName (Type t) noexcept
  1817. {
  1818. if (t == Type::int_) return "int";
  1819. if (t == Type::bool_) return "bool";
  1820. if (t == Type::float_) return "float";
  1821. return "void";
  1822. }
  1823. static Type tokenToType (TokenType t) noexcept
  1824. {
  1825. if (t == Token::int_) return Type::int_;
  1826. if (t == Token::bool_) return Type::bool_;
  1827. if (t == Token::float_) return Type::float_;
  1828. return Type::void_;
  1829. }
  1830. static Type getTypeOfVar (const var& v) noexcept
  1831. {
  1832. if (v.isInt() || v.isInt64()) return Type::int_;
  1833. if (v.isDouble()) return Type::float_;
  1834. if (v.isBool()) return Type::bool_;
  1835. return Type::void_;
  1836. }
  1837. #endif // ! DOXYGEN
  1838. };
  1839. }
  1840. #if JUCE_MSVC
  1841. #pragma warning (pop)
  1842. #endif