From 3f695c9cc10d69544440776a1dbb69866434f18d Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Fri, 11 Feb 2022 16:19:16 +0100 Subject: [PATCH] Add Dart support (#3352) --- .github/labeler.yml | 3 + etc/config/dart.amazon.properties | 14 +++ etc/config/dart.defaults.properties | 4 + examples/dart/default.dart | 8 ++ lib/asm-parser-dart.js | 165 ++++++++++++++++++++++++++++ lib/compilers/_all.js | 1 + lib/compilers/dart.js | 69 ++++++++++++ lib/languages.js | 6 + webpack.config.esm.js | 2 +- 9 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 etc/config/dart.amazon.properties create mode 100644 etc/config/dart.defaults.properties create mode 100644 examples/dart/default.dart create mode 100644 lib/asm-parser-dart.js create mode 100644 lib/compilers/dart.js diff --git a/.github/labeler.yml b/.github/labeler.yml index 21042450d..ec9f38368 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -48,6 +48,9 @@ lang-dotnet: - etc/config/csharp.*.properties - etc/config/fsharp.*.properties - etc/config/vb.*.properties +lang-dart: + - lib/compilers/dart.js + - etc/config/dart.*.properties lang-fortran: - lib/compilers/fortran.js - etc/config/fortran.*.properties diff --git a/etc/config/dart.amazon.properties b/etc/config/dart.amazon.properties new file mode 100644 index 000000000..300d73c5f --- /dev/null +++ b/etc/config/dart.amazon.properties @@ -0,0 +1,14 @@ +compilers=&dart +defaultCompiler=dart2144 +supportsBinary=true +supportsExecute=true +compilerType=dart +objdumper=/opt/compiler-explorer/gcc-11.1.0/bin/objdump + +group.dart.compilers=dart2144 +group.dart.isSemVer=true +group.dart.baseName=Dart +group.dart.groupName=Dart + +compiler.dart2144.semver=2.14.4 +compiler.dart2144.exe=/opt/compiler-explorer/dart-2.14.4/bin/dart diff --git a/etc/config/dart.defaults.properties b/etc/config/dart.defaults.properties new file mode 100644 index 000000000..140bf3312 --- /dev/null +++ b/etc/config/dart.defaults.properties @@ -0,0 +1,4 @@ +compilers=/usr/bin/dart +supportsBinary=true +supportsExecute=true +compilerType=dart diff --git a/examples/dart/default.dart b/examples/dart/default.dart new file mode 100644 index 000000000..c8ac4ab6d --- /dev/null +++ b/examples/dart/default.dart @@ -0,0 +1,8 @@ +// Type your code here, or load an example. +int square(int num) { + return num * num; +} + +int main(List args) { + return square(int.fromEnvironment("input")); +} diff --git a/lib/asm-parser-dart.js b/lib/asm-parser-dart.js new file mode 100644 index 000000000..b0c3ba31b --- /dev/null +++ b/lib/asm-parser-dart.js @@ -0,0 +1,165 @@ +// Copyright (c) 2022, Compiler Explorer Authors +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +import { AsmParser } from './asm-parser'; +import { AsmRegex } from './asmregex'; + +import * as utils from './utils'; + +export class DartAsmParser extends AsmParser { + constructor() { + super(); + + this.lineRe = /^(file:)?(\/[^:]+):(?\d+).*/; + } + + processBinaryAsm(asmResult, filters) { + const startTime = process.hrtime.bigint(); + const asm = []; + const labelDefinitions = {}; + const dontMaskFilenames = filters.dontMaskFilenames; + + let asmLines = asmResult.split('\n'); + const startingLineCount = asmLines.length; + let source = null; + let func = null; + let mayRemovePreviousLabel = filters.libraryCode; + + function maybeRemovePreviousLabel() { + if (mayRemovePreviousLabel) { + const previousLabelStart = labelDefinitions[func]; + if (previousLabelStart) { + asm.splice(previousLabelStart - 1); + } + } + } + + // Handle "error" documents. + if (asmLines.length === 1 && asmLines[0][0] === '<') { + return { + asm: [{text: asmLines[0], source: null}], + }; + } + + if (filters.preProcessBinaryAsmLines !== undefined) { + asmLines = filters.preProcessBinaryAsmLines(asmLines); + } + + for (const line of asmLines) { + const labelsInLine = []; + + if (asm.length >= this.maxAsmLines) { + if (asm.length === this.maxAsmLines) { + asm.push({ + text: '[truncated; too many lines]', + source: null, + labels: labelsInLine, + }); + } + continue; + } + let match = line.match(this.lineRe); + if (match) { + if (dontMaskFilenames) { + source = { + file: utils.maskRootdir(match[1]), + line: parseInt(match.groups.line), + mainsource: true, + }; + } else { + source = {file: null, line: parseInt(match.groups.line), mainsource: true}; + } + continue; + } + + match = line.match(this.labelRe); + if (match) { + maybeRemovePreviousLabel(); + mayRemovePreviousLabel = filters.libraryCode; + source = null; + func = match[2]; + if (this.isUserFunction(func)) { + asm.push({ + text: func + ':', + source: null, + labels: labelsInLine, + }); + labelDefinitions[func] = asm.length; + } + continue; + } + + if (func && line === `${func}():`) continue; + + if (!func || !this.isUserFunction(func)) continue; + + // note: normally the source.file will be null if it's code from example.ext + // but with filters.dontMaskFilenames it will be filled with the actual filename + // instead we can test source.mainsource in that situation + const isMainsource = source && ((source.file === null) || source.mainsource); + if (isMainsource) { + mayRemovePreviousLabel = false; + } + + match = line.match(this.asmOpcodeRe); + if (match) { + const address = parseInt(match.groups.address, 16); + const opcodes = match.groups.opcodes.split(' ').filter(x => !!x); + const disassembly = ' ' + AsmRegex.filterAsmLine(match.groups.disasm, filters); + const destMatch = line.match(this.destRe); + if (destMatch) { + const labelName = destMatch[2]; + const startCol = disassembly.indexOf(labelName) + 1; + labelsInLine.push({ + name: labelName, + range: { + startCol: startCol, + endCol: startCol + labelName.length, + }, + }); + } + asm.push({ + opcodes: opcodes, + address: address, + text: disassembly, + source: source, + labels: labelsInLine, + }); + } + } + + maybeRemovePreviousLabel(); + + this.removeLabelsWithoutDefinition(asm, labelDefinitions); + + const endTime = process.hrtime.bigint(); + + return { + asm: asm, + labelDefinitions: labelDefinitions, + parsingTime: ((endTime - startTime) / BigInt(1000000)).toString(), + filteredCount: startingLineCount - asm.length, + }; + } +} diff --git a/lib/compilers/_all.js b/lib/compilers/_all.js index d93272d96..b2c8142bf 100644 --- a/lib/compilers/_all.js +++ b/lib/compilers/_all.js @@ -34,6 +34,7 @@ export { ClangHipCompiler } from './clang'; export { CleanCompiler } from './clean'; export { CprocCompiler } from './cproc'; export { CrystalCompiler } from './crystal'; +export { DartCompiler } from './dart'; export { DefaultCompiler } from './default'; export { DMDCompiler } from './dmd'; export { CSharpCompiler } from './dotnet'; diff --git a/lib/compilers/dart.js b/lib/compilers/dart.js new file mode 100644 index 000000000..88bedd097 --- /dev/null +++ b/lib/compilers/dart.js @@ -0,0 +1,69 @@ +// Copyright (c) 2021, Compiler Explorer Authors +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +import { DartAsmParser } from '../asm-parser-dart'; +import { BaseCompiler } from '../base-compiler'; +import * as utils from '../utils'; + +import { BaseParser } from './argument-parsers'; + +export class DartCompiler extends BaseCompiler { + constructor(info, env) { + super(info, env); + this.asm = new DartAsmParser(); + } + + static get key() { return 'dart'; } + + prepareArguments(userOptions, filters, backendOptions, inputFilename, outputFilename, libraries) { + let options = this.optionsForFilter(filters, outputFilename, userOptions); + + if (this.compiler.options) { + options = options.concat(utils.splitArguments(this.compiler.options)); + } + + const libIncludes = this.getIncludeArguments(libraries); + const libOptions = this.getLibraryOptions(libraries); + + userOptions = this.filterUserOptions(userOptions) || []; + return options.concat(libIncludes, libOptions, userOptions, [this.filename(inputFilename)]); + } + + optionsForFilter(filters, outputFilename) { + // Dart includes way too much of the standard library (even for simple programs) + // to show all of it without truncation + filters.libraryCode = true; + // Dart doesn't support emitting assembly + filters.binary = true; + return [ + 'compile', + 'aot-snapshot', + '-o', this.filename(outputFilename), + ]; + } + + getArgumentParser() { + return BaseParser; + } +} diff --git a/lib/languages.js b/lib/languages.js index 3a543a2b5..87fa87d6f 100644 --- a/lib/languages.js +++ b/lib/languages.js @@ -277,6 +277,12 @@ export const languages = { extensions: ['.vb'], alias: [], }, + dart: { + name: 'Dart', + monaco: 'dart', + extensions: ['.dart'], + alias: [], + }, }; _.each(languages, (lang, key) => { diff --git a/webpack.config.esm.js b/webpack.config.esm.js index 28b4ea642..e813d31c1 100644 --- a/webpack.config.esm.js +++ b/webpack.config.esm.js @@ -47,7 +47,7 @@ const webjackJsHack = '.v5.'; const plugins = [ new MonacoEditorWebpackPlugin({ languages: ['cpp', 'go', 'pascal', 'python', 'rust', 'swift', 'java', - 'kotlin', 'scala', 'ruby', 'csharp', 'fsharp', 'vb'], + 'kotlin', 'scala', 'ruby', 'csharp', 'fsharp', 'vb', 'dart'], filename: isDev ? '[name].worker.js' : `[name]${webjackJsHack}worker.[contenthash].js`, }), new ProvidePlugin({