+bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ size_t i;
+ ir_value *dummy;
+
+ *out = NULL;
+ if (lvalue) {
+ compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
+ return false;
+ }
+
+ /* simply create a new block and jump to it */
+ self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
+ if (!self->irblock) {
+ compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
+ return false;
+ }
+ if (!func->curblock->final) {
+ if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
+ return false;
+ }
+
+ /* enter the new block */
+ func->curblock = self->irblock;
+
+ /* Generate all the leftover gotos */
+ for (i = 0; i < vec_size(self->gotos); ++i) {
+ if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
+ return false;
+ }
+
+ return true;
+}
+
+bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ *out = NULL;
+ if (lvalue) {
+ compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
+ return false;
+ }
+
+ if (self->target->irblock) {
+ if (self->irblock_from) {
+ /* we already tried once, this is the callback */
+ self->irblock_from->final = false;
+ if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
+ compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
+ return false;
+ }
+ }
+ else
+ {
+ if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
+ compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
+ return false;
+ }
+ }
+ }
+ else
+ {
+ /* the target has not yet been created...
+ * close this block in a sneaky way:
+ */
+ func->curblock->final = true;
+ self->irblock_from = func->curblock;
+ ast_label_register_goto(self->target, self);
+ }
+
+ return true;
+}
+