|
| 1 | +#include "passes.h" |
| 2 | + |
| 3 | +#include "log.h" |
| 4 | +#include "portability.h" |
| 5 | + |
| 6 | +#include "../ir_private.h" |
| 7 | +#include "../type.h" |
| 8 | +#include "../rewrite.h" |
| 9 | +#include "../transform/ir_gen_helpers.h" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + Rewriter rewriter; |
| 13 | +} Context; |
| 14 | + |
| 15 | +static const Node* process(Context* ctx, const Node* node) { |
| 16 | + IrArena* a = ctx->rewriter.dst_arena; |
| 17 | + switch (node->tag) { |
| 18 | + case GlobalVariable_TAG: { |
| 19 | + if (node->payload.global_variable.address_space == AsGeneric) { |
| 20 | + AddressSpace dst_as = AsGlobalPhysical; |
| 21 | + const Type* t = rewrite_node(&ctx->rewriter, node->payload.global_variable.type); |
| 22 | + Node* new_global = global_var(ctx->rewriter.dst_module, rewrite_nodes(&ctx->rewriter, node->payload.global_variable.annotations), t, node->payload.global_variable.name, dst_as); |
| 23 | + |
| 24 | + const Type* dst_t = ptr_type(a, (PtrType) { .pointed_type = t, .address_space = AsGeneric }); |
| 25 | + Nodes decl_annotations = singleton(annotation(a, (Annotation) { .name = "Generated" })); |
| 26 | + Node* constant_decl = constant(ctx->rewriter.dst_module, decl_annotations, dst_t, |
| 27 | + format_string_interned(a, "%s_generic", get_decl_name(node))); |
| 28 | + const Node* result = ref_decl_helper(a, constant_decl); |
| 29 | + constant_decl->payload.constant.instruction = prim_op_helper(a, convert_op, singleton(dst_t), singleton(ref_decl_helper(a, new_global))); |
| 30 | + register_processed(&ctx->rewriter, node, result); |
| 31 | + new_global->payload.global_variable.init = rewrite_node(&ctx->rewriter, node->payload.global_variable.init); |
| 32 | + return result; |
| 33 | + } |
| 34 | + } |
| 35 | + default: break; |
| 36 | + } |
| 37 | + |
| 38 | + return recreate_node_identity(&ctx->rewriter, node); |
| 39 | +} |
| 40 | + |
| 41 | +Module* lower_generic_globals(SHADY_UNUSED const CompilerConfig* config, Module* src) { |
| 42 | + ArenaConfig aconfig = get_arena_config(get_module_arena(src)); |
| 43 | + IrArena* a = new_ir_arena(aconfig); |
| 44 | + Module* dst = new_module(a, get_module_name(src)); |
| 45 | + Context ctx = { |
| 46 | + .rewriter = create_rewriter(src, dst, (RewriteNodeFn) process), |
| 47 | + }; |
| 48 | + rewrite_module(&ctx.rewriter); |
| 49 | + destroy_rewriter(&ctx.rewriter); |
| 50 | + return dst; |
| 51 | +} |
0 commit comments