Skip to content

Commit

Permalink
WIP: fix endianness of non-native 128-bit integers
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Oct 19, 2023
1 parent 07f2116 commit fcbb5a6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/m68k.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,8 @@ jobs:
- name: Build
run: |
./y.sh prepare --only-libcore
# TODO: move into prepare command under a flag --cross-patches?
pushd build_sysroot/sysroot_src
git am ../../cross_patches/*.patch
popd
./build.sh
./y.sh prepare --only-libcore --cross
./y.sh build --target-triple m68k-unknown-linux-gnu
cargo test
./clean_all.sh
Expand Down
26 changes: 17 additions & 9 deletions build_system/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::Path;
struct BuildArg {
codegen_release_channel: bool,
sysroot_release_channel: bool,
features: Vec<String>,
flags: Vec<String>,
gcc_path: String,
}

Expand All @@ -30,12 +30,12 @@ impl BuildArg {
"--release" => build_arg.codegen_release_channel = true,
"--release-sysroot" => build_arg.sysroot_release_channel = true,
"--no-default-features" => {
build_arg.features.push("--no-default-features".to_string());
build_arg.flags.push("--no-default-features".to_string());
}
"--features" => {
if let Some(arg) = args.next() {
build_arg.features.push("--features".to_string());
build_arg.features.push(arg.as_str().into());
build_arg.flags.push("--features".to_string());
build_arg.flags.push(arg.as_str().into());
} else {
return Err(
"Expected a value after `--features`, found nothing".to_string()
Expand All @@ -46,6 +46,15 @@ impl BuildArg {
Self::usage();
return Ok(None);
}
"--target-triple" => {
if let Some(arg) = args.next() {
// Handled in config.rs.
} else {
return Err(
"Expected a value after `--target-triple`, found nothing".to_string()
);
}
}
arg => return Err(format!("Unknown argument `{}`", arg)),
}
}
Expand All @@ -61,6 +70,7 @@ impl BuildArg {
--release-sysroot : Build sysroot in release mode
--no-default-features : Add `--no-default-features` flag
--features [arg] : Add a new feature [arg]
--target-triple [arg] : Set the target triple to [arg]
--help : Show this help
"#
)
Expand Down Expand Up @@ -147,8 +157,6 @@ fn build_sysroot(
&"build",
&"--target",
&target_triple,
&"--features",
&"compiler_builtins/c",
],
None,
Some(env),
Expand Down Expand Up @@ -196,9 +204,9 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
} else {
env.insert("CHANNEL".to_string(), "debug".to_string());
}
let ref_features = args.features.iter().map(|s| s.as_str()).collect::<Vec<_>>();
for feature in &ref_features {
command.push(feature);
let flags = args.flags.iter().map(|s| s.as_str()).collect::<Vec<_>>();
for flag in &flags {
command.push(flag);
}
run_command_with_output_and_env(&command, None, Some(&env))?;

Expand Down
27 changes: 22 additions & 5 deletions build_system/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,32 @@ pub fn set_config(
};
let host_triple = get_rustc_host_triple()?;
let mut linker = None;
let mut target_triple = host_triple.as_str();
let mut target_triple = host_triple.clone();
let mut run_wrapper = None;
// FIXME: handle this with a command line flag?
// let mut target_triple = "m68k-unknown-linux-gnu";

// We skip binary name and the command.
let mut args = std::env::args().skip(2);

while let Some(arg) = args.next() {
match arg.as_str() {
"--target-triple" => {
if let Some(arg) = args.next() {
target_triple = arg;
} else {
return Err(
"Expected a value after `--target-triple`, found nothing".to_string()
);
}
},
_ => (),
}
}

if host_triple != target_triple {
if target_triple == "m68k-unknown-linux-gnu" {
target_triple = "mips-unknown-linux-gnu";
linker = Some("-Clinker=m68k-linux-gcc");
//target_triple = "mips-unknown-linux-gnu";
// TODO: only add "-gcc" to the target triple instead of having these conditions?
linker = Some("-Clinker=m68k-unknown-linux-gnu-gcc");
} else if target_triple == "aarch64-unknown-linux-gnu" {
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
linker = Some("-Clinker=aarch64-linux-gnu-gcc");
Expand Down
12 changes: 8 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDat
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};

use crate::callee::get_fn;
use crate::common::SignType;

#[derive(Clone)]
pub struct FuncSig<'gcc> {
Expand Down Expand Up @@ -187,8 +188,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
let ulonglong_type = context.new_c_type(CType::ULongLong);
let sizet_type = context.new_c_type(CType::SizeT);

let isize_type = context.new_c_type(CType::Int);
let usize_type = context.new_c_type(CType::UInt);
let usize_type = sizet_type;
let isize_type = usize_type;
let bool_type = context.new_type::<bool>();

// TODO(antoyo): only have those assertions on x86_64.
Expand All @@ -212,7 +213,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
functions.insert(builtin.to_string(), context.get_builtin_function(builtin));
}

Self {
let mut cx = Self {
check_overflow,
codegen_unit,
context,
Expand Down Expand Up @@ -274,7 +275,10 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
pointee_infos: Default::default(),
structs_as_pointer: Default::default(),
cleanup_blocks: Default::default(),
}
};
// TODO(antoyo): instead of doing this, add SsizeT to libgccjit.
cx.isize_type = usize_type.to_signed(&cx);
cx
}

pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
Expand Down
11 changes: 9 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use gccjit::{ComparisonOp, FunctionType, RValue, ToRValue, Type, UnaryOp, Binary
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeMethods, BuilderMethods, OverflowOp};
use rustc_middle::ty::Ty;
use rustc_target::abi::Endian;

use crate::builder::ToGccComp;
use crate::{builder::Builder, common::{SignType, TypeReflection}, context::CodegenCx};
Expand Down Expand Up @@ -792,10 +793,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}

fn from_low_high(&self, typ: Type<'gcc>, low: i64, high: i64) -> RValue<'gcc> {
let (first, last) =
match self.sess().target.options.endian {
Endian::Little => (low, high),
Endian::Big => (high, low),
};

let native_int_type = typ.dyncast_array().expect("get element type");
let values = [
self.context.new_rvalue_from_long(native_int_type, low),
self.context.new_rvalue_from_long(native_int_type, high),
self.context.new_rvalue_from_long(native_int_type, first),
self.context.new_rvalue_from_long(native_int_type, last),
];
self.context.new_array_constructor(None, typ, &values)
}
Expand Down
6 changes: 4 additions & 2 deletions tests/lang_tests_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ pub fn main_inner(profile: Profile) {
}
// Test command 2: run `tempdir/x`.
let vm_parent_dir = "/home/bouanto/Ordinateur/VirtualMachines";
let vm_dir = "debian-m68k-root-img/";
let vm_dir = "debian-m68k-root/";
let exe_filename = exe.file_name().unwrap();
let vm_exe_path = PathBuf::from(vm_parent_dir).join(vm_dir).join("home").join(exe_filename);
let vm_home_dir = PathBuf::from(vm_parent_dir).join(vm_dir).join("home");
let _ = std::fs::create_dir_all(&vm_home_dir);
let vm_exe_path = vm_home_dir.join(exe_filename);
// FIXME: panicking here makes the test pass.
let inside_vm_exe_path = PathBuf::from("/home").join(&exe_filename);
let mut copy = Command::new("sudo");
Expand Down

0 comments on commit fcbb5a6

Please sign in to comment.