@@ -34,6 +34,27 @@ export function passReturnStatement(parent: ts.ReturnStatement, ctx: Context, bu
3434 ) ;
3535}
3636
37+ export function passIfStatement ( parent : ts . IfStatement , ctx : Context , builder : llvm . IRBuilder ) {
38+ const positiveBlock = llvm . BasicBlock . create ( ctx . llvmContext , "if.true" ) ;
39+ ctx . scope . currentFunction . addBasicBlock ( positiveBlock ) ;
40+
41+ const negativeBlock = llvm . BasicBlock . create ( ctx . llvmContext , "if.false" ) ;
42+ ctx . scope . currentFunction . addBasicBlock ( negativeBlock ) ;
43+
44+ const next = llvm . BasicBlock . create ( ctx . llvmContext , "if.end" ) ;
45+ ctx . scope . currentFunction . addBasicBlock ( next ) ;
46+
47+ builder . createBr ( positiveBlock ) ;
48+
49+ builder . setInsertionPoint ( positiveBlock ) ;
50+ builder . createBr ( next ) ;
51+
52+ builder . setInsertionPoint ( negativeBlock ) ;
53+ builder . createBr ( next ) ;
54+
55+ builder . setInsertionPoint ( next ) ;
56+ }
57+
3758export function passFunctionDeclaration ( parent : ts . FunctionDeclaration , ctx : Context , builder : llvm . IRBuilder ) {
3859 if ( ! parent . name || ! parent . name . escapedText ) {
3960 throw Error ( 'Function must be declared with name' ) ;
@@ -396,6 +417,9 @@ export function passStatement(stmt: ts.Statement, ctx: Context, builder: llvm.IR
396417 case ts . SyntaxKind . ReturnStatement :
397418 passReturnStatement ( < any > stmt , ctx , builder ) ;
398419 break ;
420+ case ts . SyntaxKind . IfStatement :
421+ passIfStatement ( < any > stmt , ctx , builder ) ;
422+ break ;
399423 default :
400424 throw new UnsupportedError (
401425 stmt ,
@@ -439,11 +463,13 @@ export function generateModuleFromProgram(program: ts.Program): llvm.Module {
439463 program . getTypeChecker ( )
440464 ) ;
441465
442- let mainFnType = llvm . FunctionType . get ( llvm . Type . getVoidTy ( ctx . llvmContext ) , false ) ;
443- let mainFn = llvm . Function . create ( mainFnType , llvm . LinkageTypes . ExternalLinkage , "main" , ctx . llvmModule ) ;
466+ const mainFnType = llvm . FunctionType . get ( llvm . Type . getVoidTy ( ctx . llvmContext ) , false ) ;
467+ const mainFn = llvm . Function . create ( mainFnType , llvm . LinkageTypes . ExternalLinkage , "main" , ctx . llvmModule ) ;
468+
469+ const block = llvm . BasicBlock . create ( ctx . llvmContext , "Entry" , mainFn ) ;
470+ const builder = new llvm . IRBuilder ( block ) ;
444471
445- let block = llvm . BasicBlock . create ( ctx . llvmContext , "Entry" , mainFn ) ;
446- let builder = new llvm . IRBuilder ( block ) ;
472+ ctx . scope . currentFunction = mainFn ;
447473
448474 for ( const sourceFile of program . getSourceFiles ( ) ) {
449475 if ( ! sourceFile . isDeclarationFile ) {
0 commit comments