diff --git a/app.js b/app.js index 12f8b417d..4c3a8bd37 100755 --- a/app.js +++ b/app.js @@ -488,6 +488,9 @@ async function main() { if (compiler.buildenvsetup && compiler.buildenvsetup.id === '') delete compiler.buildenvsetup; + if (compiler.externalparser && compiler.externalparser.id === '') + delete compiler.externalparser; + const compilerInstance = compilerFinder.compileHandler.findCompiler(compiler.lang, compiler.id); if (compilerInstance) { compiler.cachedPossibleArguments = compilerInstance.possibleArguments.possibleArguments; diff --git a/etc/config/c++.amazon.properties b/etc/config/c++.amazon.properties index a45bd20da..57da80c4c 100644 --- a/etc/config/c++.amazon.properties +++ b/etc/config/c++.amazon.properties @@ -7,6 +7,9 @@ needsMulti=false buildenvsetup=ceconan buildenvsetup.host=https://conan.compiler-explorer.com +externalparser=CEAsmParser +externalparser.exe=/usr/local/bin/asm-parser + ############################### # GCC for x86 group.gcc86.compilers=g412:g447:g453:g464:g471:g472:g473:g474:g481:g482:g483:g484:g485:g490:g491:g492:g493:g494:g510:g520:g530:g540:g550:g6:g62:g63:g64:g71:g72:g73:g74:g75:g81:g82:g83:g84:g85:g91:g92:g93:g94:g101:g102:g103:g111:g112:gsnapshot:gcontracts-trunk:gcontract-labels-trunk:gcxx-modules-trunk:gcxx-coroutines-trunk:gcc-embed-trunk:gcc-static-analysis-trunk diff --git a/lib/base-compiler.js b/lib/base-compiler.js index febdab7a0..78b54cc7b 100644 --- a/lib/base-compiler.js +++ b/lib/base-compiler.js @@ -36,6 +36,7 @@ import { CompilerArguments } from './compiler-arguments'; import { ClangParser, GCCParser } from './compilers/argument-parsers'; import { getDemanglerTypeByKey } from './demangler'; import * as exec from './exec'; +import { getExternalParserByKey } from './external-parsers'; import { InstructionSets } from './instructionsets'; import { languages } from './languages'; import { LlvmAstParser } from './llvm-ast'; @@ -106,6 +107,12 @@ export class BaseCompiler { }); } + this.externalparser = null; + if (this.compiler.externalparser && this.compiler.externalparser.id) { + const externalparserclass = getExternalParserByKey(this.compiler.externalparser.id); + this.externalparser = new externalparserclass(this.compiler, this.env, this.exec); + } + if (!this.compiler.instructionSet) { const isets = new InstructionSets(); if (this.buildenvsetup) { @@ -304,7 +311,7 @@ export class BaseCompiler { return output; } - async objdump(outputFilename, result, maxSize, intelAsm, demangle) { + async objdump(outputFilename, result, maxSize, intelAsm, demangle, filters) { outputFilename = this.getObjdumpOutputFilename(outputFilename); if (!await utils.fileExists(outputFilename)) { @@ -316,15 +323,28 @@ export class BaseCompiler { const args = ['-d', outputFilename, '-l', ...objdumper.widthOptions]; if (demangle) args.push('-C'); if (intelAsm) args.push(...objdumper.intelAsmOptions); - const execOptions = {maxOutput: maxSize, customCwd: result.dirPath || path.dirname(outputFilename)}; - const objResult = await this.exec(this.compiler.objdumper, args, execOptions); - if (objResult.code !== 0) { - result.asm = ``; + if (this.externalparser) { + result = await this.externalparser.objdumpAndParseAssembly( + result.dirPath, + args, + filters); + if (result.parsingTime !== undefined) { + result.objdumpTime = parseInt(result.execTime) - parseInt(result.parsingTime); + delete result.execTime; + } } else { - result.objdumpTime = objResult.execTime; - result.asm = this.postProcessObjdumpOutput(objResult.stdout); + const execOptions = {maxOutput: maxSize, customCwd: result.dirPath || path.dirname(outputFilename)}; + const objResult = await this.exec(this.compiler.objdumper, args, execOptions); + + if (objResult.code !== 0) { + result.asm = ``; + } else { + result.objdumpTime = objResult.execTime; + result.asm = this.postProcessObjdumpOutput(objResult.stdout); + } } + return result; } @@ -974,7 +994,7 @@ export class BaseCompiler { const inputFilename = path.join(dirPath, this.compileFilename); await fs.writeFile(inputFilename, source); - if (files) { + if (files && files.length > 0) { filters.dontMaskFilenames = true; await this.writeMultipleFiles(files, dirPath); @@ -991,7 +1011,7 @@ export class BaseCompiler { const inputFilename = path.join(dirPath, 'CMakeLists.txt'); await fs.writeFile(inputFilename, source); - if (files) { + if (files && files.length > 0) { filters.dontMaskFilenames = true; await this.writeMultipleFiles(files, dirPath); @@ -1675,14 +1695,16 @@ export class BaseCompiler { } if (!backendOptions.skipAsm) { - if (result.okToCache) { - const res = this.processAsm(result, filters, options); - result.asm = res.asm; - result.labelDefinitions = res.labelDefinitions; - result.parsingTime = res.parsingTime; - result.filteredCount = res.filteredCount; - } else { - result.asm = [{text: result.asm}]; + if (typeof result.asm === 'string') { + if (result.okToCache) { + const res = this.processAsm(result, filters, options); + result.asm = res.asm; + result.labelDefinitions = res.labelDefinitions; + result.parsingTime = res.parsingTime; + result.filteredCount = res.filteredCount; + } else { + result.asm = [{text: result.asm}]; + } } // TODO rephrase this so we don't need to reassign result = filters.demangle ? await this.postProcessAsm(result, filters) : result; @@ -1903,7 +1925,7 @@ but nothing was dumped. Possible causes are: const maxSize = this.env.ceProps('max-asm-size', 64 * 1024 * 1024); const optPromise = result.hasOptOutput ? this.processOptOutput(result.optPath) : ''; const asmPromise = (filters.binary && this.supportsObjdump()) - ? this.objdump(outputFilename, result, maxSize, filters.intel, filters.demangle) + ? this.objdump(outputFilename, result, maxSize, filters.intel, filters.demangle, filters) : (async () => { if (result.asmSize === undefined) { result.asm = ''; diff --git a/lib/compiler-finder.js b/lib/compiler-finder.js index 9729d8a2c..dafd471a0 100644 --- a/lib/compiler-finder.js +++ b/lib/compiler-finder.js @@ -247,6 +247,12 @@ export class CompilerFinder { return props(`buildenvsetup.${name}`, def); }, }, + externalparser: { + id: props('externalparser', ''), + props: (name, def) => { + return props(`externalparser.${name}`, def); + }, + }, }; if (props('demanglerClassFile') !== undefined) { @@ -381,6 +387,12 @@ export class CompilerFinder { }; } + if (compiler.externalparser) { + compiler.externalparser.props = (propName, def) => { + return this.compilerProps(langId, 'externalparser.' + propName, def); + }; + } + const fullOptions = this.optionsHandler.get(); compiler.tools = _.omit( diff --git a/lib/compilers/pascal-win.js b/lib/compilers/pascal-win.js index cef8a1856..b8dbfc7b0 100644 --- a/lib/compilers/pascal-win.js +++ b/lib/compilers/pascal-win.js @@ -126,7 +126,7 @@ export class PascalWinCompiler extends BaseCompiler { await fs.writeFile(inputFilename, source); - if (files) { + if (files && files.length > 0) { filters.dontMaskFilenames = true; await this.writeMultipleFiles(files, dirPath); diff --git a/lib/compilers/pascal.js b/lib/compilers/pascal.js index f4abd54fe..2da67f83e 100644 --- a/lib/compilers/pascal.js +++ b/lib/compilers/pascal.js @@ -153,7 +153,7 @@ export class FPCCompiler extends BaseCompiler { await fs.writeFile(inputFilename, source); } - if (files) { + if (files && files.length > 0) { filters.dontMaskFilenames = true; await this.writeMultipleFiles(files, dirPath); diff --git a/lib/external-parsers/_all.ts b/lib/external-parsers/_all.ts new file mode 100644 index 000000000..1d804beeb --- /dev/null +++ b/lib/external-parsers/_all.ts @@ -0,0 +1 @@ +export { CEAsmParser } from './ce-asm-parser'; diff --git a/lib/external-parsers/base.ts b/lib/external-parsers/base.ts new file mode 100644 index 000000000..a1a0665e5 --- /dev/null +++ b/lib/external-parsers/base.ts @@ -0,0 +1,97 @@ +import fs from 'fs'; +import path from 'path'; + +import { TypicalExecutionFunc, UnprocessedExecResult } from '../../types/execution/execution.interfaces'; +import { IParseFilters } from '../../types/features/filters.interfaces'; +import { maskRootdir } from '../utils'; + +import { IExternalParser, IParsedAsmResult } from './external-parser.interface'; + +const starterScriptName = 'dump-and-parse.sh'; + +export class ExternalParserBase implements IExternalParser { + private objdumperPath: string; + private parserPath: string; + private execFunc: TypicalExecutionFunc; + private compilerInfo; + private envInfo; + + constructor(compilerInfo, envInfo, execFunc: TypicalExecutionFunc) { + this.compilerInfo = compilerInfo; + this.envInfo = envInfo; + this.objdumperPath = compilerInfo.objdumper; + this.parserPath = compilerInfo.externalparser.props('exe', ''); + this.execFunc = execFunc; + } + + private getParserArguments(filters: IParseFilters, fromStdin: boolean): string[] { + const parameters = []; + + if (fromStdin) parameters.push('-stdin'); + if (filters.binary) parameters.push('-binary'); + if (filters.labels) parameters.push('-unused_labels'); + if (filters.directives) parameters.push('-directives'); + if (filters.commentOnly) parameters.push('-comment_only'); + if (filters.trim) parameters.push('-whitespace'); + if (filters.libraryCode) parameters.push('-library_functions'); + if (filters.dontMaskFilenames) parameters.push('-dont_mask_filenames'); + + return parameters; + } + + private getObjdumpStarterScriptContent(filters: IParseFilters) { + const parserArgs = this.getParserArguments(filters, true); + + return '#!/bin/bash\n' + + `OBJDUMP=${this.objdumperPath}\n` + + `ASMPARSER=${this.parserPath}\n` + + `$OBJDUMP "$@" | $ASMPARSER ${parserArgs.join(' ')}\n`; + } + + private async writeStarterScriptObjdump(buildfolder: string, filters: IParseFilters): Promise { + const scriptFilepath = path.join(buildfolder, starterScriptName); + + return new Promise((resolve) => { + fs.writeFile(scriptFilepath, + this.getObjdumpStarterScriptContent(filters), { + encoding: 'utf8', + mode: 0o777, + }, () => { + resolve(maskRootdir(scriptFilepath)); + }); + }); + } + + private parseAsmExecResult(execResult: UnprocessedExecResult): IParsedAsmResult { + const result = Object.assign({}, execResult, JSON.parse(execResult.stdout)); + delete result.stdout; + delete result.stderr; + return result; + } + + public async objdumpAndParseAssembly(buildfolder: string, objdumpArgs: string[], + filters: IParseFilters): Promise { + objdumpArgs = objdumpArgs.map((v) => { + return maskRootdir(v); + }); + await this.writeStarterScriptObjdump(buildfolder, filters); + const execOptions = { + env: this.envInfo.getEnv(this.compilerInfo.needsMulti), + customCwd: buildfolder, + }; + const execResult = await this.execFunc(`./${starterScriptName}`, objdumpArgs, execOptions); + return this.parseAsmExecResult(execResult); + } + + public async parseAssembly(filepath: string, filters: IParseFilters): Promise { + const execOptions = { + env: this.envInfo.getEnv(this.compilerInfo.needsMulti), + }; + + const parserArgs = this.getParserArguments(filters, false); + parserArgs.push(filepath); + + const execResult = await this.execFunc(this.parserPath, parserArgs, execOptions); + return this.parseAsmExecResult(execResult); + } +} diff --git a/lib/external-parsers/ce-asm-parser.ts b/lib/external-parsers/ce-asm-parser.ts new file mode 100644 index 000000000..75a79442a --- /dev/null +++ b/lib/external-parsers/ce-asm-parser.ts @@ -0,0 +1,5 @@ +import { ExternalParserBase } from './base'; + +export class CEAsmParser extends ExternalParserBase { + static get key() { return 'CEAsmParser'; } +} diff --git a/lib/external-parsers/external-parser.interface.ts b/lib/external-parsers/external-parser.interface.ts new file mode 100644 index 000000000..c55c4256e --- /dev/null +++ b/lib/external-parsers/external-parser.interface.ts @@ -0,0 +1,15 @@ +import { IParseFilters } from '../../types/features/filters.interfaces'; +import { IResultLineTag } from '../../types/resultline/resultline.interfaces'; + +export interface IParsedAsmResult { + asm: IResultLineTag[]; + labelDefinitions: Map; + parsingTime: string; + filteredCount: number; +} + +export interface IExternalParser { + objdumpAndParseAssembly(buildfolder: string, objdumpArgs: string[], + filters: IParseFilters): Promise; + parseAssembly(filepath: string, filters: IParseFilters): Promise; +} diff --git a/lib/external-parsers/index.ts b/lib/external-parsers/index.ts new file mode 100644 index 000000000..255dcd985 --- /dev/null +++ b/lib/external-parsers/index.ts @@ -0,0 +1,7 @@ +import { makeKeyedTypeGetter } from '../keyed-type'; + +import * as all from './_all'; + +export * from './_all'; + +export const getExternalParserByKey = makeKeyedTypeGetter('externalParser', all); diff --git a/lib/utils.ts b/lib/utils.ts index fea13bc6e..b050300d9 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -30,17 +30,7 @@ import fs from 'fs-extra'; import quote from 'shell-quote'; import _ from 'underscore'; -interface IResultLineTag { - line?: number; - column?: number; - file?: string; - text: string; -} - -interface IResultLine { - text: string; - tag?: IResultLineTag; -} +import { IResultLine } from '../types/resultline/resultline.interfaces'; const tabsRe = /\t/g; const lineRe = /\r?\n/; diff --git a/types/execution/execution.interfaces.ts b/types/execution/execution.interfaces.ts new file mode 100644 index 000000000..424e96ee8 --- /dev/null +++ b/types/execution/execution.interfaces.ts @@ -0,0 +1,12 @@ +export type FilenameTransformFunc = (filename: string) => string; + +export interface UnprocessedExecResult { + code: number; + okToCache: boolean; + filenameTransform: FilenameTransformFunc; + stdout: string; + stderr: string; + execTime: string; +} + +export type TypicalExecutionFunc = (executable: string, args: string[], execOptions: object) => IUnprocessedExecResult; diff --git a/types/features/filters.interfaces.ts b/types/features/filters.interfaces.ts new file mode 100644 index 000000000..cc1696fec --- /dev/null +++ b/types/features/filters.interfaces.ts @@ -0,0 +1,13 @@ +export interface IParseFilters { + binary: boolean; + execute: boolean; + demangle: boolean; + intel: boolean; + commentOnly: boolean; + directives: boolean; + labels: boolean; + optOutput: boolean; + libraryCode: boolean; + trim: boolean; + dontMaskFilenames?: boolean; +} diff --git a/types/resultline/resultline.interfaces.ts b/types/resultline/resultline.interfaces.ts new file mode 100644 index 000000000..c46e7944e --- /dev/null +++ b/types/resultline/resultline.interfaces.ts @@ -0,0 +1,11 @@ +export interface IResultLineTag { + line?: number; + column?: number; + file?: string; + text: string; +} + +export interface IResultLine { + text: string; + tag?: IResultLineTag; +}