mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
inline assembly
This commit is contained in:
parent
5a1de13e10
commit
2964bf9e23
38
vlib/compiler/asm.v
Normal file
38
vlib/compiler/asm.v
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module compiler
|
||||
|
||||
fn (p mut Parser) inline_asm() {
|
||||
if !p.inside_unsafe {
|
||||
p.error('asm() needs to be run unside `unsafe {}`')
|
||||
}
|
||||
p.next()
|
||||
p.check(.lpar)
|
||||
s := p.check_string()
|
||||
p.genln('asm("$s"')
|
||||
for p.tok == .str {
|
||||
p.genln('"$p.lit"')
|
||||
p.next()
|
||||
}
|
||||
for p.tok == .colon {
|
||||
p.next()
|
||||
arg := p.check_string()
|
||||
p.gen(': "$arg"')
|
||||
if p.tok == .lpar {
|
||||
p.next()
|
||||
var_name := p.check_name()
|
||||
if !p.known_var(var_name) {
|
||||
p.error('unknown variable `$var_name`')
|
||||
}
|
||||
p.check(.rpar)
|
||||
p.genln('($var_name)')
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
p.genln(');')
|
||||
p.check(.rpar)
|
||||
|
||||
}
|
@ -1208,6 +1208,9 @@ fn (p mut Parser) statement(add_semi bool) string {
|
||||
.key_assert {
|
||||
p.assert_statement()
|
||||
}
|
||||
.key_asm {
|
||||
p.inline_asm()
|
||||
}
|
||||
else {
|
||||
// An expression as a statement
|
||||
typ := p.expression()
|
||||
@ -2745,7 +2748,7 @@ fn (p mut Parser) return_st() {
|
||||
p.returns = true
|
||||
}
|
||||
|
||||
fn (p Parser) get_deferred_text() string {
|
||||
fn (p &Parser) get_deferred_text() string {
|
||||
// @emily33901: Scoped defer
|
||||
// Check all of our defer texts to see if there is one at a higher scope level
|
||||
// The one for our current scope would be the last so any before that need to be
|
||||
|
14
vlib/compiler/tests/asm_test.v
Normal file
14
vlib/compiler/tests/asm_test.v
Normal file
@ -0,0 +1,14 @@
|
||||
fn test_inline_asm() {
|
||||
a := 10
|
||||
b := 0
|
||||
unsafe {
|
||||
asm ("movl %1, %%eax;"
|
||||
"movl %%eax, %0;"
|
||||
:"=r"(b)
|
||||
:"r"(a)
|
||||
:"%eax"
|
||||
)
|
||||
}
|
||||
assert a == 10
|
||||
assert b == 10
|
||||
}
|
@ -80,6 +80,7 @@ enum TokenKind {
|
||||
// keywords
|
||||
keyword_beg
|
||||
key_as
|
||||
key_asm
|
||||
key_assert
|
||||
key_atomic
|
||||
key_break
|
||||
@ -201,6 +202,7 @@ fn build_token_str() []string {
|
||||
s[TokenKind.key_if] = 'if'
|
||||
//s[TokenKind.key_it] = 'it'
|
||||
s[TokenKind.key_else] = 'else'
|
||||
s[TokenKind.key_asm] = 'asm'
|
||||
s[TokenKind.key_return] = 'return'
|
||||
s[TokenKind.key_module] = 'module'
|
||||
s[TokenKind.key_sizeof] = 'sizeof'
|
||||
|
Loading…
Reference in New Issue
Block a user