inline assembly

pull/2791/head
Alexander Medvednikov 2019-11-17 00:58:09 +03:00
parent 5a1de13e10
commit 2964bf9e23
4 changed files with 58 additions and 1 deletions

View 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)
}

View File

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

View 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
}

View File

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