@@ -7,16 +7,20 @@ import * as cli from "commander";
77import { initializeLLVM , generateModuleFromProgram } from './backend/llvm' ;
88import DiagnosticHostInstance from "./diagnostic.host" ;
99import UnsupportedError from "./backend/error/unsupported.error" ;
10+ import { existsSync , mkdirSync , unlinkSync } from "fs" ;
11+ import { execFileSync } from "child_process" ;
1012
1113interface CommandLineArguments {
1214 args : string [ ] ;
1315 printIR ?: boolean ;
16+ outputFile ?: boolean ;
1417}
1518
1619function parseCommandLine ( ) : CommandLineArguments {
1720 cli
1821 . version ( 'next' )
1922 . option ( '-ir, --printIR' , 'Print IR' )
23+ . option ( '-o, --outputFile' , 'Name of the executable file' )
2024 . parse ( process . argv ) ;
2125
2226 return cli as any as CommandLineArguments ;
5559 if ( cliOptions . printIR ) {
5660 console . log ( llvmModule . print ( ) ) ;
5761 }
62+
63+ const outputPath = path . join ( process . cwd ( ) , 'output' ) ;
64+
65+ if ( ! existsSync ( outputPath ) ) {
66+ mkdirSync ( outputPath ) ;
67+ }
68+
69+ try {
70+ llvm . writeBitcodeToFile ( llvmModule , path . join ( outputPath , 'main.ll' ) ) ;
71+
72+ const optimizationLevel = "-O3" ;
73+
74+ execFileSync ( 'llc' , [
75+ optimizationLevel ,
76+ '-filetype=obj' , path . join ( outputPath , 'main.ll' ) ,
77+ '-o' , path . join ( outputPath , 'main.o' )
78+ ] ) ;
79+ execFileSync ( "cc" , [
80+ optimizationLevel ,
81+ path . join ( outputPath , 'main.o' ) ,
82+ path . join ( path . dirname ( __filename ) , '..' , 'packages' , 'runtime' , 'libhlvm-runtime.a' ) ,
83+ '-o' , path . join ( outputPath , 'main' ) ,
84+ '-lstdc++' ,
85+ '-std=c++11' ,
86+ '-Werror' ,
87+ '-v' ,
88+ ] ) ;
89+ } finally {
90+ // unlinkSync(outputPath);
91+ }
5892} catch ( e ) {
5993 if ( e instanceof UnsupportedError ) {
6094 console . log ( ts . formatDiagnostic ( e . toDiagnostic ( ) , DiagnosticHostInstance ) ) ;
6498
6599 throw e ;
66100}
101+
0 commit comments