|
1 | 1 | #[cfg(feature = "master")]
|
2 | 2 | use gccjit::{FnAttribute, VarAttribute, Visibility};
|
3 |
| -use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue, Type}; |
| 3 | +use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, ToRValue, Type}; |
4 | 4 | use rustc_codegen_ssa::traits::{
|
5 | 5 | BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
|
6 | 6 | };
|
@@ -92,7 +92,84 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
|
92 | 92 | }
|
93 | 93 | set_global_alignment(self, global, alloc.align);
|
94 | 94 |
|
95 |
| - global.global_set_initializer_rvalue(value); |
| 95 | + |
| 96 | + // TODO: if I still use this code, find the name of the variable in a better way (using |
| 97 | + // def_id). |
| 98 | + let var_name = format!("{:?}", global); |
| 99 | + if var_name.contains("FN") && var_name.contains("memchr") { |
| 100 | + println!("Var name: {:?}", var_name); |
| 101 | + |
| 102 | + let ptr_type = value.get_type().make_pointer(); |
| 103 | + |
| 104 | + // TODO: remove \x01 |
| 105 | + //let prefix = if self.cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" }; |
| 106 | + let prefix = "__imp__"; |
| 107 | + let mut imp_name = prefix.to_string(); |
| 108 | + imp_name.push_str(&var_name); |
| 109 | + |
| 110 | + // FIXME: if I understand correctly the code in cg_llvm, the kind should be Imported. |
| 111 | + /*let imp_global = self.context.new_global(None, GlobalKind::Exported, ptr_type, &imp_name); |
| 112 | + imp_global.global_set_initializer_rvalue(global.get_address(None));*/ |
| 113 | + |
| 114 | + /* |
| 115 | + /*let context = gccjit::Context::default(); |
| 116 | + let global = context.new_global(None, GlobalKind::Exported, val_llty, &var_name); |
| 117 | + global.global_set_initializer_rvalue(value); |
| 118 | + context.compile_to_file(gccjit::OutputKind::ObjectFile, format!("{}.o", var_name));*/ |
| 119 | +
|
| 120 | + let void_type = self.context.new_type::<()>(); |
| 121 | + let fn_ptr_type = self.context.new_function_pointer_type(None, void_type, &[], false); |
| 122 | + let my_name = format!("MY_NAME${}", var_name); |
| 123 | + //let global = self.context.new_global(None, GlobalKind::Exported, fn_ptr_type, my_name); |
| 124 | + //global.add_attribute(VarAttribute::Used); |
| 125 | + println!("{:?} = {:?}", var_name, value); |
| 126 | +
|
| 127 | + let my_func_name = format!("MY_FUNC${}", var_name); |
| 128 | + let func = self.context.new_function(None, FunctionType::Exported, void_type, &[], &my_func_name, false); |
| 129 | + func.add_attribute(FnAttribute::Used); |
| 130 | + let block = func.new_block("start"); |
| 131 | + block.end_with_void_return(None); |
| 132 | +
|
| 133 | + //let func = self.context.new_function(None, FunctionType::Extern, void_type, &[], "puts", false); |
| 134 | + let value = func.get_address(None); |
| 135 | + //let global = self.context.new_global(None, GlobalKind::Exported, value.get_type(), my_name); |
| 136 | + //let value = self.context.new_bitcast(None, func.get_address(None), fn_ptr_type); |
| 137 | + /* |
| 138 | + * TODO: Check if the hard-coded function has the correct name. |
| 139 | + * ===> It seems so. |
| 140 | + * TODO: try with a function we know exists. |
| 141 | + * ===> It doesn't seem to help. |
| 142 | + * TODO: check if the .o contains the value (before linking into the .so). |
| 143 | + * ===> It seems the object file doesn't contain the value either. |
| 144 | + * ======> This is because there are relocations. |
| 145 | + * TODO: check if fold in GCC erases the value. |
| 146 | + * ===> It doesn't seem so. |
| 147 | + * |
| 148 | + * TODO TODO: try again this code with using the used attribute. |
| 149 | + */ |
| 150 | +
|
| 151 | + /*let var_type = global.to_rvalue().get_type(); |
| 152 | + let struct_type = var_type.is_struct().unwrap(); |
| 153 | + /*let field1_type = struct_type.get_field(0).get_type(); |
| 154 | + let field2_type = struct_type.get_field(1).get_type();*/ |
| 155 | +
|
| 156 | + let field1 = value; |
| 157 | + let field2 = self.context.new_rvalue_zero(self.int_type); |
| 158 | +
|
| 159 | + let struct_val = self.context.new_struct_constructor(None, var_type, None, &[field1, field2]); |
| 160 | + let value = struct_val;*/ |
| 161 | +
|
| 162 | + //let value = self.context.new_bitcast(None, func.get_address(None), val_llty); |
| 163 | +
|
| 164 | + //let value = self.context.new_rvalue_from_int(self.usize_type, 10293); |
| 165 | + //let value = self.context.new_cast(None, value, fn_ptr_type); // WORKS |
| 166 | + //let value = self.context.new_bitcast(None, value, fn_ptr_type); // Also WORKS |
| 167 | + let value = self.context.new_bitcast(None, value, val_llty);*/ |
| 168 | + global.global_set_initializer_rvalue(value); |
| 169 | + } |
| 170 | + else { |
| 171 | + global.global_set_initializer_rvalue(value); |
| 172 | + } |
96 | 173 |
|
97 | 174 | // As an optimization, all shared statics which do not have interior
|
98 | 175 | // mutability are placed into read-only memory.
|
|
0 commit comments