Skip to content

Commit

Permalink
register wasm, add asm
Browse files Browse the repository at this point in the history
  • Loading branch information
l1mey112 committed Oct 13, 2023
1 parent 87216f2 commit 13bb68d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
3 changes: 3 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,9 @@ pub fn all_registers(mut t Table, arch pref.Arch) map[string]ScopeObject {
res[k] = v
}
}
.wasm32 {
// no registers
}
else { // TODO
panic('all_registers: unhandled arch')
}
Expand Down
28 changes: 28 additions & 0 deletions vlib/v/gen/wasm/asm.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2023 l-m.dev. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module wasm

import v.ast

pub fn (mut g Gen) asm_template(parent ast.AsmStmt, node ast.AsmTemplate) {
if node.is_label || node.is_directive {
g.v_error("`asm wasm` doesn't support labels or directives", node.pos)
}

match node.name {
'i32.const' {
g.expr()
eprintln(node)
}
else {
g.v_error('unknown opcode', node.pos)
}
}
}

pub fn (mut g Gen) asm_stmt(node ast.AsmStmt) {
for tmpl in node.templates {
g.asm_template(node, tmpl)
}
}
4 changes: 4 additions & 0 deletions vlib/v/gen/wasm/gen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@ pub fn (mut g Gen) expr_stmt(node ast.Stmt, expected ast.Type) {
}
}
}
ast.AsmStmt {
// assumed expected == void
g.asm_stmt(node)
}
else {
g.w_error('wasm.expr_stmt(): unhandled node: ' + node.type_name())
}
Expand Down
24 changes: 19 additions & 5 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,11 @@ fn (mut p Parser) asm_stmt(is_top_level bool) ast.AsmStmt {

p.check(.key_asm)
mut arch := pref.arch_from_string(p.tok.lit) or { pref.Arch._auto }

if is_top_level && arch == .wasm32 {
p.error("wasm doesn't support toplevel assembly")
}

mut is_volatile := false
mut is_goto := false
if p.tok.kind == .key_volatile {
Expand All @@ -1175,7 +1180,7 @@ fn (mut p Parser) asm_stmt(is_top_level bool) ast.AsmStmt {
}
if arch == ._auto && !p.pref.is_fmt {
if p.tok.lit == '' {
p.error('missing assembly architecture. Try i386, amd64 or arm64.')
p.error('missing assembly architecture. Try i386, amd64, arm64, or wasm.')
}
p.error('unknown assembly architecture')
}
Expand Down Expand Up @@ -1227,13 +1232,19 @@ fn (mut p Parser) asm_stmt(is_top_level bool) ast.AsmStmt {
name += p.tok.lit
p.check(.name)
}
// dots are part of instructions for some riscv extensions
if arch in [.rv32, .rv64] {
// dots are part of instructions for some riscv extensions and webassembly
if arch in [.rv32, .rv64, .wasm32] {
for p.tok.kind == .dot {
name += '.'
p.next()
name += p.tok.lit
p.check(.name)
// wasm: i32.const
if arch == .wasm32 && p.tok.kind == .key_const {
name += 'const'
p.next()
} else {
name += p.tok.lit
p.check(.name)
}
}
}
mut is_label := false
Expand Down Expand Up @@ -1293,6 +1304,9 @@ fn (mut p Parser) asm_stmt(is_top_level bool) ast.AsmStmt {
break
}
.lsbr {
if arch == .wasm32 {
p.error("wasm doesn't have addressing operands")
}
mut addressing := p.asm_addressing()
addressing.segment = segment
args << addressing
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
if b == .wasm {
res.compile_defines << 'wasm'
res.compile_defines_all << 'wasm'
res.arch = .wasm32
}
res.backend = b
i++
Expand Down Expand Up @@ -1083,7 +1084,7 @@ pub fn arch_from_string(arch_str string) !Arch {
'js_freestanding' {
return .js_freestanding
}
'wasm32' {
'wasm32', 'wasm' {
return .wasm32
}
'' {
Expand Down

0 comments on commit 13bb68d

Please sign in to comment.