Add Dart support (#3352)

This commit is contained in:
Michael Debertol
2022-02-11 16:19:16 +01:00
committed by GitHub
parent 7266bc52be
commit 3f695c9cc1
9 changed files with 271 additions and 1 deletions

3
.github/labeler.yml vendored
View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,4 @@
compilers=/usr/bin/dart
supportsBinary=true
supportsExecute=true
compilerType=dart

View File

@@ -0,0 +1,8 @@
// Type your code here, or load an example.
int square(int num) {
return num * num;
}
int main(List<String> args) {
return square(int.fromEnvironment("input"));
}

165
lib/asm-parser-dart.js Normal file
View File

@@ -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:)?(\/[^:]+):(?<line>\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,
};
}
}

View File

@@ -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';

69
lib/compilers/dart.js Normal file
View File

@@ -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;
}
}

View File

@@ -277,6 +277,12 @@ export const languages = {
extensions: ['.vb'],
alias: [],
},
dart: {
name: 'Dart',
monaco: 'dart',
extensions: ['.dart'],
alias: [],
},
};
_.each(languages, (lang, key) => {

View File

@@ -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({