diff options
Diffstat (limited to 'lib/compilers/typescript-native.ts')
-rw-r--r-- | lib/compilers/typescript-native.ts | 126 |
1 files changed, 61 insertions, 65 deletions
diff --git a/lib/compilers/typescript-native.ts b/lib/compilers/typescript-native.ts index 32017f66b..11790ce50 100644 --- a/lib/compilers/typescript-native.ts +++ b/lib/compilers/typescript-native.ts @@ -22,10 +22,14 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -import type {CompilationResult, ExecutionOptions} from '../../types/compilation/compilation.interfaces.js'; +import Semver from 'semver'; +import path from 'path'; + import type {PreliminaryCompilerInfo} from '../../types/compiler.interfaces.js'; import type {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js'; +import {LLVMIrBackendOptions} from '../../types/compilation/ir.interfaces.js'; import {BaseCompiler} from '../base-compiler.js'; +import {asSafeVer} from '../utils.js'; import {TypeScriptNativeParser} from './argument-parsers.js'; @@ -36,98 +40,90 @@ export class TypeScriptNativeCompiler extends BaseCompiler { tscJit: string; tscSharedLib: string; + tscNewOutput: boolean; + tscAsmOutput: boolean; constructor(compilerInfo: PreliminaryCompilerInfo, env) { super(compilerInfo, env); - this.compiler.supportsIntel = false; - this.compiler.supportsIrView = true; - - this.tscJit = this.compilerProps<string>(`compiler.${this.compiler.id}.exe`); + this.tscJit = this.compiler.exe; this.tscSharedLib = this.compilerProps<string>(`compiler.${this.compiler.id}.sharedlibs`); + this.tscNewOutput = Semver.gt(asSafeVer(this.compiler.semver || '0.0.0'), '0.0.32', true); + this.tscAsmOutput = Semver.gt(asSafeVer(this.compiler.semver || '0.0.0'), '0.0.34', true); + + this.compiler.irArg = ['--emit=llvm']; + this.compiler.supportsIntel = this.tscAsmOutput; + this.compiler.supportsIrView = true; } override getSharedLibraryPathsAsArguments() { return []; } - override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string) { - return [this.filename(outputFilename)]; + override optionsForFilter( + filters: ParseFiltersAndOutputOptions, + outputFilename: string, + userOptions?: string[], + ): string[] { + return []; } - override async handleInterpreting(key, executeParameters) { - executeParameters.args = [ - '--emit=jit', - this.tscSharedLib ? '--shared-libs=' + this.tscSharedLib : '-nogc', - ...executeParameters.args, - ]; - - return await super.handleInterpreting(key, executeParameters); - } + override optionsForBackend(backendOptions: Record<string, any>, outputFilename: string): string[] { + const addOpts: string[] = []; - override async runCompiler( - compiler: string, - options: string[], - inputFilename: string, - execOptions: ExecutionOptions, - ): Promise<CompilationResult> { - // These options make Clang produce an IR - const newOptions = ['--emit=mlir-llvm', inputFilename]; + addOpts.push(this.tscAsmOutput ? '--emit=asm' : '--emit=mlir'); if (!this.tscSharedLib) { - newOptions.push('-nogc'); + addOpts.push('-nogc'); } - const output = await this.runCompilerRawOutput( - this.tscJit, - newOptions, - this.filename(inputFilename), - execOptions, - ); - if (output.code !== 0) { - return { - code: output.code, - timedOut: false, - stdout: [], - stderr: [ - { - text: 'Failed to run compiler to get MLIR code', - }, - ], - }; + if (this.tscNewOutput) { + addOpts.push('-o=' + this.filename(outputFilename)); } - return {code: 0, timedOut: false, stdout: [], stderr: []}; + return addOpts; } - override async generateIR(inputFilename: string, options: string[], filters: ParseFiltersAndOutputOptions) { - // These options make Clang produce an IR - const newOptions = ['--emit=llvm', inputFilename]; + override getIrOutputFilename(inputFilename: string, filters: ParseFiltersAndOutputOptions): string { + const outputFilename = this.getOutputFilename(path.dirname(inputFilename), this.outputFilebase); + // As per #4054, if we are asked for binary mode, the output will be in the .s file, no .ll will be emited + if (!filters.binary) { + return outputFilename.replace('.s', '.ll'); + } + return outputFilename; + } - if (!this.tscSharedLib) { - newOptions.push('-nogc'); + override async generateIR( + inputFilename: string, + options: string[], + irOptions: LLVMIrBackendOptions, + produceCfg: boolean, + filters: ParseFiltersAndOutputOptions, + ) { + const newOptions = [...options.filter(e => !e.startsWith('--emit=') && !e.startsWith('-o='))]; + if (this.tscNewOutput) { + newOptions.push('-o=' + this.getIrOutputFilename(inputFilename, filters)); } - const execOptions = this.getDefaultExecOptions(); - // TODO: maybe this isn't needed? - execOptions.maxOutput = 1024 * 1024 * 1024; - - const output = await this.runCompilerRawOutput( - this.tscJit, - newOptions, - this.filename(inputFilename), - execOptions, - ); - if (output.code !== 0) { - return [{text: 'Failed to run compiler to get IR code'}]; + return await super.generateIR(inputFilename, newOptions, irOptions, produceCfg, filters); + } + + override async processIrOutput(output, irOptions: LLVMIrBackendOptions, filters: ParseFiltersAndOutputOptions) { + if (this.tscNewOutput) { + return await super.processIrOutput(output, irOptions, filters); } - filters.commentOnly = false; - filters.libraryCode = true; - filters.directives = true; + return this.llvmIr.process(output.stderr.map(l => l.text).join('\n'), irOptions); + } + + override async handleInterpreting(key, executeParameters) { + executeParameters.args = [ + '--emit=jit', + this.tscSharedLib ? '--shared-libs=' + this.tscSharedLib : '-nogc', + ...executeParameters.args, + ]; - const ir = await this.llvmIr.process(output.stderr, filters); - return ir.asm; + return await super.handleInterpreting(key, executeParameters); } override isCfgCompiler() { |