Skip to content

Commit

Permalink
Add d8 for disassembling javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
gautam1168 committed Mar 22, 2023
1 parent 01935ec commit 58d832c
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 0 deletions.
17 changes: 17 additions & 0 deletions etc/config/javascript.amazon.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
compilers=javascript
defaultCompiler=javascript
instructionSet=d8

supportsBinary=false
interpreted=true
compilerType=d8
objdumper=
demangler=
postProcess=
options=
supportsExecute=false
stubText=function add(a, b) { return a + b; } for (var i = 0; i < 10; i++) { add(i, 1) }

compiler.javascript.exe=/opt/compiler-explorer/d8-11.1/out/x64.release/d8
compiler.javascript.name=d8
compiler.javascript.options=--print-code --redirect-code-traces --allow-natives-syntax
19 changes: 19 additions & 0 deletions etc/config/javascript.defaults.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Default settings for Javascript

compilers=javascript
defaultCompiler=javascript
instructionSet=d8

supportsBinary=false
interpreted=true
compilerType=d8
objdumper=
demangler=
postProcess=
options=
supportsExecute=false
stubText=function add(a, b) { return a + b; } for (var i = 0; i < 10; i++) { add(i, 1) }

compiler.javascript.exe=d8
compiler.javascript.name=d8
compiler.javascript.options=--print-code --redirect-code-traces --allow-natives-syntax
32 changes: 32 additions & 0 deletions examples/javascript/default.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function square(a, b) {
let result = 0;
for (let i = 0; i < 1e6; i++) {
result += a * i + b;
if (result > 1e6) {
result = result - 13;
}
}
return result;
}

// Get optimization status of function to prepare for optimization
let optstatus = %GetOptimizationStatus(square);
console.log(optstatus.toString(2).padStart(12, '0'));

// Call function once to fill type information
square(23, 2);

// Call function again to go from uninitialized -> pre-monomorphic -> monomorphic
square(13, 43);
optstatus = %GetOptimizationStatus(square);
console.log(optstatus.toString(2).padStart(12, '0'));

%OptimizeFunctionOnNextCall(square);
optstatus = %GetOptimizationStatus(square);
console.log(optstatus.toString(2).padStart(12, '0'));

// Call function to optimize it
square(71, 19);

optstatus = %GetOptimizationStatus(square);
console.log(optstatus.toString(2).padStart(12, '0'));
1 change: 1 addition & 0 deletions lib/compilers/_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,4 @@ export {ZigCC} from './zigcc.js';
export {ZigCompiler} from './zig.js';
export {ZigCXX} from './zigcxx.js';
export {z88dkCompiler} from './z88dk.js';
export {JavascriptCompiler} from './d8.js';
68 changes: 68 additions & 0 deletions lib/compilers/d8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2023, 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 path from 'path';
import type {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js';
import type {PreliminaryCompilerInfo} from '../../types/compiler.interfaces.js';
import {BaseCompiler} from '../base-compiler.js';


export class JavascriptCompiler extends BaseCompiler {
private readonly disasmScriptPath: string;

static get key() {
return 'd8';
}

constructor(compilerInfo: PreliminaryCompilerInfo, env) {
super(compilerInfo, env);
this.compiler.demangler = '';
this.demanglerClass = null;
this.disasmScriptPath =
this.compilerProps<string>('disasmScript');
}

override getIrOutputFilename(inputFilename: string, filters: ParseFiltersAndOutputOptions): string {
return this.filename(path.dirname(inputFilename) + '/code.asm');
}

public override getOutputFilename(dirPath: string, outputFilebase: string, key?: any) {
let filename;
if (key && key.backendOptions && key.backendOptions.customOutputFilename) {
filename = key.backendOptions.customOutputFilename;
} else {
filename = 'code.asm';
}

if (dirPath) {
return path.join(dirPath, filename);
} else {
return filename;
}
}

override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string) {
return [];
}
}
11 changes: 11 additions & 0 deletions lib/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,17 @@ const definitions: Record<LanguageKey, LanguageDefinition> = {
previewFilter: null,
monacoDisassembly: null,
},
javascript: {
name: 'Javascript',
monaco: 'typescript',
extensions: ['.js'],
alias: [],
logoUrl: 'js.svg',
logoUrlDark: null,
formatter: null,
previewFilter: null,
monacoDisassembly: null,
},
};

export const languages: Record<LanguageKey, Language> = _.mapObject(definitions, (lang, key) => {
Expand Down
1 change: 1 addition & 0 deletions types/languages.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type LanguageKey =
| 'jakt'
| 'java'
| 'julia'
| 'javascript'
| 'kotlin'
| 'llvm'
| 'llvm_mir'
Expand Down
31 changes: 31 additions & 0 deletions views/resources/logos/js.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 58d832c

Please sign in to comment.