Skip to content

Commit b6c25f3

Browse files
authored
Merge pull request #6 from antoyo/global-initializer
Switch to using global initializer
2 parents dfef19e + 6afe3a6 commit b6c25f3

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
gcc_path
21
target
32
**/*.rs.bk
43
*.rlib

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gcc_path

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/tmp/gcc-build/build/gcc
1+
/home/bouanto/Ordinateur/Programmation/Projets/gcc-build/build/gcc

src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
7373
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
7474
let context = Context::default();
7575
//context.set_dump_code_on_compile(true);
76-
context.set_dump_initial_gimple(true);
76+
//context.set_dump_initial_gimple(true);
7777
context.set_debug_info(true);
7878
//context.set_dump_everything(true);
7979
//context.set_keep_intermediates(true);

src/common.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
8181
}
8282

8383
pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> {
84-
// TODO: use gcc_jit_context_new_blob when it's merged?
85-
// https://gcc.gnu.org/pipermail/jit/2020q2/001214.html
8684
let context = &cx.context;
8785
let typ = context.new_array_type(None, context.new_type::<u8>(), bytes.len() as i32);
88-
let value = cx.global_init_func.new_local(None, typ, "bytesTODO");
89-
let block = cx.global_init_block;
90-
for (index, byte) in bytes.iter().enumerate() {
91-
let index = context.new_rvalue_from_long(cx.i32_type, index as i64);
92-
let byte = context.new_rvalue_from_long(context.new_type::<u8>(), *byte as i64);
93-
block.add_assignment(None, context.new_array_access(None, value, index), byte);
94-
}
95-
value.to_rvalue()
86+
let global = cx.declare_unnamed_global(typ);
87+
global.global_set_initializer(bytes);
88+
global.to_rvalue()
9689
}
9790

9891
pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {

src/declare.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gccjit::{Function, FunctionType, GlobalKind, RValue, ToRValue, Type};
1+
use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, ToRValue, Type};
22
use rustc_codegen_ssa::traits::{BaseTypeMethods, DeclareMethods};
33
use rustc_middle::ty::Ty;
44
use rustc_target::abi::call::FnAbi;
@@ -18,6 +18,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
1818
}
1919
}
2020

21+
pub fn declare_unnamed_global(&self, ty: Type<'gcc>) -> LValue<'gcc> {
22+
let index = self.global_gen_sym_counter.get();
23+
self.global_gen_sym_counter.set(index + 1);
24+
let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit));
25+
self.context.new_global(None, GlobalKind::Exported, ty, &name)
26+
}
27+
2128
pub fn declare_global_with_linkage(&self, name: &str, ty: Type<'gcc>, linkage: GlobalKind) -> RValue<'gcc> {
2229
//debug!("declare_global_with_linkage(name={:?})", name);
2330
let global = self.context.new_global(None, linkage, ty, name)
@@ -77,11 +84,8 @@ impl<'gcc, 'tcx> DeclareMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
7784
}
7885

7986
fn define_private_global(&self, ty: Type<'gcc>) -> RValue<'gcc> {
80-
let index = self.global_gen_sym_counter.get();
81-
self.global_gen_sym_counter.set(index + 1);
82-
let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit));
83-
self.context.new_global(None, GlobalKind::Exported, ty, &name)
84-
.get_address(None)
87+
let global = self.declare_unnamed_global(ty);
88+
global.get_address(None)
8589
}
8690

8791
fn get_declared_value(&self, name: &str) -> Option<RValue<'gcc>> {

0 commit comments

Comments
 (0)