diff --git a/lib/base-compiler.js b/lib/base-compiler.js index ed1ae18db..307f819f4 100644 --- a/lib/base-compiler.js +++ b/lib/base-compiler.js @@ -34,7 +34,8 @@ const temp = require('temp'), compilerOptInfo = require("compiler-opt-info"), argumentParsers = require("./compilers/argument-parsers"), cfg = require('./cfg'), - languages = require('./languages').list; + languages = require('./languages').list, + Demangler = require('./demangler').Demangler; class BaseCompiler { constructor(compiler, env) { @@ -365,15 +366,11 @@ class BaseCompiler { postProcessAsm(result) { if (!result.okToCache) return result; - const demangler = this.compiler.demangler; - if (!demangler) return result; - return this.exec(demangler, [], {input: _.pluck(result.asm, 'text').join("\n")}) - .then((demangleResult) => { - const lines = utils.splitLines(demangleResult.stdout); - for (let i = 0; i < result.asm.length; ++i) - result.asm[i].text = lines[i]; - return result; - }); + const demanglerExe = this.compiler.demangler; + if (!demanglerExe) return result; + + const demangler = new Demangler(demanglerExe); + return demangler.Process(result); } processOptOutput(hasOptOutput, optPath) { diff --git a/lib/demangler.js b/lib/demangler.js index e9e1a1c37..de0d8e5fa 100644 --- a/lib/demangler.js +++ b/lib/demangler.js @@ -30,6 +30,15 @@ const exec = require('../lib/exec'), logger = require('../lib/logger').logger; +const LabelMetadata = [ + {'ident': 'C1', 'description': 'complete object constructor'}, + {'ident': 'C2', 'description': 'base object constructor'}, + {'ident': 'C3', 'description': 'complete object allocating constructor'}, + {'ident': 'D0', 'description': 'deleting destructor'}, + {'ident': 'D1', 'description': 'complete object destructor'}, + {'ident': 'D2', 'description': 'base object destructor'} +]; + class Demangler { /** * @@ -40,6 +49,8 @@ class Demangler { this.demanglerExe = demanglerExe; this.symbolstore = symbolstore; this.result = {}; + this.input = []; + this.includeMetadata = true; } CollectLabels() { @@ -54,23 +65,48 @@ class Demangler { } } - ProcessNew(result) { - - // for (var j = 0; j < result.asm.length; ++j) - // result.asm[j].text = demangleIfNeeded(result.asm[j].text); - + GetInput() { + this.input = this.symbolstore.ListSymbols(); + return this.input.join("\n"); } - GetInput() { - return _.pluck(this.result.asm, 'text').join("\n"); + GetMetadata(symbol) { + let metadata = []; - //return this.symbolstore.ListSymbols().join("\n"); + for (let i = 0; i < LabelMetadata.length; ++i) { + if (symbol.includes(LabelMetadata[i].ident)) { + metadata.push(LabelMetadata[i]); + } + } + + return metadata; + } + + AddTranslation(symbol, translation) { + let metadataStr = ""; + + if (this.includeMetadata) { + const metadata = this.GetMetadata(symbol); + metadataStr = metadata.map((meta) => " [" + meta.description + "]").join(); + } + + this.symbolstore.Add(symbol, translation + metadataStr); } ProcessOutput(output) { const lines = utils.splitLines(output.stdout); - for (let i = 0; i < this.result.asm.length; ++i) - this.result.asm[i].text = lines[i]; + for (let i = 0; i < lines.length; ++i) + this.AddTranslation(this.input[i], lines[i]); + + const translations = this.symbolstore.ListTranslations(); + for (let i = 0; i < this.result.asm.length; ++i) { + let line = this.result.asm[i].text; + for (let j = 0; j < translations.length; ++j) { + line = line.replace(translations[j][0], translations[j][1]); + } + this.result.asm[i].text = line; + } + return this.result; } @@ -83,8 +119,6 @@ class Demangler { } const options = {input: this.GetInput()}; - if (options.input.length == 0) return this.result; - return exec.execute( this.demanglerExe, [], diff --git a/test/demangler-tests.js b/test/demangler-tests.js index 44b00d5ff..4210018e0 100644 --- a/test/demangler-tests.js +++ b/test/demangler-tests.js @@ -51,7 +51,7 @@ describe('Basic demangling', function () { it('One label and some asm', function () { const result = {}; result.asm = [ - {"text": "_Z6squarei:"}, + {"text": "__Z6squarei:"}, {"text": " ret"} ]; @@ -64,4 +64,45 @@ describe('Basic demangling', function () { }) ]); }); + + it('One label and use of a label', function () { + const result = {}; + result.asm = [ + {"text": "__Z6squarei:"}, + {"text": " mov eax, $__Z6squarei"} + ]; + + const demangler = new Demangler("c++filt"); + + return Promise.all([ + demangler.Process(result).then((output) => { + output.asm[0].text.should.equal("square(int):"); + output.asm[1].text.should.equal(" mov eax, $square(int)"); + }) + ]); + }); + + it('Two destructors', function () { + const result = {}; + result.asm = [ + {"text": "__ZN6NormalD0Ev:"}, + {"text": " callq __ZdlPv"}, + {"text": "__Z7caller1v:"}, + {"text": " rep ret"}, + {"text": "__Z7caller2P6Normal:"}, + {"text": " cmp rax, OFFSET FLAT:_ZN6NormalD0Ev"}, + {"text": " jmp __ZdlPvm"}, + {"text": "__ZN6NormalD2Ev:"}, + {"text": " rep ret"} + ]; + + const demangler = new Demangler("c++filt"); + + return Promise.all([ + demangler.Process(result).then((output) => { + output.asm[0].text.should.equal("Normal::~Normal() [deleting destructor]:"); + output.asm[1].text.should.equal(" callq __ZdlPv"); // todo: should be operator delete(void*, unsigned long) + }) + ]); + }); });