Skip to content

Commit

Permalink
Feature(cli): Introduce --debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 1, 2019
1 parent bab4576 commit 6a739f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
88 changes: 60 additions & 28 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import {executeLLCSync, executeOptSync} from "./utils";

interface CommandLineArguments {
args: string[];
printIR?: boolean;
outputFile?: string;
optimizationLevel?: string;
debug: boolean;
printIR: boolean;
outputFile: string;
optimizationLevel: string;
}

function parseCommandLine(): CommandLineArguments {
cli
.version('next')
.option('--debug', 'Show all debug information', false)
.option('-ir, --printIR', 'Print IR', false)
.option('-f, --outputFile <n>', 'Name of the executable file', 'main')
.option('-o, --optimizationLevel <n>', 'Optimization level', 3)
Expand Down Expand Up @@ -74,31 +76,61 @@ try {

const optimizationLevel = `-O${cliOptions.optimizationLevel}`;

executeOptSync([
optimizationLevel,
path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.bc')
]);

executeLLCSync([
optimizationLevel,
// Fully relocatable, position independent code
'-relocation-model=pic',
'-filetype=obj', path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.o'),
]);

execFileSync("c++", [
optimizationLevel,
path.join(outputPath, 'main.o'),
RUNTIME_ARCHIVE_FILE,
'-o', path.join(outputPath, cliOptions.outputFile),
'-lstdc++',
'-std=c++11',
'-Werror',
'-pthread',
'-v',
]);
if (cliOptions.debug) {
ts.sys.write('Executing llvm-opt');
}

{
const output = executeOptSync([
optimizationLevel,
path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.bc')
]);

if (cliOptions.debug) {
ts.sys.write(output.toString());
}
}

if (cliOptions.debug) {
ts.sys.write('Executing llvm-llc');
}

{
const output = executeLLCSync([
optimizationLevel,
// Fully relocatable, position independent code
'-relocation-model=pic',
'-filetype=obj', path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.o'),
]);

if (cliOptions.debug) {
ts.sys.write(output.toString());
}
}

if (cliOptions.debug) {
ts.sys.write('Executing c++ compiler');
}

{
const output = execFileSync("c++", [
optimizationLevel,
path.join(outputPath, 'main.o'),
RUNTIME_ARCHIVE_FILE,
'-o', path.join(outputPath, cliOptions.outputFile),
'-lstdc++',
'-std=c++11',
'-Werror',
'-pthread',
'-v',
]);

if (cliOptions.debug) {
ts.sys.write(output.toString());
}
}
} catch (e) {
if (e instanceof UnsupportedError) {
ts.sys.write(ts.formatDiagnosticsWithColorAndContext([e.toDiagnostic()], DiagnosticHostInstance));
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ function getLLVMBinDirectory(): string {
return llvmBinDir;
}

export function executeLLCSync(options: Array<any>) {
child_process.execFileSync(
export function executeLLCSync(options: Array<any>): Buffer|string {
return child_process.execFileSync(
path.join(getLLVMBinDirectory(), 'llc'),
options
)
}

export function executeOptSync(options: Array<any>) {
child_process.execFileSync(
export function executeOptSync(options: Array<any>): Buffer|string {
return child_process.execFileSync(
path.join(getLLVMBinDirectory(), 'opt'),
options
)
Expand Down
1 change: 1 addition & 0 deletions tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function main() {

const compileLog = execFileSync(path.join(__dirname, '..', 'bin', 'ssc'), [
'--printIR',
'--debug',
testFile
]);

Expand Down

0 comments on commit 6a739f5

Please sign in to comment.