From 02d823fa6e78ce5e0afcebfa85b48297c31cd486 Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 20 Aug 2021 13:23:15 +0200 Subject: [PATCH] v.gen.native: support infinite loops `for {}` in amd64 and arm64 (#11254) --- vlib/v/gen/native/amd64.v | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vlib/v/gen/native/amd64.v b/vlib/v/gen/native/amd64.v index 8caf22565b..6ff7a5f3e0 100644 --- a/vlib/v/gen/native/amd64.v +++ b/vlib/v/gen/native/amd64.v @@ -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) { + 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 // g.mov(.eax, 0x77777777) mut jump_addr := 0 // location of `jne *00 00 00 00*`