Skip to content

Commit

Permalink
Supports custom targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Oct 5, 2023
1 parent e4af51d commit acd5581
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 127 deletions.
1 change: 1 addition & 0 deletions stage0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ clap = { version = "4.4", features = ["cargo"] }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
toml = "0.8"
uuid = "1.4"
46 changes: 16 additions & 30 deletions stage0/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::ffi::{
llvm_target_lookup,
};
use crate::lexer::SyntaxError;
use crate::pkg::{ExportedType, OperatingSystem, PackageMeta, PackageName, PackageVersion, Target};
use crate::pkg::{
ExportedType, PackageMeta, PackageName, PackageVersion, PrimitiveTarget, TargetOs,
};
use std::error::Error;
use std::ffi::{CStr, CString};
use std::fmt::{Display, Formatter, Write};
Expand All @@ -34,7 +36,7 @@ pub struct Codegen<'a> {
machine: *mut crate::ffi::LlvmMachine,
pkg: &'a PackageName,
version: &'a PackageVersion,
target: &'a Target,
target: &'static PrimitiveTarget,
namespace: &'a str,
resolver: &'a TypeResolver<'a>,
}
Expand All @@ -43,11 +45,11 @@ impl<'a> Codegen<'a> {
pub fn new(
pkg: &'a PackageName,
version: &'a PackageVersion,
target: &'a Target,
target: &'static PrimitiveTarget,
resolver: &'a TypeResolver<'a>,
) -> Self {
// Get LLVM target.
let triple = CString::new(target.to_llvm()).unwrap();
let triple = CString::new(target.to_string()).unwrap();
let llvm = {
let mut err = String::new();
let ptr = unsafe { llvm_target_lookup(triple.as_ptr(), &mut err) };
Expand Down Expand Up @@ -104,17 +106,13 @@ impl<'a> Codegen<'a> {
Some(Expression::NotEqual(f, s)) => (false, f.span() + s.span()),
Some(Expression::Equal(f, s)) => (true, f.span() + s.span()),
Some(e) => return Err(SyntaxError::new(e.span(), "unsupported expression")),
None => match lhs.value() {
"unix" => match os {
OperatingSystem::Darwin | OperatingSystem::Linux => return Ok(true),
OperatingSystem::Win32 => return Ok(false),
},
"win32" => match os {
OperatingSystem::Darwin | OperatingSystem::Linux => return Ok(false),
OperatingSystem::Win32 => return Ok(true),
},
_ => return Err(SyntaxError::new(lhs.span().clone(), "unknown argument")),
},
None => {
return Ok(if lhs.value() == "unix" {
os.is_unix()
} else {
lhs.value() == os.name()
})
}
};

// Check if first expression is "os".
Expand All @@ -131,21 +129,9 @@ impl<'a> Codegen<'a> {

// Compare.
let res = if equal {
match rhs.value() {
"windows" => match os {
OperatingSystem::Darwin | OperatingSystem::Linux => false,
OperatingSystem::Win32 => true,
},
_ => todo!(),
}
rhs.value() == os.name()
} else {
match rhs.value() {
"windows" => match os {
OperatingSystem::Darwin | OperatingSystem::Linux => true,
OperatingSystem::Win32 => false,
},
_ => todo!(),
}
rhs.value() != os.name()
};

if expr.next().is_some() {
Expand Down Expand Up @@ -270,7 +256,7 @@ impl<'a> Codegen<'a> {

pub fn build<F: AsRef<std::path::Path>>(self, file: F, exe: bool) -> Result<(), BuildError> {
// Generate DllMain for DLL on Windows.
if self.target.os() == OperatingSystem::Win32 && !exe {
if self.target.os() == TargetOs::Win32 && !exe {
self.build_dll_main()?;
}

Expand Down
34 changes: 19 additions & 15 deletions stage0/src/pkg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub struct Package {
}

impl Package {
const META_END: u8 = 0;
const META_NAME: u8 = 1;
const META_VERSION: u8 = 2;
const META_DATE: u8 = 3;
const ENTRY_END: u8 = 0;
const ENTRY_NAME: u8 = 1;
const ENTRY_VERSION: u8 = 2;
const ENTRY_DATE: u8 = 3;
const ENTRY_LIB: u8 = 4;

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, PackageOpenError> {
todo!()
Expand All @@ -54,35 +55,32 @@ impl Package {
};

// Write file magic.
file.write_all(b"\x7FNPK")
.map_err(|e| PackagePackError::WriteFailed(e))?;
file.write_all(b"\x7FNPK")?;

// Write package name.
let meta = &self.meta;

file.write_all(&[Self::META_NAME]).unwrap();
file.write_all(&meta.name().to_bin()).unwrap();
file.write_all(&[Self::ENTRY_NAME])?;
file.write_all(&meta.name().to_bin())?;

// Write package version.
file.write_all(&[Self::META_VERSION]).unwrap();
file.write_all(&meta.version().to_bin().to_be_bytes())
.unwrap();
file.write_all(&[Self::ENTRY_VERSION])?;
file.write_all(&meta.version().to_bin().to_be_bytes())?;

// Write created date.
let date = SystemTime::now();

file.write_all(&[Self::META_DATE]).unwrap();
file.write_all(&[Self::ENTRY_DATE])?;
file.write_all(
&date
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
.to_be_bytes(),
)
.unwrap();
)?;

// End of meta data.
file.write_all(&[Self::META_END]).unwrap();
file.write_all(&[Self::ENTRY_END])?;

Ok(())
}
Expand Down Expand Up @@ -117,6 +115,12 @@ pub enum PackagePackError {
WriteFailed(#[source] std::io::Error),
}

impl From<std::io::Error> for PackagePackError {
fn from(value: std::io::Error) -> Self {
Self::WriteFailed(value)
}
}

/// Represents an error when a package is failed to export.
#[derive(Debug, Error)]
pub enum PackageExportError {}
Expand Down
Loading

0 comments on commit acd5581

Please sign in to comment.