v.gen.native: support infinite loops `for {}` in amd64 and arm64 (#11254)

pull/11260/head
pancake 2021-08-20 13:23:15 +02:00 committed by GitHub
parent 82b16453e4
commit 02d823fa6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -1151,7 +1151,29 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
} }
} }
fn (mut g Gen) infloop() {
if g.pref.arch == .arm64 {
g.write32(byte(0x14))
} else {
g.write8(byte(0xeb))
g.write8(byte(0xfe))
}
g.println('jmp $$')
}
fn (mut g Gen) for_stmt(node ast.ForStmt) { fn (mut g Gen) for_stmt(node ast.ForStmt) {
if node.is_inf {
if node.stmts.len == 0 {
g.infloop()
return
}
// infinite loop
start := g.pos()
g.stmts(node.stmts)
g.jmp(int(0xffffffff - (g.pos() + 5 - start) + 1))
g.println('jmp after infinite for')
return
}
infix_expr := node.cond as ast.InfixExpr infix_expr := node.cond as ast.InfixExpr
// g.mov(.eax, 0x77777777) // g.mov(.eax, 0x77777777)
mut jump_addr := 0 // location of `jne *00 00 00 00*` mut jump_addr := 0 // location of `jne *00 00 00 00*`