From 6afe3a69472bd6bc52bd3d815f672d430d0f80f0 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 18 Oct 2020 12:58:58 -0400 Subject: [PATCH] Switch to using global initializer --- .gitignore | 1 - Cargo.lock | 2 ++ gcc_path | 2 +- src/base.rs | 2 +- src/common.rs | 13 +++---------- src/declare.rs | 16 ++++++++++------ 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 5e1cbb6d01d..dd5bd98d7eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -gcc_path target **/*.rs.bk *.rlib diff --git a/Cargo.lock b/Cargo.lock index e4b9d48cb1d..c0fd4a20494 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,7 @@ dependencies = [ [[package]] name = "gccjit" version = "1.0.0" +source = "git+https://github.com/antoyo/gccjit.rs?branch=feature/rustc#d47429baf195f599667b18585bc29603c0abd48a" dependencies = [ "gccjit_sys", ] @@ -37,6 +38,7 @@ dependencies = [ [[package]] name = "gccjit_sys" version = "0.0.1" +source = "git+https://github.com/antoyo/gccjit.rs?branch=feature/rustc#d47429baf195f599667b18585bc29603c0abd48a" dependencies = [ "libc 0.1.12", ] diff --git a/gcc_path b/gcc_path index 85e26eb02d4..b3696e3cf88 100644 --- a/gcc_path +++ b/gcc_path @@ -1 +1 @@ -/tmp/gcc-build/build/gcc +/home/bouanto/Ordinateur/Programmation/Projets/gcc-build/build/gcc diff --git a/src/base.rs b/src/base.rs index 4d70a3e99b3..193b579ed57 100644 --- a/src/base.rs +++ b/src/base.rs @@ -73,7 +73,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul //let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str()); let context = Context::default(); //context.set_dump_code_on_compile(true); - context.set_dump_initial_gimple(true); + //context.set_dump_initial_gimple(true); context.set_debug_info(true); //context.set_dump_everything(true); //context.set_keep_intermediates(true); diff --git a/src/common.rs b/src/common.rs index 496eec631ec..87540e9f0b3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -81,18 +81,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { } pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> { - // TODO: use gcc_jit_context_new_blob when it's merged? - // https://gcc.gnu.org/pipermail/jit/2020q2/001214.html let context = &cx.context; let typ = context.new_array_type(None, context.new_type::(), bytes.len() as i32); - let value = cx.global_init_func.new_local(None, typ, "bytesTODO"); - let block = cx.global_init_block; - for (index, byte) in bytes.iter().enumerate() { - let index = context.new_rvalue_from_long(cx.i32_type, index as i64); - let byte = context.new_rvalue_from_long(context.new_type::(), *byte as i64); - block.add_assignment(None, context.new_array_access(None, value, index), byte); - } - value.to_rvalue() + let global = cx.declare_unnamed_global(typ); + global.global_set_initializer(bytes); + global.to_rvalue() } pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool { diff --git a/src/declare.rs b/src/declare.rs index 6f9deffd7f7..f0504b22c4e 100644 --- a/src/declare.rs +++ b/src/declare.rs @@ -1,4 +1,4 @@ -use gccjit::{Function, FunctionType, GlobalKind, RValue, ToRValue, Type}; +use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, ToRValue, Type}; use rustc_codegen_ssa::traits::{BaseTypeMethods, DeclareMethods}; use rustc_middle::ty::Ty; use rustc_target::abi::call::FnAbi; @@ -18,6 +18,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { } } + pub fn declare_unnamed_global(&self, ty: Type<'gcc>) -> LValue<'gcc> { + let index = self.global_gen_sym_counter.get(); + self.global_gen_sym_counter.set(index + 1); + let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit)); + self.context.new_global(None, GlobalKind::Exported, ty, &name) + } + pub fn declare_global_with_linkage(&self, name: &str, ty: Type<'gcc>, linkage: GlobalKind) -> RValue<'gcc> { //debug!("declare_global_with_linkage(name={:?})", name); let global = self.context.new_global(None, linkage, ty, name) @@ -77,11 +84,8 @@ impl<'gcc, 'tcx> DeclareMethods<'tcx> for CodegenCx<'gcc, 'tcx> { } fn define_private_global(&self, ty: Type<'gcc>) -> RValue<'gcc> { - let index = self.global_gen_sym_counter.get(); - self.global_gen_sym_counter.set(index + 1); - let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit)); - self.context.new_global(None, GlobalKind::Exported, ty, &name) - .get_address(None) + let global = self.declare_unnamed_global(ty); + global.get_address(None) } fn get_declared_value(&self, name: &str) -> Option> {