aboutsummaryrefslogtreecommitdiff
path: root/lib/compilers/pascal.js
diff options
context:
space:
mode:
authorRubén <RabsRincon@users.noreply.github.com>2018-02-07 21:51:06 +0100
committerGitHub <noreply@github.com>2018-02-07 21:51:06 +0100
commit5daeceb2831f5768f5776fff84e79abb4fa37637 (patch)
tree5b0b2b7399fd6b0b0ea98262f17b67ef9f180d5c /lib/compilers/pascal.js
parentef89c6ab16a017aa5c131aa2cf044f635d3bde4e (diff)
parenta414d8addb0fc4f8923d18c8224feeaa57632339 (diff)
downloadcompiler-explorer-5daeceb2831f5768f5776fff84e79abb4fa37637.tar.gz
compiler-explorer-5daeceb2831f5768f5776fff84e79abb4fa37637.zip
Merge pull request #792 from mattgodbolt/fixedes6
ES6fication of lib files
Diffstat (limited to 'lib/compilers/pascal.js')
-rw-r--r--lib/compilers/pascal.js219
1 files changed, 105 insertions, 114 deletions
diff --git a/lib/compilers/pascal.js b/lib/compilers/pascal.js
index 3fa0d3821..9fe60251b 100644
--- a/lib/compilers/pascal.js
+++ b/lib/compilers/pascal.js
@@ -23,66 +23,58 @@
// POSSIBILITY OF SUCH DAMAGE.
"use strict";
-var Compile = require('../base-compiler'),
+const BaseCompiler = require('../base-compiler'),
PascalDemangler = require('../pascal-support').demangler,
utils = require('../utils'),
+ _ = require('underscore-node'),
fs = require("fs"),
- path = require("path");
-
-function compileFPC(info, env) {
- var demangler = new PascalDemangler();
- var compiler = new Compile(info, env);
- compiler.compileFilename = "output.pas";
- compiler.supportsOptOutput = false;
-
- var originalExecBinary = compiler.execBinary;
- var currentlyActiveFilters = {};
-
- compiler.postProcessAsm = function (result) {
+ path = require("path"),
+ argumentParsers = require("./argument-parsers");
+
+class FPCCompiler extends BaseCompiler {
+ constructor(info, env) {
+ super(info, env);
+ this.demangler = new PascalDemangler();
+ this.compileFilename = 'output.pas';
+ this.supportsOptOutput = false;
+ }
+
+ postProcessAsm(result, filters) {
if (!result.okToCache) return result;
- if (currentlyActiveFilters.binary) {
- preProcessAsm(result.asm);
+ if (filters.binary) {
+ for (let j = 0; j < result.asm.length; ++j) {
+ this.demangler.addDemangleToCache(result.asm[j].text);
+ }
}
- for (var j = 0; j < result.asm.length; ++j)
- result.asm[j].text = demangler.demangleIfNeeded(result.asm[j].text);
+ for (let j = 0; j < result.asm.length; ++j)
+ result.asm[j].text = this.demangler.demangleIfNeeded(result.asm[j].text);
return result;
- };
+ }
- compiler.optionsForFilter = function (filters, outputFilename, userOptions) {
- var options = ['-g'];
+ optionsForFilter(filters, outputFilename, userOptions) {
+ let options = ['-g', '-al'];
if (this.compiler.intelAsm && filters.intel && !filters.binary) {
options = options.concat(this.compiler.intelAsm.split(" "));
}
- currentlyActiveFilters = filters;
- filters.preProcessLines = preProcessLines;
+ filters.preProcessLines = _.bind(this.preProcessLines, this);
return options;
- };
+ }
- compiler.getOutputFilename = function (dirPath, outputFilebase) {
- return path.join(dirPath, path.basename(this.compileFilename, this.lang.extensions[0]) + ".s");
- };
+ getOutputFilename(dirPath, outputFilebase) {
+ return path.join(dirPath, `${path.basename(this.compileFilename, this.lang.extensions[0])}.s`);
+ }
- var saveDummyProjectFile = function (filename) {
- const unitName = path.basename(compiler.compileFilename, compiler.lang.extensions[0]);
-
- fs.writeFileSync(filename,
- "program prog; " +
- "uses " + unitName + " in '" + compiler.compileFilename + "'; " +
- "begin " +
- "end.", function() {});
- };
-
- var preProcessBinaryAsm = function (input) {
- var relevantAsmStartsAt = input.indexOf("<OUTPUT");
- if (relevantAsmStartsAt != -1) {
- var lastLinefeedBeforeStart = input.lastIndexOf("\n", relevantAsmStartsAt);
- if (lastLinefeedBeforeStart != -1) {
+ static preProcessBinaryAsm(input) {
+ const relevantAsmStartsAt = input.indexOf("<OUTPUT");
+ if (relevantAsmStartsAt !== -1) {
+ const lastLinefeedBeforeStart = input.lastIndexOf("\n", relevantAsmStartsAt);
+ if (lastLinefeedBeforeStart !== -1) {
input =
input.substr(0, input.indexOf("00000000004")) + "\n" +
input.substr(lastLinefeedBeforeStart + 1);
@@ -92,22 +84,80 @@ function compileFPC(info, env) {
input.substr(relevantAsmStartsAt);
}
}
-
return input;
- };
+ }
+
+ objdump(outputFilename, result, maxSize, intelAsm, demangle) {
+ outputFilename = path.join(path.dirname(outputFilename), "prog");
+ let args = ["-d", outputFilename, "-l", "--insn-width=16"];
+ if (demangle) args = args.concat(["-C"]);
+ if (intelAsm) args = args.concat(["-M", "intel"]);
+ return this.exec(this.compiler.objdumper, args, {maxOutput: maxSize}).then(objResult => {
+ if (objResult.code !== 0) {
+ result.asm = "<No output: objdump returned " + objResult.code + ">";
+ } else {
+ result.asm = FPCCompiler.preProcessBinaryAsm(objResult.stdout);
+ }
+ return result;
+ });
+ }
+
+ saveDummyProjectFile(filename) {
+ const unitName = path.basename(this.compileFilename, this.lang.extensions[0]);
+
+ fs.writeFileSync(filename,
+ "program prog; " +
+ "uses " + unitName + " in '" + this.compileFilename + "'; " +
+ "begin " +
+ "end.", () => {
+ });
+ }
+
+ runCompiler(compiler, options, inputFilename, execOptions) {
+ if (!execOptions) {
+ execOptions = this.getDefaultExecOptions();
+ }
+
+ const tempPath = path.dirname(inputFilename);
+ const projectFile = path.join(tempPath, "prog.dpr");
- var getExtraAsmHint = function (asm) {
+ this.saveDummyProjectFile(projectFile);
+
+ options.pop();
+ options.push('-FE' + tempPath);
+ options.push('-B');
+ options.push(projectFile);
+
+ return this.exec(compiler, options, execOptions).then(result => {
+ result.inputFilename = inputFilename;
+ result.stdout = utils.parseOutput(result.stdout, inputFilename);
+ result.stderr = utils.parseOutput(result.stderr, inputFilename);
+ return result;
+ });
+ }
+
+ execBinary(executable, result, maxSize) {
+ executable = path.join(path.dirname(executable), "prog");
+
+ super.execBinary(executable, result, maxSize);
+ }
+
+ getArgumentParser() {
+ return argumentParsers.Base;
+ }
+
+ getExtraAsmHint(asm) {
if (asm.startsWith("# [")) {
- var bracketEndPos = asm.indexOf("]", 3);
- var valueInBrackets = asm.substr(3, bracketEndPos - 3);
- var colonPos = valueInBrackets.indexOf(":");
+ const bracketEndPos = asm.indexOf("]", 3);
+ let valueInBrackets = asm.substr(3, bracketEndPos - 3);
+ const colonPos = valueInBrackets.indexOf(":");
if (colonPos != -1) {
valueInBrackets = valueInBrackets.substr(0, colonPos - 1);
}
if (!isNaN(valueInBrackets)) {
return " .loc 1 " + valueInBrackets + " 0";
- } else if (valueInBrackets.includes(compiler.compileFilename)) {
+ } else if (valueInBrackets.includes(this.compileFilename)) {
return " .file 1 \"<stdin>\"";
} else {
return false;
@@ -117,84 +167,25 @@ function compileFPC(info, env) {
} else {
return false;
}
- };
+ }
- var preProcessLines = function(asmLines) {
- var i = 0;
+ preProcessLines(asmLines) {
+ let i = 0;
while (i < asmLines.length) {
- var extraHint = getExtraAsmHint(asmLines[i]);
+ const extraHint = this.getExtraAsmHint(asmLines[i]);
if (extraHint) {
i++;
asmLines.splice(i, 0, extraHint);
} else {
- demangler.addDemangleToCache(asmLines[i]);
+ this.demangler.addDemangleToCache(asmLines[i]);
}
i++;
}
return asmLines;
- };
-
- var preProcessAsm = function(asm) {
- for (var j = 0; j < asm.length; ++j) demangler.addDemangleToCache(asm[j].text);
- };
-
- compiler.objdump = function (outputFilename, result, maxSize, intelAsm, demangle) {
- outputFilename = path.join(path.dirname(outputFilename), "prog");
-
- var args = ["-d", outputFilename, "-l", "--insn-width=16"];
- if (demangle) args = args.concat(["-C"]);
- if (intelAsm) args = args.concat(["-M", "intel"]);
- return this.exec(this.compiler.objdumper, args, {maxOutput: maxSize})
- .then(function (objResult) {
- if (objResult.code !== 0) {
- result.asm = "<No output: objdump returned " + objResult.code + ">";
- } else {
- result.asm = preProcessBinaryAsm(objResult.stdout);
- }
-
- return result;
- });
- };
-
- compiler.runCompiler = function (compiler, options, inputFilename, execOptions) {
- if (!execOptions) {
- execOptions = this.getDefaultExecOptions();
- }
-
- var tempPath = path.dirname(inputFilename);
- var projectFile = path.join(tempPath, "prog.dpr");
-
- saveDummyProjectFile(projectFile);
-
- options.pop();
- options.push('-FE' + tempPath);
- options.push('-B');
- options.push(projectFile);
-
- return this.exec(compiler, options, execOptions).then(function (result) {
- result.inputFilename = inputFilename;
- result.stdout = utils.parseOutput(result.stdout, inputFilename);
- result.stderr = utils.parseOutput(result.stderr, inputFilename);
- return result;
- });
- };
-
- compiler.execBinary = function (executable, result, maxSize) {
- executable = path.join(path.dirname(executable), "prog");
-
- originalExecBinary(executable, result, maxSize);
- };
-
- compiler.getArgumentParser = () => (compiler) => compiler;
-
- if (info.unitTestMode) {
- compiler.initialise();
- return compiler;
- } else
- return compiler.initialise();
+ }
}
-module.exports = compileFPC;
+module.exports = FPCCompiler;