Skip to content

Switch to using global initializer #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
gcc_path
target
**/*.rs.bk
*.rlib
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gcc_path
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/tmp/gcc-build/build/gcc
/home/bouanto/Ordinateur/Programmation/Projets/gcc-build/build/gcc
2 changes: 1 addition & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 3 additions & 10 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(), 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::<u8>(), *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 {
Expand Down
16 changes: 10 additions & 6 deletions src/declare.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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<RValue<'gcc>> {
Expand Down