Skip to content

Commit

Permalink
Add webassembly language (#6429)
Browse files Browse the repository at this point in the history
馃憢 Hey,

This PR adds support for the Webassembly Text Language (#1871).
Currently it only supports the
[wasmtime](https://github.com/bytecodealliance/wasmtime) compiler, but
it should be possible to add others (i.e. v8 / wasmer / etc...).

It looks like the monaco does not support the WAT language, so I've
added a simple mode for it, that is mostly copied from the clojure mode
with some changes. From what I've tested it seems to work reasonably
well.


![image](https://github.com/compiler-explorer/compiler-explorer/assets/1357143/9e38b28e-235c-47e5-a71e-50217e5d2056)

---------

Co-authored-by: Patrick Quist <partouf@gmail.com>
  • Loading branch information
afonso360 and partouf committed Jun 11, 2024
1 parent d8d4f57 commit bdb5fea
Show file tree
Hide file tree
Showing 12 changed files with 868 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@
- 'etc/config/vala.*.properties'
- 'static/modes/vala-mode.ts'

'lang-wasm':
- changed-files:
- any-glob-to-any-file:
- 'lib/compilers/wasmtime.ts'
- 'etc/config/wasm.*.properties'
- 'static/modes/wat-mode.ts'

'lang-zig':
- changed-files:
- any-glob-to-any-file:
Expand Down
21 changes: 21 additions & 0 deletions etc/config/wasm.amazon.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
compilers=&wasmtime
objdumper=llvm-objdump
objdumperType=llvm
binaryHideFuncRe=^.*::(:?native_to_wasm_trampoline|wasm_to_native_trampoline|array_to_wasm_trampoline).*$

group.wasmtime.compilers=wasmtime2001
group.wasmtime.compilerType=wasmtime
group.wasmtime.options=-Ddebug-info=y -Ccache=n
group.wasmtime.supportsBinary=true
group.wasmtime.supportsBinaryObject=true
group.wasmtime.versionFlag=--version
group.wasmtime.isSemVer=true
group.wasmtime.baseName=Wasmtime

compiler.wasmtime2001.exe=/opt/compiler-explorer/wasmtime-20.0.1/wasmtime
compiler.wasmtime2001.semver=20.0.1

#################################
#################################
# Installed libs (See c++.amazon.properties for a scheme of libs group)
libs=
20 changes: 20 additions & 0 deletions etc/config/wasm.defaults.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Default settings for Wasm
compilers=&wasmtimelocal
objdumper=llvm-objdump
objdumperType=llvm
binaryHideFuncRe=^.*::(:?native_to_wasm_trampoline|wasm_to_native_trampoline|array_to_wasm_trampoline).*$

group.wasmtimelocal.compilers=wasmtimedefault
group.wasmtimelocal.compilerType=wasmtime
group.wasmtimelocal.options=-Ddebug-info=y -Ccache=n
group.wasmtimelocal.supportsBinary=true
group.wasmtimelocal.supportsBinaryObject=true
group.wasmtimelocal.versionFlag=--version

compiler.wasmtimedefault.exe=/opt/compiler-explorer/wasmtime-20.0.1/wasmtime
compiler.wasmtimedefault.name=Wasmtime default

#################################
#################################
# Installed libs (See c++.amazon.properties for a scheme of libs group)
libs=
7 changes: 7 additions & 0 deletions examples/wasm/default.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(module
(func $square (param i32) (result i32)
local.get 0
local.get 0
i32.mul
)
)
1 change: 1 addition & 0 deletions lib/compilers/_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export {Win32MingWClang} from './win32-mingw-clang.js';
export {WineVcCompiler} from './wine-vc.js';
export {WslVcCompiler} from './wsl-vc.js';
export {WyrmCompiler} from './wyrm.js';
export {WasmtimeCompiler} from './wasmtime.js';
export {ZigCC} from './zigcc.js';
export {ZigCompiler} from './zig.js';
export {ZigCXX} from './zigcxx.js';
Expand Down
7 changes: 7 additions & 0 deletions lib/compilers/argument-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,13 @@ export class Z88dkParser extends BaseParser {
}
}

export class WasmtimeParser extends BaseParser {
static override async parse(compiler) {
await this.getOptions(compiler, '--help');
return compiler;
}
}

export class ZigCxxParser extends ClangParser {
static override getMainHelpOptions(): string[] {
return ['c++', '--help'];
Expand Down
70 changes: 70 additions & 0 deletions lib/compilers/wasmtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2024, 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 {BaseCompiler} from '../base-compiler.js';

import {WasmtimeParser} from './argument-parsers.js';

export class WasmtimeCompiler extends BaseCompiler {
static get key() {
return 'wasmtime';
}

override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string, userOptions?: string[]) {
filters.binary = true;
return ['-o', this.filename(outputFilename)];
}

override orderArguments(
options: string[],
inputFilename: string,
libIncludes: string[],
libOptions: string[],
libPaths: string[],
libLinks: string[],
userOptions: string[],
staticLibLinks: string[],
): string[] {
return ['compile', inputFilename]
.concat(options)
.concat(libIncludes, libOptions, libPaths, libLinks, userOptions, staticLibLinks);
}

override getOutputFilename(dirPath: string, outputFilebase: string): string {
return path.join(dirPath, `${outputFilebase}.cwasm`);
}

override getArgumentParser() {
return WasmtimeParser;
}

override getSharedLibraryPathsAsArguments() {
// Wasmtime does not have an equivalent to -Wl,-rpath in its driver, return
// an empty list.
return [];
}
}
11 changes: 11 additions & 0 deletions lib/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,17 @@ const definitions: Record<LanguageKey, LanguageDefinition> = {
previewFilter: null,
monacoDisassembly: null,
},
wasm: {
name: 'WASM',
monaco: 'wat',
extensions: ['.wat'],
alias: [],
logoUrl: 'wasm.svg',
logoUrlDark: null,
formatter: null,
previewFilter: null,
monacoDisassembly: null,
},
zig: {
name: 'Zig',
monaco: 'zig',
Expand Down
1 change: 1 addition & 0 deletions static/modes/_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ import './spirv-mode';
import './tablegen-mode';
import './v-mode';
import './vala-mode';
import './wat-mode';
import './zig-mode';

0 comments on commit bdb5fea

Please sign in to comment.