1 #include "llvm/DerivedTypes.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/JIT.h"
4 #include "llvm/LLVMContext.h"
5 #include "llvm/Module.h"
6 #include "llvm/PassManager.h"
7 #include "llvm/Analysis/Verifier.h"
8 #include "llvm/Target/TargetData.h"
9 #include "llvm/Target/TargetSelect.h"
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/Support/IRBuilder.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
23 // of these for known things.
28 tok_def
= -2, tok_extern
= -3,
31 tok_identifier
= -4, tok_number
= -5,
34 tok_if
= -6, tok_then
= -7, tok_else
= -8,
35 tok_for
= -9, tok_in
= -10
38 static std::string IdentifierStr
; // Filled in if tok_identifier
39 static double NumVal
; // Filled in if tok_number
41 /// gettok - Return the next token from standard input.
43 static int LastChar
= ' ';
45 // Skip any whitespace.
46 while (isspace(LastChar
))
49 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
50 IdentifierStr
= LastChar
;
51 while (isalnum((LastChar
= getchar())))
52 IdentifierStr
+= LastChar
;
54 if (IdentifierStr
== "def") return tok_def
;
55 if (IdentifierStr
== "extern") return tok_extern
;
56 if (IdentifierStr
== "if") return tok_if
;
57 if (IdentifierStr
== "then") return tok_then
;
58 if (IdentifierStr
== "else") return tok_else
;
59 if (IdentifierStr
== "for") return tok_for
;
60 if (IdentifierStr
== "in") return tok_in
;
61 return tok_identifier
;
64 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
69 } while (isdigit(LastChar
) || LastChar
== '.');
71 NumVal
= strtod(NumStr
.c_str(), 0);
75 if (LastChar
== '#') {
76 // Comment until end of line.
77 do LastChar
= getchar();
78 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
84 // Check for end of file. Don't eat the EOF.
88 // Otherwise, just return the character as its ascii value.
89 int ThisChar
= LastChar
;
94 //===----------------------------------------------------------------------===//
95 // Abstract Syntax Tree (aka Parse Tree)
96 //===----------------------------------------------------------------------===//
98 /// ExprAST - Base class for all expression nodes.
101 virtual ~ExprAST() {}
102 virtual Value
*Codegen() = 0;
105 /// NumberExprAST - Expression class for numeric literals like "1.0".
106 class NumberExprAST
: public ExprAST
{
109 NumberExprAST(double val
) : Val(val
) {}
110 virtual Value
*Codegen();
113 /// VariableExprAST - Expression class for referencing a variable, like "a".
114 class VariableExprAST
: public ExprAST
{
117 VariableExprAST(const std::string
&name
) : Name(name
) {}
118 virtual Value
*Codegen();
121 /// BinaryExprAST - Expression class for a binary operator.
122 class BinaryExprAST
: public ExprAST
{
126 BinaryExprAST(char op
, ExprAST
*lhs
, ExprAST
*rhs
)
127 : Op(op
), LHS(lhs
), RHS(rhs
) {}
128 virtual Value
*Codegen();
131 /// CallExprAST - Expression class for function calls.
132 class CallExprAST
: public ExprAST
{
134 std::vector
<ExprAST
*> Args
;
136 CallExprAST(const std::string
&callee
, std::vector
<ExprAST
*> &args
)
137 : Callee(callee
), Args(args
) {}
138 virtual Value
*Codegen();
141 /// IfExprAST - Expression class for if/then/else.
142 class IfExprAST
: public ExprAST
{
143 ExprAST
*Cond
, *Then
, *Else
;
145 IfExprAST(ExprAST
*cond
, ExprAST
*then
, ExprAST
*_else
)
146 : Cond(cond
), Then(then
), Else(_else
) {}
147 virtual Value
*Codegen();
150 /// ForExprAST - Expression class for for/in.
151 class ForExprAST
: public ExprAST
{
153 ExprAST
*Start
, *End
, *Step
, *Body
;
155 ForExprAST(const std::string
&varname
, ExprAST
*start
, ExprAST
*end
,
156 ExprAST
*step
, ExprAST
*body
)
157 : VarName(varname
), Start(start
), End(end
), Step(step
), Body(body
) {}
158 virtual Value
*Codegen();
161 /// PrototypeAST - This class represents the "prototype" for a function,
162 /// which captures its name, and its argument names (thus implicitly the number
163 /// of arguments the function takes).
166 std::vector
<std::string
> Args
;
168 PrototypeAST(const std::string
&name
, const std::vector
<std::string
> &args
)
169 : Name(name
), Args(args
) {}
174 /// FunctionAST - This class represents a function definition itself.
179 FunctionAST(PrototypeAST
*proto
, ExprAST
*body
)
180 : Proto(proto
), Body(body
) {}
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
190 /// token the parser is looking at. getNextToken reads another token from the
191 /// lexer and updates CurTok with its results.
193 static int getNextToken() {
194 return CurTok
= gettok();
197 /// BinopPrecedence - This holds the precedence for each binary operator that is
199 static std::map
<char, int> BinopPrecedence
;
201 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
202 static int GetTokPrecedence() {
203 if (!isascii(CurTok
))
206 // Make sure it's a declared binop.
207 int TokPrec
= BinopPrecedence
[CurTok
];
208 if (TokPrec
<= 0) return -1;
212 /// Error* - These are little helper functions for error handling.
213 ExprAST
*Error(const char *Str
) { fprintf(stderr
, "Error: %s\n", Str
);return 0;}
214 PrototypeAST
*ErrorP(const char *Str
) { Error(Str
); return 0; }
215 FunctionAST
*ErrorF(const char *Str
) { Error(Str
); return 0; }
217 static ExprAST
*ParseExpression();
221 /// ::= identifier '(' expression* ')'
222 static ExprAST
*ParseIdentifierExpr() {
223 std::string IdName
= IdentifierStr
;
225 getNextToken(); // eat identifier.
227 if (CurTok
!= '(') // Simple variable ref.
228 return new VariableExprAST(IdName
);
231 getNextToken(); // eat (
232 std::vector
<ExprAST
*> Args
;
235 ExprAST
*Arg
= ParseExpression();
239 if (CurTok
== ')') break;
242 return Error("Expected ')' or ',' in argument list");
250 return new CallExprAST(IdName
, Args
);
253 /// numberexpr ::= number
254 static ExprAST
*ParseNumberExpr() {
255 ExprAST
*Result
= new NumberExprAST(NumVal
);
256 getNextToken(); // consume the number
260 /// parenexpr ::= '(' expression ')'
261 static ExprAST
*ParseParenExpr() {
262 getNextToken(); // eat (.
263 ExprAST
*V
= ParseExpression();
267 return Error("expected ')'");
268 getNextToken(); // eat ).
272 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
273 static ExprAST
*ParseIfExpr() {
274 getNextToken(); // eat the if.
277 ExprAST
*Cond
= ParseExpression();
280 if (CurTok
!= tok_then
)
281 return Error("expected then");
282 getNextToken(); // eat the then
284 ExprAST
*Then
= ParseExpression();
285 if (Then
== 0) return 0;
287 if (CurTok
!= tok_else
)
288 return Error("expected else");
292 ExprAST
*Else
= ParseExpression();
295 return new IfExprAST(Cond
, Then
, Else
);
298 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
299 static ExprAST
*ParseForExpr() {
300 getNextToken(); // eat the for.
302 if (CurTok
!= tok_identifier
)
303 return Error("expected identifier after for");
305 std::string IdName
= IdentifierStr
;
306 getNextToken(); // eat identifier.
309 return Error("expected '=' after for");
310 getNextToken(); // eat '='.
313 ExprAST
*Start
= ParseExpression();
314 if (Start
== 0) return 0;
316 return Error("expected ',' after for start value");
319 ExprAST
*End
= ParseExpression();
320 if (End
== 0) return 0;
322 // The step value is optional.
326 Step
= ParseExpression();
327 if (Step
== 0) return 0;
330 if (CurTok
!= tok_in
)
331 return Error("expected 'in' after for");
332 getNextToken(); // eat 'in'.
334 ExprAST
*Body
= ParseExpression();
335 if (Body
== 0) return 0;
337 return new ForExprAST(IdName
, Start
, End
, Step
, Body
);
341 /// ::= identifierexpr
346 static ExprAST
*ParsePrimary() {
348 default: return Error("unknown token when expecting an expression");
349 case tok_identifier
: return ParseIdentifierExpr();
350 case tok_number
: return ParseNumberExpr();
351 case '(': return ParseParenExpr();
352 case tok_if
: return ParseIfExpr();
353 case tok_for
: return ParseForExpr();
358 /// ::= ('+' primary)*
359 static ExprAST
*ParseBinOpRHS(int ExprPrec
, ExprAST
*LHS
) {
360 // If this is a binop, find its precedence.
362 int TokPrec
= GetTokPrecedence();
364 // If this is a binop that binds at least as tightly as the current binop,
365 // consume it, otherwise we are done.
366 if (TokPrec
< ExprPrec
)
369 // Okay, we know this is a binop.
371 getNextToken(); // eat binop
373 // Parse the primary expression after the binary operator.
374 ExprAST
*RHS
= ParsePrimary();
377 // If BinOp binds less tightly with RHS than the operator after RHS, let
378 // the pending operator take RHS as its LHS.
379 int NextPrec
= GetTokPrecedence();
380 if (TokPrec
< NextPrec
) {
381 RHS
= ParseBinOpRHS(TokPrec
+1, RHS
);
382 if (RHS
== 0) return 0;
386 LHS
= new BinaryExprAST(BinOp
, LHS
, RHS
);
391 /// ::= primary binoprhs
393 static ExprAST
*ParseExpression() {
394 ExprAST
*LHS
= ParsePrimary();
397 return ParseBinOpRHS(0, LHS
);
401 /// ::= id '(' id* ')'
402 static PrototypeAST
*ParsePrototype() {
403 if (CurTok
!= tok_identifier
)
404 return ErrorP("Expected function name in prototype");
406 std::string FnName
= IdentifierStr
;
410 return ErrorP("Expected '(' in prototype");
412 std::vector
<std::string
> ArgNames
;
413 while (getNextToken() == tok_identifier
)
414 ArgNames
.push_back(IdentifierStr
);
416 return ErrorP("Expected ')' in prototype");
419 getNextToken(); // eat ')'.
421 return new PrototypeAST(FnName
, ArgNames
);
424 /// definition ::= 'def' prototype expression
425 static FunctionAST
*ParseDefinition() {
426 getNextToken(); // eat def.
427 PrototypeAST
*Proto
= ParsePrototype();
428 if (Proto
== 0) return 0;
430 if (ExprAST
*E
= ParseExpression())
431 return new FunctionAST(Proto
, E
);
435 /// toplevelexpr ::= expression
436 static FunctionAST
*ParseTopLevelExpr() {
437 if (ExprAST
*E
= ParseExpression()) {
438 // Make an anonymous proto.
439 PrototypeAST
*Proto
= new PrototypeAST("", std::vector
<std::string
>());
440 return new FunctionAST(Proto
, E
);
445 /// external ::= 'extern' prototype
446 static PrototypeAST
*ParseExtern() {
447 getNextToken(); // eat extern.
448 return ParsePrototype();
451 //===----------------------------------------------------------------------===//
453 //===----------------------------------------------------------------------===//
455 static Module
*TheModule
;
456 static IRBuilder
<> Builder(getGlobalContext());
457 static std::map
<std::string
, Value
*> NamedValues
;
458 static FunctionPassManager
*TheFPM
;
460 Value
*ErrorV(const char *Str
) { Error(Str
); return 0; }
462 Value
*NumberExprAST::Codegen() {
463 return ConstantFP::get(getGlobalContext(), APFloat(Val
));
466 Value
*VariableExprAST::Codegen() {
467 // Look this variable up in the function.
468 Value
*V
= NamedValues
[Name
];
469 return V
? V
: ErrorV("Unknown variable name");
472 Value
*BinaryExprAST::Codegen() {
473 Value
*L
= LHS
->Codegen();
474 Value
*R
= RHS
->Codegen();
475 if (L
== 0 || R
== 0) return 0;
478 case '+': return Builder
.CreateFAdd(L
, R
, "addtmp");
479 case '-': return Builder
.CreateFSub(L
, R
, "subtmp");
480 case '*': return Builder
.CreateFMul(L
, R
, "multmp");
482 L
= Builder
.CreateFCmpULT(L
, R
, "cmptmp");
483 // Convert bool 0/1 to double 0.0 or 1.0
484 return Builder
.CreateUIToFP(L
, Type::getDoubleTy(getGlobalContext()),
486 default: return ErrorV("invalid binary operator");
490 Value
*CallExprAST::Codegen() {
491 // Look up the name in the global module table.
492 Function
*CalleeF
= TheModule
->getFunction(Callee
);
494 return ErrorV("Unknown function referenced");
496 // If argument mismatch error.
497 if (CalleeF
->arg_size() != Args
.size())
498 return ErrorV("Incorrect # arguments passed");
500 std::vector
<Value
*> ArgsV
;
501 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
502 ArgsV
.push_back(Args
[i
]->Codegen());
503 if (ArgsV
.back() == 0) return 0;
506 return Builder
.CreateCall(CalleeF
, ArgsV
.begin(), ArgsV
.end(), "calltmp");
509 Value
*IfExprAST::Codegen() {
510 Value
*CondV
= Cond
->Codegen();
511 if (CondV
== 0) return 0;
513 // Convert condition to a bool by comparing equal to 0.0.
514 CondV
= Builder
.CreateFCmpONE(CondV
,
515 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
518 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
520 // Create blocks for the then and else cases. Insert the 'then' block at the
521 // end of the function.
522 BasicBlock
*ThenBB
= BasicBlock::Create(getGlobalContext(), "then", TheFunction
);
523 BasicBlock
*ElseBB
= BasicBlock::Create(getGlobalContext(), "else");
524 BasicBlock
*MergeBB
= BasicBlock::Create(getGlobalContext(), "ifcont");
526 Builder
.CreateCondBr(CondV
, ThenBB
, ElseBB
);
529 Builder
.SetInsertPoint(ThenBB
);
531 Value
*ThenV
= Then
->Codegen();
532 if (ThenV
== 0) return 0;
534 Builder
.CreateBr(MergeBB
);
535 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
536 ThenBB
= Builder
.GetInsertBlock();
539 TheFunction
->getBasicBlockList().push_back(ElseBB
);
540 Builder
.SetInsertPoint(ElseBB
);
542 Value
*ElseV
= Else
->Codegen();
543 if (ElseV
== 0) return 0;
545 Builder
.CreateBr(MergeBB
);
546 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
547 ElseBB
= Builder
.GetInsertBlock();
550 TheFunction
->getBasicBlockList().push_back(MergeBB
);
551 Builder
.SetInsertPoint(MergeBB
);
552 PHINode
*PN
= Builder
.CreatePHI(Type::getDoubleTy(getGlobalContext()),
555 PN
->addIncoming(ThenV
, ThenBB
);
556 PN
->addIncoming(ElseV
, ElseBB
);
560 Value
*ForExprAST::Codegen() {
566 // variable = phi [start, loopheader], [nextvariable, loopend]
572 // nextvariable = variable + step
574 // br endcond, loop, endloop
577 // Emit the start code first, without 'variable' in scope.
578 Value
*StartVal
= Start
->Codegen();
579 if (StartVal
== 0) return 0;
581 // Make the new basic block for the loop header, inserting after current
583 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
584 BasicBlock
*PreheaderBB
= Builder
.GetInsertBlock();
585 BasicBlock
*LoopBB
= BasicBlock::Create(getGlobalContext(), "loop", TheFunction
);
587 // Insert an explicit fall through from the current block to the LoopBB.
588 Builder
.CreateBr(LoopBB
);
590 // Start insertion in LoopBB.
591 Builder
.SetInsertPoint(LoopBB
);
593 // Start the PHI node with an entry for Start.
594 PHINode
*Variable
= Builder
.CreatePHI(Type::getDoubleTy(getGlobalContext()), VarName
.c_str());
595 Variable
->addIncoming(StartVal
, PreheaderBB
);
597 // Within the loop, the variable is defined equal to the PHI node. If it
598 // shadows an existing variable, we have to restore it, so save it now.
599 Value
*OldVal
= NamedValues
[VarName
];
600 NamedValues
[VarName
] = Variable
;
602 // Emit the body of the loop. This, like any other expr, can change the
603 // current BB. Note that we ignore the value computed by the body, but don't
605 if (Body
->Codegen() == 0)
608 // Emit the step value.
611 StepVal
= Step
->Codegen();
612 if (StepVal
== 0) return 0;
614 // If not specified, use 1.0.
615 StepVal
= ConstantFP::get(getGlobalContext(), APFloat(1.0));
618 Value
*NextVar
= Builder
.CreateFAdd(Variable
, StepVal
, "nextvar");
620 // Compute the end condition.
621 Value
*EndCond
= End
->Codegen();
622 if (EndCond
== 0) return EndCond
;
624 // Convert condition to a bool by comparing equal to 0.0.
625 EndCond
= Builder
.CreateFCmpONE(EndCond
,
626 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
629 // Create the "after loop" block and insert it.
630 BasicBlock
*LoopEndBB
= Builder
.GetInsertBlock();
631 BasicBlock
*AfterBB
= BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction
);
633 // Insert the conditional branch into the end of LoopEndBB.
634 Builder
.CreateCondBr(EndCond
, LoopBB
, AfterBB
);
636 // Any new code will be inserted in AfterBB.
637 Builder
.SetInsertPoint(AfterBB
);
639 // Add a new entry to the PHI node for the backedge.
640 Variable
->addIncoming(NextVar
, LoopEndBB
);
642 // Restore the unshadowed variable.
644 NamedValues
[VarName
] = OldVal
;
646 NamedValues
.erase(VarName
);
649 // for expr always returns 0.0.
650 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
653 Function
*PrototypeAST::Codegen() {
654 // Make the function type: double(double,double) etc.
655 std::vector
<const Type
*> Doubles(Args
.size(),
656 Type::getDoubleTy(getGlobalContext()));
657 FunctionType
*FT
= FunctionType::get(Type::getDoubleTy(getGlobalContext()),
660 Function
*F
= Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
);
662 // If F conflicted, there was already something named 'Name'. If it has a
663 // body, don't allow redefinition or reextern.
664 if (F
->getName() != Name
) {
665 // Delete the one we just made and get the existing one.
666 F
->eraseFromParent();
667 F
= TheModule
->getFunction(Name
);
669 // If F already has a body, reject this.
671 ErrorF("redefinition of function");
675 // If F took a different number of args, reject.
676 if (F
->arg_size() != Args
.size()) {
677 ErrorF("redefinition of function with different # args");
682 // Set names for all arguments.
684 for (Function::arg_iterator AI
= F
->arg_begin(); Idx
!= Args
.size();
686 AI
->setName(Args
[Idx
]);
688 // Add arguments to variable symbol table.
689 NamedValues
[Args
[Idx
]] = AI
;
695 Function
*FunctionAST::Codegen() {
698 Function
*TheFunction
= Proto
->Codegen();
699 if (TheFunction
== 0)
702 // Create a new basic block to start insertion into.
703 BasicBlock
*BB
= BasicBlock::Create(getGlobalContext(), "entry", TheFunction
);
704 Builder
.SetInsertPoint(BB
);
706 if (Value
*RetVal
= Body
->Codegen()) {
707 // Finish off the function.
708 Builder
.CreateRet(RetVal
);
710 // Validate the generated code, checking for consistency.
711 verifyFunction(*TheFunction
);
713 // Optimize the function.
714 TheFPM
->run(*TheFunction
);
719 // Error reading body, remove function.
720 TheFunction
->eraseFromParent();
724 //===----------------------------------------------------------------------===//
725 // Top-Level parsing and JIT Driver
726 //===----------------------------------------------------------------------===//
728 static ExecutionEngine
*TheExecutionEngine
;
730 static void HandleDefinition() {
731 if (FunctionAST
*F
= ParseDefinition()) {
732 if (Function
*LF
= F
->Codegen()) {
733 fprintf(stderr
, "Read function definition:");
737 // Skip token for error recovery.
742 static void HandleExtern() {
743 if (PrototypeAST
*P
= ParseExtern()) {
744 if (Function
*F
= P
->Codegen()) {
745 fprintf(stderr
, "Read extern: ");
749 // Skip token for error recovery.
754 static void HandleTopLevelExpression() {
755 // Evaluate a top-level expression into an anonymous function.
756 if (FunctionAST
*F
= ParseTopLevelExpr()) {
757 if (Function
*LF
= F
->Codegen()) {
758 // JIT the function, returning a function pointer.
759 void *FPtr
= TheExecutionEngine
->getPointerToFunction(LF
);
761 // Cast it to the right type (takes no arguments, returns a double) so we
762 // can call it as a native function.
763 double (*FP
)() = (double (*)())(intptr_t)FPtr
;
764 fprintf(stderr
, "Evaluated to %f\n", FP());
767 // Skip token for error recovery.
772 /// top ::= definition | external | expression | ';'
773 static void MainLoop() {
775 fprintf(stderr
, "ready> ");
777 case tok_eof
: return;
778 case ';': getNextToken(); break; // ignore top-level semicolons.
779 case tok_def
: HandleDefinition(); break;
780 case tok_extern
: HandleExtern(); break;
781 default: HandleTopLevelExpression(); break;
786 //===----------------------------------------------------------------------===//
787 // "Library" functions that can be "extern'd" from user code.
788 //===----------------------------------------------------------------------===//
790 /// putchard - putchar that takes a double and returns 0.
792 double putchard(double X
) {
797 //===----------------------------------------------------------------------===//
799 //===----------------------------------------------------------------------===//
802 InitializeNativeTarget();
803 LLVMContext
&Context
= getGlobalContext();
805 // Install standard binary operators.
806 // 1 is lowest precedence.
807 BinopPrecedence
['<'] = 10;
808 BinopPrecedence
['+'] = 20;
809 BinopPrecedence
['-'] = 20;
810 BinopPrecedence
['*'] = 40; // highest.
812 // Prime the first token.
813 fprintf(stderr
, "ready> ");
816 // Make the module, which holds all the code.
817 TheModule
= new Module("my cool jit", Context
);
819 // Create the JIT. This takes ownership of the module.
821 TheExecutionEngine
= EngineBuilder(TheModule
).setErrorStr(&ErrStr
).create();
822 if (!TheExecutionEngine
) {
823 fprintf(stderr
, "Could not create ExecutionEngine: %s\n", ErrStr
.c_str());
827 FunctionPassManager
OurFPM(TheModule
);
829 // Set up the optimizer pipeline. Start with registering info about how the
830 // target lays out data structures.
831 OurFPM
.add(new TargetData(*TheExecutionEngine
->getTargetData()));
832 // Do simple "peephole" optimizations and bit-twiddling optzns.
833 OurFPM
.add(createInstructionCombiningPass());
834 // Reassociate expressions.
835 OurFPM
.add(createReassociatePass());
836 // Eliminate Common SubExpressions.
837 OurFPM
.add(createGVNPass());
838 // Simplify the control flow graph (deleting unreachable blocks, etc).
839 OurFPM
.add(createCFGSimplificationPass());
841 OurFPM
.doInitialization();
843 // Set the global so the code gen can use this.
846 // Run the main "interpreter loop" now.
851 // Print out all of the generated code.