2024-09-13 07:30:23 -05:00
|
|
|
pub use self::reg::{RET_ADDR, STACK_PTR, ZERO};
|
2024-07-02 07:49:05 -05:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
ident::{self, Ident},
|
|
|
|
instrs::{self, *},
|
2024-09-13 08:12:20 -05:00
|
|
|
lexer::TokenKind,
|
2024-09-14 05:27:53 -05:00
|
|
|
parser::{
|
|
|
|
self, find_symbol, idfl, CommentOr, CtorField, Expr, ExprRef, FileId, Pos, StructField,
|
|
|
|
},
|
2024-10-13 07:11:17 -05:00
|
|
|
ty::{self, TyCheck},
|
|
|
|
Field, Func, Global, LoggedMem, OffsetIter, ParamAlloc, Reloc, Sig, Struct, SymKey,
|
2024-10-12 06:07:49 -05:00
|
|
|
TypedReloc, Types, HEADER_SIZE,
|
2024-07-02 07:49:05 -05:00
|
|
|
},
|
2024-10-01 08:28:18 -05:00
|
|
|
alloc::{boxed::Box, string::String, vec::Vec},
|
2024-10-13 13:01:18 -05:00
|
|
|
core::fmt::Display,
|
2024-05-17 12:53:59 -05:00
|
|
|
};
|
2024-05-12 04:52:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
type Offset = u32;
|
|
|
|
type Size = u32;
|
2024-07-08 11:08:58 -05:00
|
|
|
type ArrayLen = u32;
|
2024-05-12 04:52:58 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn load_value(ptr: *const u8, size: u32) -> u64 {
|
|
|
|
let mut dst = [0u8; 8];
|
2024-09-30 12:09:17 -05:00
|
|
|
dst[..size as usize]
|
|
|
|
.copy_from_slice(unsafe { core::slice::from_raw_parts(ptr, size as usize) });
|
2024-07-19 05:00:55 -05:00
|
|
|
u64::from_ne_bytes(dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_loaded(value: CtValue, derefed: bool, size: u32) -> u64 {
|
|
|
|
if derefed {
|
|
|
|
load_value(value.0 as *const u8, size)
|
|
|
|
} else {
|
|
|
|
value.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
mod stack {
|
2024-07-08 00:22:53 -05:00
|
|
|
use {
|
|
|
|
super::{Offset, Size},
|
2024-09-30 12:09:17 -05:00
|
|
|
alloc::vec::Vec,
|
|
|
|
core::num::NonZeroU32,
|
2024-07-08 00:22:53 -05:00
|
|
|
};
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
impl crate::Reloc {
|
|
|
|
pub fn pack_srel(id: &Id, off: u32) -> u64 {
|
|
|
|
((id.repr() as u64) << 32) | (off as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_stack_offset(&self, code: &mut [u8], stack: &Alloc) {
|
|
|
|
let bytes =
|
|
|
|
&code[self.offset as usize + self.sub_offset as usize..][..self.width as usize];
|
|
|
|
let (id, off) = Self::unpack_srel(u64::from_ne_bytes(bytes.try_into().unwrap()));
|
|
|
|
self.write_offset(code, stack.final_offset(id, off) as i64);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unpack_srel(id: u64) -> (u32, u32) {
|
|
|
|
((id >> 32) as u32, id as u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct Id(NonZeroU32);
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Id {
|
|
|
|
fn index(&self) -> usize {
|
|
|
|
(self.0.get() as usize - 1) & !(1 << 31)
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn repr(&self) -> u32 {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ref(&self) -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(self.0.get() | 1 << 31) })
|
|
|
|
}
|
2024-05-12 04:52:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn is_ref(&self) -> bool {
|
|
|
|
self.0.get() & (1 << 31) != 0
|
|
|
|
}
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Drop for Id {
|
|
|
|
fn drop(&mut self) {
|
2024-09-30 12:09:17 -05:00
|
|
|
let is_panicking = {
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
{
|
|
|
|
std::thread::panicking()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !is_panicking && !self.is_ref() {
|
2024-06-23 02:09:33 -05:00
|
|
|
unreachable!("stack id leaked: {:?}", self.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-14 08:03:36 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
struct Meta {
|
2024-07-08 00:22:53 -05:00
|
|
|
size: Size,
|
2024-06-23 02:09:33 -05:00
|
|
|
offset: Offset,
|
2024-07-08 00:22:53 -05:00
|
|
|
rc: u32,
|
2024-05-14 08:03:36 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Alloc {
|
|
|
|
height: Size,
|
|
|
|
pub max_height: Size,
|
|
|
|
meta: Vec<Meta>,
|
2024-05-14 08:03:36 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Alloc {
|
|
|
|
pub fn allocate(&mut self, size: Size) -> Id {
|
2024-07-08 00:22:53 -05:00
|
|
|
self.meta.push(Meta { size, offset: 0, rc: 1 });
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
self.height += size;
|
|
|
|
self.max_height = self.max_height.max(self.height);
|
|
|
|
|
|
|
|
Id(unsafe { NonZeroU32::new_unchecked(self.meta.len() as u32) })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free(&mut self, id: Id) {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::forget(id);
|
2024-09-20 05:03:24 -05:00
|
|
|
//if id.is_ref() {}
|
|
|
|
//let meta = &mut self.meta[id.index()];
|
|
|
|
//meta.rc -= 1;
|
|
|
|
//if meta.rc != 0 {
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
//meta.offset = self.height;
|
|
|
|
//self.height -= meta.size;
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
pub fn dup_id(&mut self, id: &Id) -> Id {
|
|
|
|
if id.is_ref() {
|
|
|
|
return id.as_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.meta[id.index()].rc += 1;
|
|
|
|
Id(id.0)
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn finalize_leaked(&mut self) {
|
|
|
|
for meta in self.meta.iter_mut().filter(|m| m.rc > 0) {
|
|
|
|
meta.offset = self.height;
|
|
|
|
self.height -= meta.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.height = 0;
|
|
|
|
self.max_height = 0;
|
|
|
|
self.meta.clear();
|
|
|
|
}
|
2024-05-14 08:03:36 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn final_offset(&self, id: u32, extra_offset: Offset) -> Offset {
|
|
|
|
debug_assert_ne!(id, 0);
|
|
|
|
(self.max_height - self.meta[(id as usize - 1) & !(1 << 31)].offset) + extra_offset
|
|
|
|
}
|
2024-05-14 08:03:36 -05:00
|
|
|
}
|
2024-05-13 04:05:35 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
mod reg {
|
2024-09-30 12:09:17 -05:00
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub const STACK_PTR: Reg = 254;
|
|
|
|
pub const ZERO: Reg = 0;
|
|
|
|
pub const RET: Reg = 1;
|
|
|
|
pub const RET_ADDR: Reg = 31;
|
|
|
|
|
|
|
|
type Reg = u8;
|
|
|
|
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
2024-09-13 11:22:27 -05:00
|
|
|
type Bt = std::backtrace::Backtrace;
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(not(all(debug_assertions, feature = "std")))]
|
2024-09-13 11:22:27 -05:00
|
|
|
type Bt = ();
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct Id(Reg, Option<Bt>);
|
|
|
|
|
|
|
|
impl PartialEq for Id {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Id {}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
impl Id {
|
2024-09-13 11:22:27 -05:00
|
|
|
pub const RET: Self = Id(RET, None);
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
pub fn get(&self) -> Reg {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ref(&self) -> Self {
|
2024-09-13 11:22:27 -05:00
|
|
|
Self(self.0, None)
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_ref(&self) -> bool {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.1.is_none()
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl From<u8> for Id {
|
|
|
|
fn from(value: u8) -> Self {
|
2024-09-13 11:22:27 -05:00
|
|
|
Self(value, None)
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Drop for Id {
|
|
|
|
fn drop(&mut self) {
|
2024-09-30 12:09:17 -05:00
|
|
|
let is_panicking = {
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
2024-09-30 12:09:17 -05:00
|
|
|
{
|
|
|
|
std::thread::panicking()
|
|
|
|
}
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(not(all(debug_assertions, feature = "std")))]
|
2024-09-30 12:09:17 -05:00
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !is_panicking && let Some(bt) = self.1.take() {
|
2024-09-13 11:22:27 -05:00
|
|
|
unreachable!("reg id leaked: {:?} {bt}", self.0);
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
2024-05-13 04:05:35 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Default, PartialEq, Eq)]
|
|
|
|
pub struct Alloc {
|
2024-07-08 00:22:53 -05:00
|
|
|
free: Vec<Reg>,
|
2024-06-23 02:09:33 -05:00
|
|
|
max_used: Reg,
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Alloc {
|
|
|
|
pub fn init(&mut self) {
|
|
|
|
self.free.clear();
|
|
|
|
self.free.extend((32..=253).rev());
|
|
|
|
self.max_used = RET_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allocate(&mut self) -> Id {
|
|
|
|
let reg = self.free.pop().expect("TODO: we need to spill");
|
|
|
|
self.max_used = self.max_used.max(reg);
|
2024-09-13 11:22:27 -05:00
|
|
|
Id(
|
|
|
|
reg,
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
2024-09-13 11:22:27 -05:00
|
|
|
Some(std::backtrace::Backtrace::capture()),
|
2024-10-01 08:28:18 -05:00
|
|
|
#[cfg(not(all(debug_assertions, feature = "std")))]
|
2024-09-13 11:22:27 -05:00
|
|
|
Some(()),
|
|
|
|
)
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
2024-10-13 05:25:12 -05:00
|
|
|
pub fn free(&mut self, mut reg: Id) {
|
|
|
|
if reg.1.take().is_some() {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.free.push(reg.0);
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::forget(reg);
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn pushed_size(&self) -> usize {
|
|
|
|
((self.max_used as usize).saturating_sub(RET_ADDR as usize) + 1) * 8
|
|
|
|
}
|
2024-05-13 04:05:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
struct Value {
|
2024-07-08 00:22:53 -05:00
|
|
|
ty: ty::Id,
|
2024-06-23 02:09:33 -05:00
|
|
|
loc: Loc,
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Value {
|
|
|
|
fn new(ty: impl Into<ty::Id>, loc: impl Into<Loc>) -> Self {
|
2024-07-08 00:22:53 -05:00
|
|
|
Self { ty: ty.into(), loc: loc.into() }
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn void() -> Self {
|
2024-09-30 12:09:17 -05:00
|
|
|
Self { ty: ty::Id::VOID, loc: Loc::ct(0) }
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn imm(value: u64) -> Self {
|
2024-09-30 12:09:17 -05:00
|
|
|
Self { ty: ty::Id::UINT, loc: Loc::ct(value) }
|
2024-05-11 05:51:32 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn ty(ty: ty::Id) -> Self {
|
2024-09-30 12:09:17 -05:00
|
|
|
Self { ty: ty::Id::TYPE, loc: Loc::ct(ty.repr() as u64) }
|
2024-05-13 06:36:29 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum LocCow<'a> {
|
|
|
|
Ref(&'a Loc),
|
|
|
|
Owned(Loc),
|
|
|
|
}
|
2024-05-13 06:36:29 -05:00
|
|
|
|
2024-10-12 08:04:58 -05:00
|
|
|
impl LocCow<'_> {
|
2024-06-23 02:09:33 -05:00
|
|
|
fn as_ref(&self) -> &Loc {
|
|
|
|
match self {
|
|
|
|
Self::Ref(value) => value,
|
|
|
|
Self::Owned(value) => value,
|
2024-05-13 06:36:29 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl<'a> From<&'a Loc> for LocCow<'a> {
|
|
|
|
fn from(value: &'a Loc) -> Self {
|
|
|
|
Self::Ref(value)
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
|
2024-10-12 08:04:58 -05:00
|
|
|
impl From<Loc> for LocCow<'_> {
|
2024-06-23 02:09:33 -05:00
|
|
|
fn from(value: Loc) -> Self {
|
|
|
|
Self::Owned(value)
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
struct CtValue(u64);
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2024-07-19 05:00:55 -05:00
|
|
|
enum Loc {
|
2024-07-08 00:22:53 -05:00
|
|
|
Rt { derefed: bool, reg: reg::Id, stack: Option<stack::Id>, offset: Offset },
|
2024-07-19 05:00:55 -05:00
|
|
|
Ct { derefed: bool, value: CtValue },
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Loc {
|
|
|
|
fn stack(stack: stack::Id) -> Self {
|
2024-07-08 00:22:53 -05:00
|
|
|
Self::Rt { stack: Some(stack), reg: reg::STACK_PTR.into(), derefed: true, offset: 0 }
|
2024-05-10 15:54:12 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn reg(reg: impl Into<reg::Id>) -> Self {
|
|
|
|
let reg = reg.into();
|
|
|
|
assert!(reg.get() != 0);
|
2024-07-08 00:22:53 -05:00
|
|
|
Self::Rt { derefed: false, reg, stack: None, offset: 0 }
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn ct(value: u64) -> Self {
|
|
|
|
Self::Ct { value: CtValue(value), derefed: false }
|
2024-07-06 16:02:04 -05:00
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn ct_ptr(value: u64) -> Self {
|
|
|
|
Self::Ct { value: CtValue(value), derefed: true }
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn ty(ty: ty::Id) -> Self {
|
2024-07-19 05:00:55 -05:00
|
|
|
Self::ct(ty.repr() as _)
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn offset(mut self, offset: u32) -> Self {
|
|
|
|
match &mut self {
|
|
|
|
Self::Rt { offset: off, .. } => *off += offset,
|
2024-07-19 05:00:55 -05:00
|
|
|
Self::Ct { derefed: false, value } => value.0 += offset as u64,
|
2024-06-23 02:09:33 -05:00
|
|
|
_ => unreachable!("offseting constant"),
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
self
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-05-10 14:33:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn as_ref(&self) -> Self {
|
|
|
|
match *self {
|
2024-07-08 00:22:53 -05:00
|
|
|
Loc::Rt { derefed, ref reg, ref stack, offset } => Loc::Rt {
|
2024-06-23 02:09:33 -05:00
|
|
|
derefed,
|
|
|
|
reg: reg.as_ref(),
|
|
|
|
stack: stack.as_ref().map(stack::Id::as_ref),
|
|
|
|
offset,
|
|
|
|
},
|
2024-07-19 05:00:55 -05:00
|
|
|
Loc::Ct { value, derefed } => Self::Ct { derefed, value },
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn into_derefed(mut self) -> Self {
|
|
|
|
match &mut self {
|
|
|
|
Self::Rt { derefed, .. } => *derefed = true,
|
2024-06-24 10:26:00 -05:00
|
|
|
val => unreachable!("{val:?}"),
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn assert_valid(&self) {
|
|
|
|
assert!(!matches!(self, Self::Rt { reg, .. } if reg.get() == 0));
|
2024-05-16 05:42:11 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn take_owned(&mut self) -> Option<Self> {
|
|
|
|
if self.is_ref() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
Some(core::mem::replace(self, self.as_ref()))
|
2024-05-10 08:29:11 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn is_ref(&self) -> bool {
|
|
|
|
matches!(self, Self::Rt { reg, stack, .. } if reg.is_ref() && stack.as_ref().map_or(true, stack::Id::is_ref))
|
2024-05-10 08:29:11 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
|
|
|
|
fn to_ty(&self) -> Option<ty::Id> {
|
|
|
|
match *self {
|
2024-07-19 05:00:55 -05:00
|
|
|
Self::Ct { derefed: false, value } => Some(ty::Id::from(value.0)),
|
|
|
|
Self::Ct { derefed: true, value } => {
|
2024-09-30 12:09:17 -05:00
|
|
|
Some(unsafe { core::ptr::read(value.0 as *const u8 as _) })
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
Self::Rt { .. } => None,
|
|
|
|
}
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
|
|
|
|
fn is_stack(&self) -> bool {
|
|
|
|
matches!(self, Self::Rt { derefed: true, reg, stack: Some(_), offset: 0 } if reg.get() == STACK_PTR)
|
|
|
|
}
|
2024-09-09 18:15:18 -05:00
|
|
|
|
|
|
|
fn is_reg(&self) -> bool {
|
|
|
|
matches!(self, Self::Rt { derefed: false, reg: _, stack: None, offset: 0 })
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl From<reg::Id> for Loc {
|
|
|
|
fn from(reg: reg::Id) -> Self {
|
|
|
|
Loc::reg(reg)
|
2024-05-10 08:29:11 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-12 13:10:50 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Default for Loc {
|
|
|
|
fn default() -> Self {
|
2024-07-19 05:00:55 -05:00
|
|
|
Self::ct(0)
|
2024-05-12 13:10:50 -05:00
|
|
|
}
|
2024-05-10 08:29:11 -05:00
|
|
|
}
|
2024-05-09 11:16:01 -05:00
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2024-06-23 02:09:33 -05:00
|
|
|
struct Loop {
|
2024-07-08 00:22:53 -05:00
|
|
|
var_count: u32,
|
|
|
|
offset: u32,
|
2024-06-23 02:09:33 -05:00
|
|
|
reloc_base: u32,
|
2024-05-10 14:33:42 -05:00
|
|
|
}
|
|
|
|
|
2024-05-12 04:52:58 -05:00
|
|
|
struct Variable {
|
2024-07-08 00:22:53 -05:00
|
|
|
id: Ident,
|
2024-05-12 04:52:58 -05:00
|
|
|
value: Value,
|
2024-05-11 05:51:32 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
struct ItemCtxSnap {
|
|
|
|
stack_relocs: usize,
|
|
|
|
ret_relocs: usize,
|
|
|
|
loop_relocs: usize,
|
|
|
|
code: usize,
|
|
|
|
relocs: usize,
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ItemCtx {
|
2024-07-08 00:22:53 -05:00
|
|
|
file: FileId,
|
|
|
|
id: ty::Kind,
|
2024-07-19 05:00:55 -05:00
|
|
|
ret: Option<ty::Id>,
|
2024-06-23 02:09:33 -05:00
|
|
|
ret_reg: reg::Id,
|
2024-09-01 20:21:39 -05:00
|
|
|
inline_ret_loc: Loc,
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
task_base: usize,
|
|
|
|
|
|
|
|
stack: stack::Alloc,
|
2024-07-08 00:22:53 -05:00
|
|
|
regs: reg::Alloc,
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
loops: Vec<Loop>,
|
|
|
|
vars: Vec<Variable>,
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
stack_relocs: Vec<Reloc>,
|
2024-07-08 00:22:53 -05:00
|
|
|
ret_relocs: Vec<Reloc>,
|
|
|
|
loop_relocs: Vec<Reloc>,
|
2024-09-13 11:22:27 -05:00
|
|
|
code: Vec<u8>,
|
|
|
|
relocs: Vec<TypedReloc>,
|
2024-05-11 09:04:13 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl ItemCtx {
|
2024-09-13 11:22:27 -05:00
|
|
|
fn write_trap(&mut self, kind: trap::Trap) {
|
|
|
|
self.emit(eca());
|
|
|
|
self.code.push(255);
|
|
|
|
self.code.extend(kind.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snap(&self) -> ItemCtxSnap {
|
|
|
|
ItemCtxSnap {
|
|
|
|
stack_relocs: self.stack_relocs.len(),
|
|
|
|
ret_relocs: self.ret_relocs.len(),
|
|
|
|
loop_relocs: self.loop_relocs.len(),
|
|
|
|
code: self.code.len(),
|
|
|
|
relocs: self.relocs.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn revert(&mut self, snap: ItemCtxSnap) {
|
|
|
|
self.stack_relocs.truncate(snap.stack_relocs);
|
|
|
|
self.ret_relocs.truncate(snap.ret_relocs);
|
|
|
|
self.loop_relocs.truncate(snap.loop_relocs);
|
|
|
|
self.code.truncate(snap.code);
|
|
|
|
self.relocs.truncate(snap.relocs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_addi(&mut self, dest: u8, op: u8, delta: u64) {
|
|
|
|
if delta == 0 {
|
|
|
|
if dest != op {
|
|
|
|
self.emit(cp(dest, op));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit(addi64(dest, op, delta));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit(&mut self, (len, instr): (usize, [u8; instrs::MAX_SIZE])) {
|
|
|
|
self.code.extend_from_slice(&instr[..len]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_prelude(&mut self) {
|
|
|
|
self.emit(instrs::addi64(STACK_PTR, STACK_PTR, 0));
|
|
|
|
self.emit(instrs::st(RET_ADDR, STACK_PTR, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_entry_prelude(&mut self) {
|
|
|
|
self.emit(jal(RET_ADDR, reg::ZERO, 0));
|
|
|
|
self.emit(tx());
|
|
|
|
}
|
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
pub fn dup_loc(&mut self, loc: &Loc) -> Loc {
|
|
|
|
match *loc {
|
|
|
|
Loc::Rt { derefed, ref reg, ref stack, offset } => Loc::Rt {
|
|
|
|
reg: reg.as_ref(),
|
|
|
|
derefed,
|
|
|
|
stack: stack.as_ref().map(|s| self.stack.dup_id(s)),
|
|
|
|
offset,
|
|
|
|
},
|
2024-07-19 05:00:55 -05:00
|
|
|
ref loc => loc.as_ref(),
|
2024-07-08 00:22:53 -05:00
|
|
|
}
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
fn finalize(&mut self) {
|
2024-09-01 19:38:11 -05:00
|
|
|
if let Some(last_ret) = self.ret_relocs.last()
|
2024-09-13 11:22:27 -05:00
|
|
|
&& last_ret.offset as usize == self.code.len() - 5
|
2024-09-01 19:38:11 -05:00
|
|
|
{
|
2024-09-13 11:22:27 -05:00
|
|
|
self.code.truncate(self.code.len() - 5);
|
2024-09-01 19:38:11 -05:00
|
|
|
self.ret_relocs.pop();
|
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let len = self.code.len() as Offset;
|
2024-09-01 19:38:11 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
self.stack.finalize_leaked();
|
|
|
|
for rel in self.stack_relocs.drain(..) {
|
2024-09-13 11:22:27 -05:00
|
|
|
rel.apply_stack_offset(&mut self.code, &self.stack)
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
for rel in self.ret_relocs.drain(..) {
|
2024-09-13 11:22:27 -05:00
|
|
|
let off = rel.apply_jump(&mut self.code, len, 0);
|
2024-09-03 19:35:09 -05:00
|
|
|
debug_assert!(off > 0);
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-11 15:22:08 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let pushed = self.regs.pushed_size() as i64;
|
|
|
|
let stack = self.stack.max_height as i64;
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
write_reloc(&mut self.code, 3, -(pushed + stack), 8);
|
|
|
|
write_reloc(&mut self.code, 3 + 8 + 3, stack, 8);
|
|
|
|
write_reloc(&mut self.code, 3 + 8 + 3 + 8, pushed, 2);
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.emit(instrs::ld(reg::RET_ADDR, reg::STACK_PTR, stack as _, pushed as _));
|
|
|
|
self.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, (pushed + stack) as _));
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.stack.clear();
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
debug_assert!(self.loops.is_empty());
|
|
|
|
debug_assert!(self.loop_relocs.is_empty());
|
|
|
|
debug_assert!(self.vars.is_empty());
|
2024-05-12 15:40:28 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn free_loc(&mut self, src: impl Into<LocCow>) {
|
|
|
|
if let LocCow::Owned(Loc::Rt { reg, stack, .. }) = src.into() {
|
|
|
|
self.regs.free(reg);
|
|
|
|
if let Some(stack) = stack {
|
|
|
|
self.stack.free(stack);
|
2024-05-20 07:11:58 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-12 15:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn write_reloc(doce: &mut [u8], offset: usize, value: i64, size: u16) {
|
|
|
|
let value = value.to_ne_bytes();
|
|
|
|
doce[offset..offset + size as usize].copy_from_slice(&value[..size as usize]);
|
2024-05-15 07:36:38 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
mod task {
|
|
|
|
use super::Offset;
|
|
|
|
|
|
|
|
pub fn unpack(offset: Offset) -> Result<Offset, usize> {
|
|
|
|
if offset >> 31 != 0 {
|
|
|
|
Err((offset & !(1 << 31)) as usize)
|
|
|
|
} else {
|
|
|
|
Ok(offset)
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn id(index: usize) -> Offset {
|
|
|
|
1 << 31 | index as u32
|
2024-05-12 15:40:28 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-12 15:40:28 -05:00
|
|
|
|
2024-09-08 10:11:33 -05:00
|
|
|
#[derive(Debug)]
|
2024-06-23 02:09:33 -05:00
|
|
|
struct FTask {
|
|
|
|
file: FileId,
|
2024-07-08 00:22:53 -05:00
|
|
|
id: ty::Func,
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct Ctx {
|
|
|
|
loc: Option<Loc>,
|
2024-07-08 00:22:53 -05:00
|
|
|
ty: Option<ty::Id>,
|
2024-10-13 07:11:17 -05:00
|
|
|
check: TyCheck,
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Ctx {
|
|
|
|
pub fn with_loc(self, loc: Loc) -> Self {
|
2024-07-08 00:22:53 -05:00
|
|
|
Self { loc: Some(loc), ..self }
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
pub fn with_ty(self, ty: impl Into<ty::Id>) -> Self {
|
2024-07-08 00:22:53 -05:00
|
|
|
Self { ty: Some(ty.into()), ..self }
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-10-13 07:11:17 -05:00
|
|
|
pub fn with_check(self, check: TyCheck) -> Self {
|
|
|
|
Self { check, ..self }
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn into_value(self) -> Option<Value> {
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty: self.ty.unwrap(), loc: self.loc? })
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl From<Value> for Ctx {
|
|
|
|
fn from(value: Value) -> Self {
|
2024-10-13 07:11:17 -05:00
|
|
|
Self { loc: Some(value.loc), ty: Some(value.ty), ..Default::default() }
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Pool {
|
2024-07-08 00:22:53 -05:00
|
|
|
cis: Vec<ItemCtx>,
|
2024-06-24 10:26:00 -05:00
|
|
|
arg_locs: Vec<Loc>,
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-10-10 06:04:17 -05:00
|
|
|
const VM_STACK_SIZE: usize = 1024 * 64;
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-10-12 06:07:49 -05:00
|
|
|
pub struct Comptime {
|
|
|
|
pub vm: hbvm::Vm<LoggedMem, { 1024 * 10 }>,
|
|
|
|
stack: Box<[u8; VM_STACK_SIZE]>,
|
2024-09-13 11:22:27 -05:00
|
|
|
code: Vec<u8>,
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
impl Comptime {
|
|
|
|
fn reset(&mut self) {
|
|
|
|
let ptr = unsafe { self.stack.as_mut_ptr().cast::<u8>().add(VM_STACK_SIZE) as u64 };
|
|
|
|
self.vm.registers.fill(hbvm::value::Value(0));
|
|
|
|
self.vm.write_reg(STACK_PTR, ptr);
|
|
|
|
self.vm.pc = hbvm::mem::Address::new(self.code.as_ptr() as u64 + HEADER_SIZE as u64);
|
|
|
|
}
|
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Default for Comptime {
|
|
|
|
fn default() -> Self {
|
|
|
|
let mut stack = Box::<[u8; VM_STACK_SIZE]>::new_uninit();
|
|
|
|
let mut vm = hbvm::Vm::default();
|
|
|
|
let ptr = unsafe { stack.as_mut_ptr().cast::<u8>().add(VM_STACK_SIZE) as u64 };
|
|
|
|
vm.write_reg(STACK_PTR, ptr);
|
2024-10-12 06:07:49 -05:00
|
|
|
Self { vm, stack: unsafe { stack.assume_init() }, code: Default::default() }
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-12 04:52:58 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
mod trap {
|
|
|
|
use {
|
|
|
|
super::ty,
|
|
|
|
crate::parser::{ExprRef, FileId},
|
|
|
|
};
|
|
|
|
|
|
|
|
macro_rules! gen_trap {
|
|
|
|
(
|
|
|
|
#[derive(Trap)]
|
|
|
|
$vis:vis enum $name:ident {
|
|
|
|
$($variant:ident {
|
|
|
|
$($fname:ident: $fty:ty,)*
|
|
|
|
},)*
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
#[repr(u8)]
|
|
|
|
$vis enum $name {
|
|
|
|
$($variant($variant),)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
$vis fn size(&self) -> usize {
|
|
|
|
1 + match self {
|
2024-09-30 12:09:17 -05:00
|
|
|
$(Self::$variant(_) => core::mem::size_of::<$variant>(),)*
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
|
|
|
#[repr(packed)]
|
|
|
|
$vis struct $variant {
|
|
|
|
$($vis $fname: $fty,)*
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_trap! {
|
|
|
|
#[derive(Trap)]
|
|
|
|
pub enum Trap {
|
|
|
|
MakeStruct {
|
|
|
|
file: FileId,
|
|
|
|
struct_expr: ExprRef,
|
|
|
|
},
|
|
|
|
MomizedCall {
|
|
|
|
func: ty::Func,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Trap {
|
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
2024-09-30 12:09:17 -05:00
|
|
|
unsafe { core::slice::from_raw_parts(self as *const _ as _, self.size()) }
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-13 04:05:35 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Codegen {
|
2024-07-07 06:42:48 -05:00
|
|
|
pub files: Vec<parser::Ast>,
|
2024-07-08 00:22:53 -05:00
|
|
|
tasks: Vec<Option<FTask>>,
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
tys: Types,
|
|
|
|
ci: ItemCtx,
|
|
|
|
pool: Pool,
|
|
|
|
ct: Comptime,
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
impl Codegen {
|
2024-10-13 08:33:57 -05:00
|
|
|
pub fn push_embeds(&mut self, embeds: Vec<Vec<u8>>) {
|
|
|
|
self.tys.ins.globals = embeds
|
|
|
|
.into_iter()
|
|
|
|
.map(|data| Global {
|
|
|
|
ty: self.tys.make_array(ty::Id::U8, data.len() as _),
|
|
|
|
data,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2024-10-13 05:25:12 -05:00
|
|
|
pub fn generate(&mut self, root: FileId) {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit_entry_prelude();
|
2024-10-13 05:25:12 -05:00
|
|
|
self.find_or_declare(0, root, Err("main"), "");
|
2024-09-01 19:38:11 -05:00
|
|
|
self.make_func_reachable(0);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.complete_call_graph();
|
2024-05-20 07:11:58 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn expr(&mut self, expr: &Expr) -> Option<Value> {
|
|
|
|
self.expr_ctx(expr, Ctx::default())
|
|
|
|
}
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-09-22 11:17:30 -05:00
|
|
|
fn build_struct(
|
|
|
|
&mut self,
|
2024-09-30 15:15:40 -05:00
|
|
|
file: FileId,
|
2024-10-01 10:43:15 -05:00
|
|
|
pos: Option<Pos>,
|
2024-09-30 12:09:17 -05:00
|
|
|
explicit_alignment: Option<u8>,
|
2024-09-22 11:17:30 -05:00
|
|
|
fields: &[CommentOr<StructField>],
|
|
|
|
) -> ty::Struct {
|
2024-10-01 10:43:15 -05:00
|
|
|
let sym = pos.map(|pos| SymKey::Struct(file, pos));
|
2024-10-01 15:53:03 -05:00
|
|
|
if let Some(sym) = sym
|
|
|
|
&& let Some(&ty) = self.tys.syms.get(sym, &self.tys.ins)
|
2024-10-01 10:43:15 -05:00
|
|
|
{
|
|
|
|
return ty.expand().inner();
|
|
|
|
}
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let prev_tmp = self.tys.tmp.fields.len();
|
2024-09-30 12:09:17 -05:00
|
|
|
for sf in fields.iter().filter_map(CommentOr::or) {
|
2024-10-01 14:33:30 -05:00
|
|
|
let f = Field { name: self.tys.names.intern(sf.name), ty: self.ty(&sf.ty) };
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.tmp.fields.push(f);
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
2024-10-01 14:36:23 -05:00
|
|
|
self.tys.ins.structs.push(Struct {
|
2024-10-01 14:39:23 -05:00
|
|
|
field_start: self.tys.ins.fields.len() as _,
|
2024-10-01 15:53:03 -05:00
|
|
|
pos: pos.unwrap_or(Pos::MAX),
|
2024-09-30 12:09:17 -05:00
|
|
|
explicit_alignment,
|
2024-09-30 15:15:40 -05:00
|
|
|
file,
|
2024-09-30 12:09:17 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.ins.fields.extend(self.tys.tmp.fields.drain(prev_tmp..));
|
2024-09-30 12:09:17 -05:00
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
if let Some(sym) = sym {
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.syms.insert(
|
|
|
|
sym,
|
|
|
|
ty::Kind::Struct(self.tys.ins.structs.len() as u32 - 1).compress(),
|
|
|
|
&self.tys.ins,
|
|
|
|
);
|
2024-10-01 10:43:15 -05:00
|
|
|
}
|
|
|
|
|
2024-10-01 14:36:23 -05:00
|
|
|
self.tys.ins.structs.len() as u32 - 1
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_ctx(&mut self, expr: &Expr, mut ctx: Ctx) -> Option<Value> {
|
|
|
|
use {Expr as E, TokenKind as T};
|
2024-05-13 04:05:35 -05:00
|
|
|
let value = match *expr {
|
2024-06-23 02:09:33 -05:00
|
|
|
E::Mod { id, .. } => Some(Value::ty(ty::Kind::Module(id).compress())),
|
2024-10-13 09:38:51 -05:00
|
|
|
E::Embed { id, .. } => self.handle_global(id),
|
2024-10-01 10:43:15 -05:00
|
|
|
E::Struct { captured, packed, fields, pos, .. } => {
|
2024-05-20 07:11:58 -05:00
|
|
|
if captured.is_empty() {
|
2024-10-01 07:00:41 -05:00
|
|
|
Some(Value::ty(
|
|
|
|
ty::Kind::Struct(self.build_struct(
|
|
|
|
self.ci.file,
|
2024-10-01 10:43:15 -05:00
|
|
|
Some(pos),
|
2024-10-01 07:00:41 -05:00
|
|
|
packed.then_some(1),
|
|
|
|
fields,
|
|
|
|
))
|
|
|
|
.compress(),
|
|
|
|
))
|
2024-05-20 07:11:58 -05:00
|
|
|
} else {
|
|
|
|
let values = captured
|
|
|
|
.iter()
|
2024-10-13 05:25:12 -05:00
|
|
|
.map(|&id| E::Ident { pos: 0, is_ct: false, id, is_first: false })
|
2024-05-20 07:11:58 -05:00
|
|
|
.map(|expr| self.expr(&expr))
|
|
|
|
.collect::<Option<Vec<_>>>()?;
|
2024-07-08 00:22:53 -05:00
|
|
|
let values_size =
|
|
|
|
values.iter().map(|value| 4 + self.tys.size_of(value.ty)).sum::<Size>();
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let stack = self.ci.stack.allocate(values_size);
|
|
|
|
let mut ptr = Loc::stack(stack.as_ref());
|
2024-05-20 07:11:58 -05:00
|
|
|
for value in values {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.store_sized(Loc::ty(value.ty), &ptr, 4);
|
|
|
|
ptr = ptr.offset(4);
|
|
|
|
let size = self.tys.size_of(value.ty);
|
|
|
|
self.store_sized(value.loc, &ptr, size);
|
|
|
|
ptr = ptr.offset(size);
|
2024-05-20 07:11:58 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 06:55:48 -05:00
|
|
|
self.stack_offset(2, STACK_PTR, Some(&stack), 0);
|
2024-06-23 02:09:33 -05:00
|
|
|
let val = self.eca(
|
2024-07-19 05:00:55 -05:00
|
|
|
trap::Trap::MakeStruct(trap::MakeStruct {
|
|
|
|
file: self.ci.file,
|
|
|
|
struct_expr: ExprRef::new(expr),
|
|
|
|
}),
|
2024-06-23 02:09:33 -05:00
|
|
|
ty::TYPE,
|
2024-05-20 07:11:58 -05:00
|
|
|
);
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.free_loc(Loc::stack(stack));
|
|
|
|
Some(val)
|
2024-05-20 07:11:58 -05:00
|
|
|
}
|
|
|
|
}
|
2024-07-08 11:08:58 -05:00
|
|
|
E::Slice { size, item, .. } => {
|
|
|
|
let ty = self.ty(item);
|
|
|
|
let len = size.map_or(ArrayLen::MAX, |expr| self.eval_const(expr, ty::U32) as _);
|
|
|
|
Some(Value::ty(self.tys.make_array(ty, len)))
|
|
|
|
}
|
|
|
|
E::Index { base, index } => {
|
|
|
|
// TODO: we need to check if index is in bounds on debug builds
|
|
|
|
|
|
|
|
let mut base_val = self.expr(base)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
if base_val.ty.is_pointer() {
|
|
|
|
base_val.loc = self.make_loc_owned(base_val.loc, base_val.ty);
|
|
|
|
}
|
2024-07-08 11:08:58 -05:00
|
|
|
let index_val = self.expr(index)?;
|
2024-10-13 07:11:17 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
index.pos(),
|
|
|
|
index_val.ty,
|
|
|
|
ty::Id::INT,
|
|
|
|
TyCheck::BinOp,
|
|
|
|
"subsctipt",
|
|
|
|
);
|
2024-07-08 11:08:58 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
if let Some(ty) = self.tys.base_of(base_val.ty) {
|
|
|
|
base_val.ty = ty;
|
2024-07-08 11:08:58 -05:00
|
|
|
base_val.loc = base_val.loc.into_derefed();
|
|
|
|
}
|
|
|
|
|
|
|
|
match base_val.ty.expand() {
|
|
|
|
ty::Kind::Slice(arr) => {
|
2024-10-01 14:36:23 -05:00
|
|
|
let ty = self.tys.ins.arrays[arr as usize].ty;
|
2024-07-08 11:08:58 -05:00
|
|
|
let item_size = self.tys.size_of(ty);
|
|
|
|
|
|
|
|
let Loc::Rt { derefed: true, ref mut reg, ref stack, offset } =
|
|
|
|
base_val.loc
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
if reg.is_ref() {
|
|
|
|
let new_reg = self.ci.regs.allocate();
|
|
|
|
self.stack_offset(new_reg.get(), reg.get(), stack.as_ref(), offset);
|
|
|
|
*reg = new_reg;
|
|
|
|
} else {
|
|
|
|
self.stack_offset(reg.get(), reg.get(), stack.as_ref(), offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
let idx = self.loc_to_reg(index_val.loc, 8);
|
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
if item_size != 1 {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(muli64(idx.get(), idx.get(), item_size as _));
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(add64(reg.get(), reg.get(), idx.get()));
|
2024-07-08 11:08:58 -05:00
|
|
|
self.ci.regs.free(idx);
|
|
|
|
|
|
|
|
Some(Value::new(ty, base_val.loc))
|
|
|
|
}
|
|
|
|
_ => self.report(
|
|
|
|
base.pos(),
|
|
|
|
format_args!(
|
|
|
|
"compiler did not (yet) learn how to index into '{}'",
|
|
|
|
self.ty_display(base_val.ty)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 19:38:11 -05:00
|
|
|
E::Directive { name: "inline", args: [func_ast, args @ ..], .. } => {
|
|
|
|
let ty::Kind::Func(mut func) = self.ty(func_ast).expand() else {
|
|
|
|
self.report(func_ast.pos(), "first argument of inline needs to be a function");
|
|
|
|
};
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &self.tys.ins.funcs[func as usize];
|
2024-10-12 15:03:53 -05:00
|
|
|
let ast = self.files[fuc.file as usize].clone();
|
2024-09-01 19:38:11 -05:00
|
|
|
let E::BinOp { right: &E::Closure { args: cargs, body, .. }, .. } =
|
2024-10-12 15:03:53 -05:00
|
|
|
fuc.expr.get(&ast).unwrap()
|
2024-09-01 19:38:11 -05:00
|
|
|
else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
|
|
|
let scope = self.ci.vars.len();
|
|
|
|
let sig = self.compute_signature(&mut func, func_ast.pos(), args)?;
|
2024-10-12 15:29:52 -05:00
|
|
|
self.ci.vars.truncate(scope);
|
2024-09-01 19:38:11 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
self.assert_arg_count(expr.pos(), args.len(), cargs.len(), "inline function call");
|
|
|
|
|
2024-10-12 15:03:53 -05:00
|
|
|
let mut sig_args = sig.args.range();
|
|
|
|
for (arg, carg) in args.iter().zip(cargs) {
|
|
|
|
let ty = self.tys.ins.args[sig_args.next().unwrap()];
|
|
|
|
let sym = parser::find_symbol(&ast.symbols, carg.id);
|
2024-10-12 15:29:52 -05:00
|
|
|
let loc = match sym.flags & idfl::COMPTIME != 0 {
|
|
|
|
true => Loc::ty(self.tys.ins.args[sig_args.next().unwrap()]),
|
|
|
|
false => self.expr_ctx(arg, Ctx::default().with_ty(ty))?.loc,
|
|
|
|
};
|
2024-10-12 15:03:53 -05:00
|
|
|
self.ci.vars.push(Variable { id: carg.id, value: Value { ty, loc } });
|
2024-09-01 19:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let ret_reloc_base = self.ci.ret_relocs.len();
|
|
|
|
|
2024-09-01 20:21:39 -05:00
|
|
|
let loc = self.alloc_ret(sig.ret, ctx, true);
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_ret_reg = core::mem::replace(&mut self.ci.inline_ret_loc, loc);
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &self.tys.ins.funcs[func as usize];
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_file = core::mem::replace(&mut self.ci.file, fuc.file);
|
|
|
|
let prev_ret = core::mem::replace(&mut self.ci.ret, Some(sig.ret));
|
2024-09-01 19:38:11 -05:00
|
|
|
self.expr(body);
|
2024-09-30 12:09:17 -05:00
|
|
|
let loc = core::mem::replace(&mut self.ci.inline_ret_loc, prev_ret_reg);
|
2024-09-01 20:37:49 -05:00
|
|
|
self.ci.file = prev_file;
|
|
|
|
self.ci.ret = prev_ret;
|
2024-09-01 20:21:39 -05:00
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let mut vars = core::mem::take(&mut self.ci.vars);
|
2024-10-01 08:28:18 -05:00
|
|
|
for var in vars.drain(scope..) {
|
2024-09-01 20:56:22 -05:00
|
|
|
self.ci.free_loc(var.value.loc);
|
|
|
|
}
|
2024-10-01 08:28:18 -05:00
|
|
|
self.ci.vars = vars;
|
2024-09-01 20:56:22 -05:00
|
|
|
|
2024-09-01 20:21:39 -05:00
|
|
|
if let Some(last_ret) = self.ci.ret_relocs.last()
|
2024-09-13 11:22:27 -05:00
|
|
|
&& last_ret.offset as usize == self.ci.code.len() - 5
|
2024-09-01 20:21:39 -05:00
|
|
|
{
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.code.truncate(self.ci.code.len() - 5);
|
2024-09-01 20:21:39 -05:00
|
|
|
self.ci.ret_relocs.pop();
|
|
|
|
}
|
2024-09-13 11:22:27 -05:00
|
|
|
let len = self.ci.code.len() as u32;
|
2024-09-03 19:35:09 -05:00
|
|
|
for rel in self.ci.ret_relocs.drain(ret_reloc_base..) {
|
2024-09-13 11:22:27 -05:00
|
|
|
rel.apply_jump(&mut self.ci.code, len, 0);
|
2024-09-01 19:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Some(Value { ty: sig.ret, loc });
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "TypeOf", args: [expr], .. } => {
|
2024-09-09 12:36:53 -05:00
|
|
|
Some(Value::ty(self.infer_type(expr)))
|
2024-05-20 07:11:58 -05:00
|
|
|
}
|
2024-09-17 11:07:15 -05:00
|
|
|
E::Directive { name: "eca", args, pos } => {
|
|
|
|
let Some(ty) = ctx.ty else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
"type to return form eca is unknown, use `@as(<type>, @eca(...<expr>))`",
|
|
|
|
);
|
|
|
|
};
|
2024-05-16 09:50:29 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let mut parama = self.tys.parama(ty);
|
2024-06-24 10:26:00 -05:00
|
|
|
let base = self.pool.arg_locs.len();
|
2024-05-14 16:07:32 -05:00
|
|
|
for arg in args {
|
|
|
|
let arg = self.expr(arg)?;
|
2024-09-17 11:07:15 -05:00
|
|
|
if arg.ty == ty::Id::from(ty::TYPE) {
|
|
|
|
self.report(pos, "na na na nana, no passing types to ecas");
|
|
|
|
}
|
2024-05-14 16:07:32 -05:00
|
|
|
self.pass_arg(&arg, &mut parama);
|
2024-06-24 10:26:00 -05:00
|
|
|
self.pool.arg_locs.push(arg.loc);
|
2024-05-14 16:07:32 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
for value in self.pool.arg_locs.drain(base..) {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.free_loc(value);
|
|
|
|
}
|
2024-05-14 16:07:32 -05:00
|
|
|
|
2024-09-01 19:38:11 -05:00
|
|
|
let loc = self.alloc_ret(ty, ctx, false);
|
2024-05-14 16:07:32 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(eca());
|
2024-05-14 16:07:32 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
self.load_ret(ty, &loc);
|
2024-05-14 16:07:32 -05:00
|
|
|
|
|
|
|
return Some(Value { ty, loc });
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "sizeof", args: [ty], .. } => {
|
2024-05-14 16:07:32 -05:00
|
|
|
let ty = self.ty(ty);
|
2024-10-13 15:24:57 -05:00
|
|
|
Some(Value::imm(self.tys.size_of(ty) as _))
|
2024-05-14 16:07:32 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "alignof", args: [ty], .. } => {
|
2024-05-14 16:07:32 -05:00
|
|
|
let ty = self.ty(ty);
|
2024-10-13 15:24:57 -05:00
|
|
|
Some(Value::imm(self.tys.align_of(ty) as _))
|
2024-05-14 16:07:32 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "intcast", args: [val], .. } => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let Some(ty) = ctx.ty else {
|
2024-05-14 16:07:32 -05:00
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
2024-09-17 11:07:15 -05:00
|
|
|
"type to cast to is unknown, use `@as(<type>, @intcast(<expr>))`",
|
2024-05-14 16:07:32 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
let mut val = self.expr(val)?;
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let from_size = self.tys.size_of(val.ty);
|
|
|
|
let to_size = self.tys.size_of(ty);
|
2024-05-14 16:07:32 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if from_size < to_size && val.ty.is_signed() {
|
2024-05-14 16:07:32 -05:00
|
|
|
let reg = self.loc_to_reg(val.loc, from_size);
|
2024-06-23 02:09:33 -05:00
|
|
|
let op = [sxt8, sxt16, sxt32][from_size.ilog2() as usize];
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(reg.get(), reg.get()));
|
2024-06-23 02:09:33 -05:00
|
|
|
val.loc = Loc::reg(reg);
|
2024-05-14 16:07:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Value { ty, loc: val.loc })
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "bitcast", args: [val], .. } => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let Some(ty) = ctx.ty else {
|
2024-05-14 16:07:32 -05:00
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
2024-09-09 12:36:53 -05:00
|
|
|
"type to cast to is unknown, use `@as(<type>, @bitcast(<expr>))`",
|
2024-05-14 16:07:32 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let size = self.tys.size_of(ty);
|
2024-05-14 16:07:32 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
ctx.ty = None;
|
2024-05-14 16:07:32 -05:00
|
|
|
|
|
|
|
let val = self.expr_ctx(val, ctx)?;
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if self.tys.size_of(val.ty) != size {
|
2024-05-14 16:07:32 -05:00
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
format_args!(
|
|
|
|
"cannot bitcast {} to {} (different sizes: {} != {size})",
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ty_display(val.ty),
|
|
|
|
self.ty_display(ty),
|
|
|
|
self.tys.size_of(val.ty),
|
2024-05-14 16:07:32 -05:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
debug_assert_eq!(
|
|
|
|
self.tys.align_of(val.ty),
|
|
|
|
self.tys.align_of(ty),
|
|
|
|
"TODO: might need stack relocation"
|
|
|
|
);
|
2024-05-14 16:07:32 -05:00
|
|
|
|
|
|
|
return Some(Value { ty, loc: val.loc });
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Directive { name: "as", args: [ty, val], .. } => {
|
2024-05-14 16:07:32 -05:00
|
|
|
let ty = self.ty(ty);
|
2024-06-23 02:09:33 -05:00
|
|
|
ctx.ty = Some(ty);
|
2024-05-14 16:07:32 -05:00
|
|
|
return self.expr_ctx(val, ctx);
|
|
|
|
}
|
2024-09-30 12:09:17 -05:00
|
|
|
E::Bool { value, .. } => Some(Value { ty: ty::Id::BOOL, loc: Loc::ct(value as u64) }),
|
2024-09-09 12:36:53 -05:00
|
|
|
E::Idk { pos } => {
|
|
|
|
let Some(ty) = ctx.ty else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
"`idk` can be used only when type can be inferred, use @as(<type>, idk)",
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ctx.loc.is_some() {
|
2024-09-12 11:42:21 -05:00
|
|
|
// self.report(
|
|
|
|
// pos,
|
|
|
|
// format_args!(
|
|
|
|
// "`idk` would be written to an existing memory location \
|
|
|
|
// which at ths point does notthing so its prohibited. TODO: make debug \
|
|
|
|
// builds write 0xAA instead. Info for weak people: {:?}",
|
|
|
|
// ctx.loc
|
|
|
|
// ),
|
|
|
|
// );
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let loc = match self.tys.size_of(ty) {
|
|
|
|
0 => Loc::default(),
|
|
|
|
1..=8 => Loc::reg(self.ci.regs.allocate()),
|
|
|
|
size => Loc::stack(self.ci.stack.allocate(size)),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Value { ty, loc })
|
|
|
|
}
|
2024-07-02 07:49:05 -05:00
|
|
|
E::String { pos, mut literal } => {
|
2024-10-10 01:35:17 -05:00
|
|
|
literal = &literal[1..literal.len() - 1];
|
2024-07-02 07:49:05 -05:00
|
|
|
|
|
|
|
if !literal.ends_with("\\0") {
|
|
|
|
self.report(pos, "string literal must end with null byte (for now)");
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let report = |bytes: &core::str::Bytes, message| {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.report(pos + (literal.len() - bytes.len()) as u32 - 1, message)
|
2024-07-02 07:49:05 -05:00
|
|
|
};
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let mut str = Vec::<u8>::with_capacity(literal.len());
|
2024-07-07 06:42:48 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let decode_braces = |str: &mut Vec<u8>, bytes: &mut core::str::Bytes| {
|
2024-07-02 07:49:05 -05:00
|
|
|
while let Some(b) = bytes.next()
|
|
|
|
&& b != b'}'
|
|
|
|
{
|
|
|
|
let c = bytes
|
|
|
|
.next()
|
2024-09-13 11:22:27 -05:00
|
|
|
.unwrap_or_else(|| report(bytes, "incomplete escape sequence"));
|
|
|
|
let decode = |b: u8| match b {
|
2024-07-02 07:49:05 -05:00
|
|
|
b'0'..=b'9' => b - b'0',
|
|
|
|
b'a'..=b'f' => b - b'a' + 10,
|
|
|
|
b'A'..=b'F' => b - b'A' + 10,
|
2024-09-13 11:22:27 -05:00
|
|
|
_ => report(bytes, "expected hex digit or '}'"),
|
2024-07-02 07:49:05 -05:00
|
|
|
};
|
2024-09-13 11:22:27 -05:00
|
|
|
str.push(decode(b) << 4 | decode(c));
|
2024-07-02 07:49:05 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = literal.bytes();
|
|
|
|
while let Some(b) = bytes.next() {
|
|
|
|
if b != b'\\' {
|
2024-09-13 11:22:27 -05:00
|
|
|
str.push(b);
|
2024-07-02 07:49:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let b = match bytes
|
|
|
|
.next()
|
2024-09-13 11:22:27 -05:00
|
|
|
.unwrap_or_else(|| report(&bytes, "incomplete escape sequence"))
|
2024-07-02 07:49:05 -05:00
|
|
|
{
|
|
|
|
b'n' => b'\n',
|
|
|
|
b'r' => b'\r',
|
|
|
|
b't' => b'\t',
|
|
|
|
b'\\' => b'\\',
|
|
|
|
b'\'' => b'\'',
|
|
|
|
b'"' => b'"',
|
|
|
|
b'0' => b'\0',
|
|
|
|
b'{' => {
|
2024-09-13 11:22:27 -05:00
|
|
|
decode_braces(&mut str, &mut bytes);
|
2024-07-02 07:49:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2024-09-13 11:22:27 -05:00
|
|
|
_ => report(&bytes, "unknown escape sequence, expected [nrt\\\"'{0]"),
|
2024-07-02 07:49:05 -05:00
|
|
|
};
|
2024-09-13 11:22:27 -05:00
|
|
|
str.push(b);
|
2024-07-02 07:49:05 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let reloc = Reloc::new(self.ci.code.len() as _, 3, 4);
|
2024-10-01 14:39:23 -05:00
|
|
|
let glob = self.tys.ins.globals.len() as ty::Global;
|
|
|
|
self.tys.ins.globals.push(Global { data: str, ..Default::default() });
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci
|
|
|
|
.relocs
|
|
|
|
.push(TypedReloc { target: ty::Kind::Global(glob).compress(), reloc });
|
2024-07-02 07:49:05 -05:00
|
|
|
let reg = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(instrs::lra(reg.get(), 0, 0));
|
2024-07-02 07:49:05 -05:00
|
|
|
Some(Value::new(self.tys.make_ptr(ty::U8.into()), reg))
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Ctor { pos, ty, fields, .. } => {
|
2024-09-17 10:59:03 -05:00
|
|
|
let (ty, loc) = self.prepare_struct_ctor(pos, &mut ctx, ty, fields.len());
|
2024-07-08 11:08:58 -05:00
|
|
|
|
|
|
|
let ty::Kind::Struct(stru) = ty.expand() else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
"our current technology does not (yet) allow\
|
|
|
|
us to construct '{}' with struct constructor",
|
|
|
|
);
|
|
|
|
};
|
2024-10-13 06:30:00 -05:00
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
for &CtorField { pos, name, ref value, .. } in fields {
|
2024-09-30 12:09:17 -05:00
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(&self.tys, stru, name) else {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.report(pos, format_args!("field not found: {name:?}"));
|
|
|
|
};
|
|
|
|
let loc = loc.as_ref().offset(offset);
|
2024-07-08 00:22:53 -05:00
|
|
|
let value = self.expr_ctx(value, Ctx::default().with_loc(loc).with_ty(ty))?;
|
2024-07-07 12:16:15 -05:00
|
|
|
self.ci.free_loc(value.loc);
|
|
|
|
}
|
2024-09-17 10:59:03 -05:00
|
|
|
|
|
|
|
if let Some(dst_loc) = ctx.loc {
|
|
|
|
self.store_typed(loc, &dst_loc, ty);
|
|
|
|
return Some(Value { ty, loc: dst_loc });
|
|
|
|
} else {
|
|
|
|
return Some(Value { ty, loc });
|
|
|
|
}
|
2024-07-07 12:16:15 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Tupl { pos, ty, fields, .. } => {
|
2024-09-17 10:59:03 -05:00
|
|
|
let (ty, loc) = self.prepare_struct_ctor(pos, &mut ctx, ty, fields.len());
|
2024-07-08 11:08:58 -05:00
|
|
|
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Struct(stru) => {
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut oiter = OffsetIter::new(stru, &self.tys);
|
2024-09-28 14:56:39 -05:00
|
|
|
for field in fields {
|
|
|
|
let (ty, offset) = oiter.next_ty(&self.tys).unwrap();
|
2024-07-08 11:08:58 -05:00
|
|
|
let loc = loc.as_ref().offset(offset);
|
2024-09-28 14:56:39 -05:00
|
|
|
let ctx = Ctx::default().with_loc(loc).with_ty(ty);
|
2024-07-08 11:08:58 -05:00
|
|
|
let value = self.expr_ctx(field, ctx)?;
|
|
|
|
self.ci.free_loc(value.loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Kind::Slice(arr) => {
|
2024-10-01 14:36:23 -05:00
|
|
|
let arr = self.tys.ins.arrays[arr as usize];
|
2024-07-08 11:08:58 -05:00
|
|
|
let item_size = self.tys.size_of(arr.ty);
|
|
|
|
for (i, value) in fields.iter().enumerate() {
|
|
|
|
let loc = loc.as_ref().offset(i as u32 * item_size);
|
|
|
|
let value =
|
2024-09-04 11:48:25 -05:00
|
|
|
self.expr_ctx(value, Ctx::default().with_loc(loc).with_ty(arr.ty))?;
|
2024-07-08 11:08:58 -05:00
|
|
|
self.ci.free_loc(value.loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
|
|
|
"compiler does not (yet) know how to initialize\
|
|
|
|
'{}' with tuple constructor",
|
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
),
|
2024-05-12 06:13:36 -05:00
|
|
|
}
|
2024-05-13 04:05:35 -05:00
|
|
|
|
2024-09-17 10:59:03 -05:00
|
|
|
if let Some(dst_loc) = ctx.loc {
|
|
|
|
self.store_typed(loc, &dst_loc, ty);
|
|
|
|
return Some(Value { ty, loc: dst_loc });
|
|
|
|
} else {
|
|
|
|
return Some(Value { ty, loc });
|
|
|
|
}
|
2024-05-12 06:13:36 -05:00
|
|
|
}
|
2024-09-30 12:09:17 -05:00
|
|
|
E::Field { target, name: field, pos } => {
|
2024-09-13 11:22:27 -05:00
|
|
|
let checkpoint = self.ci.snap();
|
2024-05-13 04:05:35 -05:00
|
|
|
let mut tal = self.expr(target)?;
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
if let Some(ty) = self.tys.base_of(tal.ty) {
|
|
|
|
tal.ty = ty;
|
2024-06-23 02:09:33 -05:00
|
|
|
tal.loc = tal.loc.into_derefed();
|
2024-05-12 15:40:28 -05:00
|
|
|
}
|
2024-06-01 13:30:07 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
match tal.ty.expand() {
|
|
|
|
ty::Kind::Struct(idx) => {
|
2024-09-30 12:09:17 -05:00
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(&self.tys, idx, field)
|
|
|
|
else {
|
|
|
|
self.report(pos, format_args!("field not found: {field:?}"));
|
2024-06-23 02:09:33 -05:00
|
|
|
};
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty, loc: tal.loc.offset(offset) })
|
2024-06-01 13:30:07 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
ty::Kind::Builtin(ty::TYPE) => {
|
2024-07-06 16:02:04 -05:00
|
|
|
self.ci.free_loc(tal.loc);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.revert(checkpoint);
|
2024-10-01 10:43:15 -05:00
|
|
|
match self.ty(target).expand() {
|
2024-07-07 07:52:31 -05:00
|
|
|
ty::Kind::Module(idx) => {
|
2024-10-13 05:25:12 -05:00
|
|
|
match self.find_or_declare(pos, idx, Err(field), "") {
|
2024-07-07 07:52:31 -05:00
|
|
|
ty::Kind::Global(idx) => self.handle_global(idx),
|
|
|
|
e => Some(Value::ty(e.compress())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Kind::Global(idx) => self.handle_global(idx),
|
2024-07-06 16:02:04 -05:00
|
|
|
e => unimplemented!("{e:?}"),
|
2024-06-01 13:30:07 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
_ => self.report(
|
2024-06-01 13:30:07 -05:00
|
|
|
target.pos(),
|
2024-10-12 06:07:49 -05:00
|
|
|
format_args!(
|
|
|
|
"the field operation is not supported: {}",
|
|
|
|
self.ty_display(tal.ty)
|
|
|
|
),
|
2024-06-01 13:30:07 -05:00
|
|
|
),
|
|
|
|
}
|
2024-05-12 06:13:36 -05:00
|
|
|
}
|
2024-09-14 04:26:54 -05:00
|
|
|
E::UnOp { op: T::Sub, val, pos } => {
|
|
|
|
let value = self.expr(val)?;
|
|
|
|
|
|
|
|
if !value.ty.is_integer() {
|
|
|
|
self.report(pos, format_args!("cant negate '{}'", self.ty_display(value.ty)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = self.tys.size_of(value.ty);
|
|
|
|
|
|
|
|
let (oper, dst, drop_loc) = if let Some(dst) = &ctx.loc
|
|
|
|
&& dst.is_reg()
|
|
|
|
&& let Some(dst) = ctx.loc.take()
|
|
|
|
{
|
|
|
|
(
|
|
|
|
self.loc_to_reg(&value.loc, size),
|
|
|
|
if dst.is_ref() {
|
|
|
|
self.loc_to_reg(&dst, size)
|
|
|
|
} else {
|
|
|
|
self.loc_to_reg(dst, size)
|
|
|
|
},
|
|
|
|
value.loc,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let oper = self.loc_to_reg(value.loc, size);
|
|
|
|
(oper.as_ref(), oper, Loc::default())
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ci.emit(neg(dst.get(), oper.get()));
|
|
|
|
self.ci.free_loc(drop_loc);
|
2024-09-30 14:55:34 -05:00
|
|
|
self.ci.regs.free(oper);
|
2024-09-14 04:26:54 -05:00
|
|
|
|
|
|
|
Some(Value::new(value.ty, dst))
|
|
|
|
}
|
|
|
|
E::UnOp { op: T::Xor, val, .. } => {
|
|
|
|
let val = self.ty(val);
|
|
|
|
Some(Value::ty(self.tys.make_ptr(val)))
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::UnOp { op: T::Band, val, pos } => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let mut val = self.expr(val)?;
|
2024-07-08 00:22:53 -05:00
|
|
|
let Loc::Rt { derefed: drfd @ true, reg, stack, offset } = &mut val.loc else {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.report(
|
2024-05-12 16:19:45 -05:00
|
|
|
pos,
|
2024-06-23 02:09:33 -05:00
|
|
|
format_args!(
|
|
|
|
"cant take pointer of {} ({:?})",
|
|
|
|
self.ty_display(val.ty),
|
|
|
|
val.loc
|
|
|
|
),
|
|
|
|
);
|
2024-05-13 04:05:35 -05:00
|
|
|
};
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
*drfd = false;
|
2024-09-30 12:09:17 -05:00
|
|
|
let offset = core::mem::take(offset) as _;
|
2024-06-23 02:09:33 -05:00
|
|
|
if reg.is_ref() {
|
|
|
|
let new_reg = self.ci.regs.allocate();
|
2024-06-23 06:55:48 -05:00
|
|
|
self.stack_offset(new_reg.get(), reg.get(), stack.as_ref(), offset);
|
2024-06-23 02:09:33 -05:00
|
|
|
*reg = new_reg;
|
|
|
|
} else {
|
2024-06-23 06:55:48 -05:00
|
|
|
self.stack_offset(reg.get(), reg.get(), stack.as_ref(), offset);
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we might be able to track this but it will be pain
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::forget(stack.take());
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty: self.tys.make_ptr(val.ty), loc: val.loc })
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::UnOp { op: T::Mul, val, pos } => {
|
2024-05-13 04:05:35 -05:00
|
|
|
let val = self.expr(val)?;
|
2024-10-10 01:35:17 -05:00
|
|
|
match self.tys.base_of(val.ty) {
|
|
|
|
Some(ty) => Some(Value {
|
|
|
|
ty,
|
2024-06-23 02:09:33 -05:00
|
|
|
loc: Loc::reg(self.loc_to_reg(val.loc, self.tys.size_of(val.ty)))
|
|
|
|
.into_derefed(),
|
2024-05-12 04:52:58 -05:00
|
|
|
}),
|
2024-05-12 16:19:45 -05:00
|
|
|
_ => self.report(
|
|
|
|
pos,
|
2024-06-23 02:09:33 -05:00
|
|
|
format_args!("expected pointer, got {}", self.ty_display(val.ty)),
|
2024-05-12 16:19:45 -05:00
|
|
|
),
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
E::BinOp { left, op: T::Decl, right } if self.has_ct(left) => {
|
|
|
|
let slot_base = self.ct.vm.read_reg(reg::STACK_PTR).0;
|
|
|
|
let (cnt, ty) = self.eval_const_low(right, None);
|
|
|
|
if self.assign_ct_pattern(left, ty, cnt as _) {
|
|
|
|
self.ct.vm.write_reg(reg::STACK_PTR, slot_base);
|
|
|
|
}
|
|
|
|
Some(Value::void())
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::BinOp { left, op: T::Decl, right } => {
|
|
|
|
let value = self.expr(right)?;
|
|
|
|
self.assign_pattern(left, value)
|
2024-05-11 15:22:08 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::Call { func: fast, args, .. } => {
|
2024-09-30 12:27:00 -05:00
|
|
|
log::trace!("call {}", self.ast_display(fast));
|
2024-06-24 10:26:00 -05:00
|
|
|
let func_ty = self.ty(fast);
|
2024-07-19 05:00:55 -05:00
|
|
|
let ty::Kind::Func(mut func) = func_ty.expand() else {
|
2024-06-24 10:26:00 -05:00
|
|
|
self.report(fast.pos(), "can't call this, maybe in the future");
|
2024-05-19 11:20:42 -05:00
|
|
|
};
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-09-01 19:38:11 -05:00
|
|
|
// TODO: this will be usefull but not now
|
|
|
|
let scope = self.ci.vars.len();
|
|
|
|
let sig = self.compute_signature(&mut func, expr.pos(), args)?;
|
|
|
|
self.ci.vars.truncate(scope);
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &self.tys.ins.funcs[func as usize];
|
2024-07-19 05:00:55 -05:00
|
|
|
let ast = self.files[fuc.file as usize].clone();
|
2024-09-01 19:38:11 -05:00
|
|
|
let E::BinOp { right: &E::Closure { args: cargs, .. }, .. } =
|
2024-07-19 05:00:55 -05:00
|
|
|
fuc.expr.get(&ast).unwrap()
|
2024-06-24 10:26:00 -05:00
|
|
|
else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut parama = self.tys.parama(sig.ret);
|
2024-10-10 09:08:03 -05:00
|
|
|
let base = self.pool.arg_locs.len();
|
2024-06-24 10:26:00 -05:00
|
|
|
let mut sig_args = sig.args.range();
|
2024-07-19 05:00:55 -05:00
|
|
|
let mut should_momize = !args.is_empty() && sig.ret == ty::Id::from(ty::TYPE);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
|
|
|
self.assert_arg_count(expr.pos(), args.len(), cargs.len(), "function call");
|
|
|
|
|
|
|
|
for (i, (arg, carg)) in args.iter().zip(cargs).enumerate() {
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[sig_args.next().unwrap()];
|
2024-06-24 10:26:00 -05:00
|
|
|
let sym = parser::find_symbol(&ast.symbols, carg.id);
|
|
|
|
if sym.flags & idfl::COMPTIME != 0 {
|
|
|
|
sig_args.next().unwrap();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
// TODO: pass the arg as dest
|
2024-06-24 10:26:00 -05:00
|
|
|
let varg = self.expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
2024-10-13 07:11:17 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
arg.pos(),
|
|
|
|
varg.ty,
|
|
|
|
ty,
|
|
|
|
TyCheck::Assign,
|
|
|
|
format_args!("argument({i})"),
|
|
|
|
);
|
2024-06-24 10:26:00 -05:00
|
|
|
self.pass_arg(&varg, &mut parama);
|
2024-10-10 09:08:03 -05:00
|
|
|
self.pool.arg_locs.push(varg.loc);
|
2024-07-19 05:00:55 -05:00
|
|
|
should_momize = false;
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-10-10 09:08:03 -05:00
|
|
|
for value in self.pool.arg_locs.drain(base..) {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.free_loc(value);
|
|
|
|
}
|
2024-05-13 02:38:33 -05:00
|
|
|
|
2024-09-09 18:15:18 -05:00
|
|
|
let loc = self.alloc_ret(sig.ret, ctx, true);
|
2024-07-06 16:02:04 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if should_momize {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.write_trap(trap::Trap::MomizedCall(trap::MomizedCall { func }));
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let reloc = Reloc::new(self.ci.code.len(), 3, 4);
|
|
|
|
self.ci.relocs.push(TypedReloc { target: ty::Kind::Func(func).compress(), reloc });
|
|
|
|
self.ci.emit(jal(RET_ADDR, ZERO, 0));
|
2024-09-01 19:38:11 -05:00
|
|
|
self.make_func_reachable(func);
|
2024-07-06 16:02:04 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if should_momize {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(tx());
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
self.load_ret(sig.ret, &loc);
|
|
|
|
return Some(Value { ty: sig.ret, loc });
|
2024-05-11 09:04:13 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
E::Ident { id, .. } if ident::is_null(id) => Some(Value::ty(id.into())),
|
2024-09-09 12:36:53 -05:00
|
|
|
E::Ident { id, .. }
|
2024-07-08 00:22:53 -05:00
|
|
|
if let Some((var_index, var)) =
|
2024-10-13 09:16:27 -05:00
|
|
|
self.ci.vars.iter_mut().enumerate().rfind(|(_, v)| v.id == id) =>
|
2024-05-19 11:20:42 -05:00
|
|
|
{
|
2024-09-20 05:03:24 -05:00
|
|
|
let loc = var.value.loc.as_ref();
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty: self.ci.vars[var_index].value.ty, loc })
|
2024-05-11 05:51:32 -05:00
|
|
|
}
|
2024-10-13 05:25:12 -05:00
|
|
|
E::Ident { id, .. } => {
|
|
|
|
let cfile = self.cfile().clone();
|
|
|
|
match self.find_or_declare(
|
|
|
|
ident::pos(id),
|
|
|
|
self.ci.file,
|
|
|
|
Ok(id),
|
|
|
|
cfile.ident_str(id),
|
|
|
|
) {
|
2024-10-01 10:43:15 -05:00
|
|
|
ty::Kind::Global(id) => self.handle_global(id),
|
|
|
|
tk => Some(Value::ty(tk.compress())),
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
E::Return { pos, val, .. } => {
|
2024-09-17 08:25:31 -05:00
|
|
|
let size = self.ci.ret.map_or(17, |ty| self.tys.size_of(ty));
|
|
|
|
let loc = match size {
|
|
|
|
_ if self.ci.inline_ret_loc != Loc::default() => {
|
|
|
|
Some(self.ci.inline_ret_loc.as_ref())
|
|
|
|
}
|
|
|
|
0 => None,
|
2024-09-17 10:59:03 -05:00
|
|
|
1..=16 => Some(Loc::reg(1)),
|
2024-09-17 08:25:31 -05:00
|
|
|
_ => Some(Loc::reg(self.ci.ret_reg.as_ref()).into_derefed()),
|
|
|
|
};
|
|
|
|
let value = if let Some(val) = val {
|
2024-10-13 07:11:17 -05:00
|
|
|
self.expr_ctx(val, Ctx { ty: self.ci.ret, loc, ..Default::default() })?
|
2024-09-01 21:45:42 -05:00
|
|
|
} else {
|
2024-09-17 08:25:31 -05:00
|
|
|
Value::void()
|
2024-09-01 21:45:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match self.ci.ret {
|
2024-09-17 08:25:31 -05:00
|
|
|
None => self.ci.ret = Some(value.ty),
|
2024-10-13 07:11:17 -05:00
|
|
|
Some(ret) => {
|
|
|
|
_ = self.assert_ty(pos, value.ty, ret, TyCheck::Assign, "return type")
|
|
|
|
}
|
2024-09-17 08:25:31 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.ret_relocs.push(Reloc::new(self.ci.code.len(), 1, 4));
|
|
|
|
self.ci.emit(jmp(0));
|
2024-05-10 14:33:42 -05:00
|
|
|
None
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
2024-05-11 15:22:08 -05:00
|
|
|
E::Block { stmts, .. } => {
|
2024-05-09 16:41:59 -05:00
|
|
|
for stmt in stmts {
|
2024-09-09 18:15:18 -05:00
|
|
|
let val = self.expr(stmt)?;
|
|
|
|
self.ci.free_loc(val.loc);
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
Some(Value::void())
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
E::Number { value, pos, .. } => Some(Value {
|
|
|
|
ty: {
|
2024-09-30 12:09:17 -05:00
|
|
|
let ty = ctx.ty.map(ty::Id::strip_pointer).unwrap_or(ty::Id::INT);
|
2024-09-04 09:54:34 -05:00
|
|
|
if !ty.is_integer() && !ty.is_pointer() {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
2024-10-12 06:07:49 -05:00
|
|
|
"this integer was inferred to be '{}'",
|
2024-09-04 09:54:34 -05:00
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ty
|
|
|
|
},
|
2024-09-03 10:51:28 -05:00
|
|
|
loc: Loc::ct(value as u64),
|
2024-05-10 14:33:42 -05:00
|
|
|
}),
|
2024-09-09 12:36:53 -05:00
|
|
|
E::If { cond, then, mut else_, .. } => {
|
|
|
|
let mut then = Some(then);
|
|
|
|
let jump_offset;
|
|
|
|
if let &E::BinOp { left, op, right } = cond
|
|
|
|
&& let ty = self.infer_type(left)
|
2024-09-13 11:41:01 -05:00
|
|
|
&& let Some((op, swapped)) = op.cond_op(ty.is_signed())
|
2024-09-09 12:36:53 -05:00
|
|
|
{
|
|
|
|
let left = self.expr_ctx(left, Ctx::default())?;
|
|
|
|
let right = self.expr_ctx(right, Ctx::default())?;
|
|
|
|
let lsize = self.tys.size_of(left.ty);
|
|
|
|
let rsize = self.tys.size_of(right.ty);
|
|
|
|
let left_reg = self.loc_to_reg(&left.loc, lsize);
|
|
|
|
let right_reg = self.loc_to_reg(&right.loc, rsize);
|
2024-09-13 11:41:01 -05:00
|
|
|
jump_offset = self.ci.code.len();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(left_reg.get(), right_reg.get(), 0));
|
2024-09-09 12:36:53 -05:00
|
|
|
self.ci.free_loc(left.loc);
|
|
|
|
self.ci.free_loc(right.loc);
|
|
|
|
self.ci.regs.free(left_reg);
|
|
|
|
self.ci.regs.free(right_reg);
|
|
|
|
if swapped {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(&mut then, &mut else_);
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?;
|
|
|
|
let reg = self.loc_to_reg(&cond.loc, 1);
|
2024-09-13 11:41:01 -05:00
|
|
|
jump_offset = self.ci.code.len();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(jeq(reg.get(), 0, 0));
|
2024-09-09 12:36:53 -05:00
|
|
|
self.ci.free_loc(cond.loc);
|
|
|
|
self.ci.regs.free(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
let then_unreachable =
|
|
|
|
if let Some(then) = then { self.expr(then).is_none() } else { false };
|
2024-05-12 07:56:59 -05:00
|
|
|
let mut else_unreachable = false;
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-09-13 11:41:01 -05:00
|
|
|
let mut jump = self.ci.code.len() as i64 - jump_offset as i64;
|
2024-05-11 11:16:27 -05:00
|
|
|
|
|
|
|
if let Some(else_) = else_ {
|
2024-09-13 11:41:01 -05:00
|
|
|
let else_jump_offset = self.ci.code.len();
|
2024-05-12 07:56:59 -05:00
|
|
|
if !then_unreachable {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(jmp(0));
|
2024-09-13 11:41:01 -05:00
|
|
|
jump = self.ci.code.len() as i64 - jump_offset as i64;
|
2024-05-12 07:56:59 -05:00
|
|
|
}
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-05-13 04:05:35 -05:00
|
|
|
else_unreachable = self.expr(else_).is_none();
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-05-12 07:56:59 -05:00
|
|
|
if !then_unreachable {
|
2024-09-13 11:41:01 -05:00
|
|
|
let jump = self.ci.code.len() as i64 - else_jump_offset as i64;
|
|
|
|
write_reloc(&mut self.ci.code, else_jump_offset + 1, jump, 4);
|
2024-05-12 07:56:59 -05:00
|
|
|
}
|
2024-05-11 11:16:27 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:41:01 -05:00
|
|
|
write_reloc(&mut self.ci.code, jump_offset + 3, jump, 2);
|
2024-05-12 07:56:59 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
(!then_unreachable || !else_unreachable).then_some(Value::void())
|
2024-05-11 10:05:22 -05:00
|
|
|
}
|
2024-05-12 07:56:59 -05:00
|
|
|
E::Loop { body, .. } => 'a: {
|
2024-09-13 11:41:01 -05:00
|
|
|
let loop_start = self.ci.code.len();
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.loops.push(Loop {
|
2024-07-08 00:22:53 -05:00
|
|
|
var_count: self.ci.vars.len() as _,
|
2024-09-13 11:41:01 -05:00
|
|
|
offset: loop_start as _,
|
2024-06-23 02:09:33 -05:00
|
|
|
reloc_base: self.ci.loop_relocs.len() as u32,
|
2024-05-11 11:16:27 -05:00
|
|
|
});
|
2024-05-13 04:05:35 -05:00
|
|
|
let body_unreachable = self.expr(body).is_none();
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-05-12 07:56:59 -05:00
|
|
|
if !body_unreachable {
|
2024-09-13 11:41:01 -05:00
|
|
|
let loop_end = self.ci.code.len();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(jmp(loop_start as i32 - loop_end as i32));
|
2024-05-12 07:56:59 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let loop_end = self.ci.code.len() as u32;
|
2024-05-11 11:16:27 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let loopa = self.ci.loops.pop().unwrap();
|
|
|
|
let is_unreachable = loopa.reloc_base == self.ci.loop_relocs.len() as u32;
|
|
|
|
for reloc in self.ci.loop_relocs.drain(loopa.reloc_base as usize..) {
|
2024-09-13 11:22:27 -05:00
|
|
|
let off = reloc.apply_jump(&mut self.ci.code, loop_end, 0);
|
2024-09-03 19:35:09 -05:00
|
|
|
debug_assert!(off > 0);
|
2024-05-11 11:16:27 -05:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut vars = core::mem::take(&mut self.ci.vars);
|
2024-06-24 10:26:00 -05:00
|
|
|
for var in vars.drain(loopa.var_count as usize..) {
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.free_loc(var.value.loc);
|
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
self.ci.vars = vars;
|
2024-05-14 07:01:40 -05:00
|
|
|
|
2024-05-12 07:56:59 -05:00
|
|
|
if is_unreachable {
|
|
|
|
break 'a None;
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
Some(Value::void())
|
2024-05-11 11:16:27 -05:00
|
|
|
}
|
2024-05-11 15:22:08 -05:00
|
|
|
E::Break { .. } => {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.loop_relocs.push(Reloc::new(self.ci.code.len(), 1, 4));
|
|
|
|
self.ci.emit(jmp(0));
|
2024-05-11 11:16:27 -05:00
|
|
|
None
|
|
|
|
}
|
2024-05-11 15:22:08 -05:00
|
|
|
E::Continue { .. } => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let loop_ = self.ci.loops.last().unwrap();
|
2024-09-13 11:41:01 -05:00
|
|
|
let offset = self.ci.code.len();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(jmp(loop_.offset as i32 - offset as i32));
|
2024-05-11 11:16:27 -05:00
|
|
|
None
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::BinOp { left, op: op @ (T::And | T::Or), right } => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let lhs = self.expr_ctx(left, Ctx::default().with_ty(ty::BOOL))?;
|
2024-05-15 04:10:20 -05:00
|
|
|
let lhs = self.loc_to_reg(lhs.loc, 1);
|
2024-09-13 11:22:27 -05:00
|
|
|
let jump_offset = self.ci.code.len() + 3;
|
2024-06-23 02:09:33 -05:00
|
|
|
let op = if op == T::And { jeq } else { jne };
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(lhs.get(), 0, 0));
|
2024-05-15 04:10:20 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if let Some(rhs) = self.expr_ctx(right, Ctx::default().with_ty(ty::BOOL)) {
|
2024-05-15 04:10:20 -05:00
|
|
|
let rhs = self.loc_to_reg(rhs.loc, 1);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(cp(lhs.get(), rhs.get()));
|
2024-05-15 04:10:20 -05:00
|
|
|
}
|
2024-05-13 06:36:29 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let jump = self.ci.code.len() as i64 - jump_offset as i64;
|
|
|
|
write_reloc(&mut self.ci.code, jump_offset, jump, 2);
|
2024-05-15 04:10:20 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
Some(Value { ty: ty::Id::BOOL, loc: Loc::reg(lhs) })
|
2024-05-15 04:10:20 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
E::BinOp { left, op, right } if op != T::Decl => 'ops: {
|
2024-09-05 19:42:07 -05:00
|
|
|
let left = self.expr_ctx(left, Ctx {
|
2024-09-08 05:00:07 -05:00
|
|
|
ty: ctx.ty.filter(|_| op.is_homogenous()),
|
2024-10-13 08:22:16 -05:00
|
|
|
check: ctx.check,
|
2024-09-05 19:42:07 -05:00
|
|
|
..Default::default()
|
|
|
|
})?;
|
2024-05-13 08:24:48 -05:00
|
|
|
|
2024-05-12 04:52:58 -05:00
|
|
|
if op == T::Assign {
|
2024-06-23 02:09:33 -05:00
|
|
|
let value = self.expr_ctx(right, Ctx::from(left)).unwrap();
|
|
|
|
self.ci.free_loc(value.loc);
|
|
|
|
return Some(Value::void());
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
2024-05-10 15:54:12 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if let ty::Kind::Struct(_) = left.ty.expand() {
|
|
|
|
let right = self.expr_ctx(right, Ctx::default().with_ty(left.ty))?;
|
2024-10-13 07:11:17 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
expr.pos(),
|
|
|
|
right.ty,
|
|
|
|
left.ty,
|
|
|
|
TyCheck::Assign,
|
|
|
|
"right struct operand",
|
|
|
|
);
|
2024-05-14 05:17:39 -05:00
|
|
|
return self.struct_op(op, left.ty, ctx, left.loc, right.loc);
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let lsize = self.tys.size_of(left.ty);
|
2024-05-16 06:29:16 -05:00
|
|
|
|
2024-09-09 18:15:18 -05:00
|
|
|
let (mut lhs, dst, drop_loc) = if let Some(dst) = &ctx.loc
|
|
|
|
&& dst.is_reg()
|
|
|
|
&& let Some(dst) = ctx.loc.take()
|
|
|
|
{
|
|
|
|
(
|
|
|
|
self.loc_to_reg(&left.loc, lsize),
|
|
|
|
if dst.is_ref() {
|
|
|
|
self.loc_to_reg(&dst, lsize)
|
|
|
|
} else {
|
|
|
|
self.loc_to_reg(dst, lsize)
|
|
|
|
},
|
|
|
|
left.loc,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let lhs = self.loc_to_reg(left.loc, lsize);
|
|
|
|
(lhs.as_ref(), lhs, Loc::default())
|
|
|
|
};
|
2024-10-13 07:11:17 -05:00
|
|
|
let right = self
|
|
|
|
.expr_ctx(right, Ctx::default().with_ty(left.ty).with_check(TyCheck::BinOp))?;
|
2024-06-23 02:09:33 -05:00
|
|
|
let rsize = self.tys.size_of(right.ty);
|
2024-05-13 06:36:29 -05:00
|
|
|
|
2024-10-13 07:11:17 -05:00
|
|
|
let ty = self.assert_ty(
|
|
|
|
expr.pos(),
|
|
|
|
right.ty,
|
|
|
|
left.ty,
|
|
|
|
TyCheck::BinOp,
|
|
|
|
"right sclalar operand",
|
|
|
|
);
|
2024-06-23 02:09:33 -05:00
|
|
|
let size = self.tys.size_of(ty);
|
|
|
|
let signed = ty.is_signed();
|
2024-05-13 07:23:19 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if let Loc::Ct { value: CtValue(mut imm), derefed } = right.loc
|
2024-09-15 13:14:56 -05:00
|
|
|
&& let Some(oper) = op.imm_binop(signed, size)
|
2024-05-16 06:29:16 -05:00
|
|
|
{
|
2024-07-19 05:00:55 -05:00
|
|
|
if derefed {
|
|
|
|
let mut dst = [0u8; 8];
|
|
|
|
dst[..size as usize].copy_from_slice(unsafe {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::slice::from_raw_parts(imm as _, rsize as usize)
|
2024-07-19 05:00:55 -05:00
|
|
|
});
|
|
|
|
imm = u64::from_ne_bytes(dst);
|
|
|
|
}
|
2024-05-16 06:32:04 -05:00
|
|
|
if matches!(op, T::Add | T::Sub)
|
2024-10-10 01:35:17 -05:00
|
|
|
&& let Some(ty) = self.tys.base_of(ty)
|
2024-05-16 06:32:04 -05:00
|
|
|
{
|
2024-10-10 01:35:17 -05:00
|
|
|
imm *= self.tys.size_of(ty) as u64;
|
2024-05-16 06:29:16 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(oper(dst.get(), lhs.get(), imm));
|
2024-09-09 18:15:18 -05:00
|
|
|
self.ci.regs.free(lhs);
|
|
|
|
self.ci.free_loc(drop_loc);
|
|
|
|
break 'ops Some(Value::new(ty, dst));
|
2024-05-16 06:29:16 -05:00
|
|
|
}
|
|
|
|
|
2024-09-09 18:15:18 -05:00
|
|
|
let mut rhs = self.loc_to_reg(&right.loc, rsize);
|
2024-05-15 03:37:39 -05:00
|
|
|
if matches!(op, T::Add | T::Sub) {
|
2024-05-13 08:24:48 -05:00
|
|
|
let min_size = lsize.min(rsize);
|
2024-06-23 02:09:33 -05:00
|
|
|
if ty.is_signed() && min_size < size {
|
2024-09-09 18:15:18 -05:00
|
|
|
let operand = if lsize < rsize {
|
|
|
|
lhs = self.cow_reg(lhs);
|
|
|
|
lhs.get()
|
|
|
|
} else {
|
|
|
|
rhs = self.cow_reg(rhs);
|
|
|
|
rhs.get()
|
|
|
|
};
|
2024-06-23 02:09:33 -05:00
|
|
|
let op = [sxt8, sxt16, sxt32][min_size.ilog2() as usize];
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(operand, operand));
|
2024-05-13 08:24:48 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if left.ty.is_pointer() ^ right.ty.is_pointer() {
|
|
|
|
let (offset, ty) = if left.ty.is_pointer() {
|
2024-09-09 18:15:18 -05:00
|
|
|
rhs = self.cow_reg(rhs);
|
2024-06-23 02:09:33 -05:00
|
|
|
(rhs.get(), left.ty)
|
2024-05-13 08:24:48 -05:00
|
|
|
} else {
|
2024-09-09 18:15:18 -05:00
|
|
|
lhs = self.cow_reg(lhs);
|
2024-06-23 02:09:33 -05:00
|
|
|
(lhs.get(), right.ty)
|
2024-05-13 08:24:48 -05:00
|
|
|
};
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
let ty = self.tys.base_of(ty).unwrap();
|
|
|
|
let size = self.tys.size_of(ty);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(muli64(offset, offset, size as _));
|
2024-05-13 08:24:48 -05:00
|
|
|
}
|
2024-05-13 07:23:19 -05:00
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
if let Some(op) = op.binop(signed, size) {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(dst.get(), lhs.get(), rhs.get()));
|
2024-09-09 18:15:18 -05:00
|
|
|
self.ci.regs.free(lhs);
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.free(rhs);
|
2024-09-09 18:15:18 -05:00
|
|
|
self.ci.free_loc(right.loc);
|
|
|
|
self.ci.free_loc(drop_loc);
|
|
|
|
break 'ops Some(Value::new(ty, dst));
|
2024-05-13 07:23:19 -05:00
|
|
|
}
|
2024-05-13 06:36:29 -05:00
|
|
|
|
2024-05-13 08:24:48 -05:00
|
|
|
'cmp: {
|
|
|
|
let against = match op {
|
2024-05-19 11:20:42 -05:00
|
|
|
T::Le | T::Gt => 1,
|
2024-05-13 08:24:48 -05:00
|
|
|
T::Ne | T::Eq => 0,
|
2024-05-19 11:20:42 -05:00
|
|
|
T::Ge | T::Lt => (-1i64) as _,
|
2024-05-13 08:24:48 -05:00
|
|
|
_ => break 'cmp,
|
|
|
|
};
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let op_fn = if signed { cmps } else { cmpu };
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op_fn(dst.get(), lhs.get(), rhs.get()));
|
|
|
|
self.ci.emit(cmpui(dst.get(), dst.get(), against));
|
2024-05-13 08:24:48 -05:00
|
|
|
if matches!(op, T::Eq | T::Lt | T::Gt) {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(not(dst.get(), dst.get()));
|
2024-05-11 11:16:27 -05:00
|
|
|
}
|
2024-05-10 15:54:12 -05:00
|
|
|
|
2024-09-09 18:15:18 -05:00
|
|
|
self.ci.regs.free(lhs);
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.free(rhs);
|
2024-09-09 18:15:18 -05:00
|
|
|
self.ci.free_loc(right.loc);
|
|
|
|
self.ci.free_loc(drop_loc);
|
|
|
|
break 'ops Some(Value::new(ty::BOOL, dst));
|
2024-05-13 08:24:48 -05:00
|
|
|
}
|
2024-05-10 15:54:12 -05:00
|
|
|
|
2024-05-13 08:24:48 -05:00
|
|
|
unimplemented!("{:#?}", op)
|
2024-05-10 15:54:12 -05:00
|
|
|
}
|
2024-06-25 12:55:25 -05:00
|
|
|
E::Comment { .. } => Some(Value::void()),
|
2024-10-12 06:07:49 -05:00
|
|
|
ref ast => self.report_unhandled_ast(ast, "something"),
|
2024-05-13 04:05:35 -05:00
|
|
|
}?;
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
if let Some(ty) = ctx.ty {
|
2024-10-13 07:11:17 -05:00
|
|
|
_ = self.assert_ty(expr.pos(), value.ty, ty, ctx.check, "something");
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(match ctx.loc {
|
|
|
|
Some(dest) => {
|
2024-09-16 14:46:02 -05:00
|
|
|
self.store_sized(
|
|
|
|
value.loc,
|
|
|
|
dest,
|
|
|
|
self.tys.size_of(ctx.ty.unwrap_or(value.ty)).min(self.tys.size_of(value.ty)),
|
|
|
|
);
|
2024-09-09 12:36:53 -05:00
|
|
|
Value { ty: value.ty, loc: Loc::ct(0) }
|
2024-05-14 16:07:32 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
None => value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 19:38:11 -05:00
|
|
|
fn compute_signature(&mut self, func: &mut ty::Func, pos: Pos, args: &[Expr]) -> Option<Sig> {
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &self.tys.ins.funcs[*func as usize];
|
2024-09-01 19:38:11 -05:00
|
|
|
let fast = self.files[fuc.file as usize].clone();
|
|
|
|
let Expr::BinOp { right: &Expr::Closure { args: cargs, ret, .. }, .. } =
|
|
|
|
fuc.expr.get(&fast).unwrap()
|
|
|
|
else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(if let Some(sig) = fuc.sig {
|
|
|
|
sig
|
|
|
|
} else {
|
2024-10-12 15:21:20 -05:00
|
|
|
let arg_base = self.tys.tmp.args.len();
|
2024-09-01 19:38:11 -05:00
|
|
|
|
|
|
|
for (arg, carg) in args.iter().zip(cargs) {
|
|
|
|
let ty = self.ty(&carg.ty);
|
2024-10-12 15:21:20 -05:00
|
|
|
self.tys.tmp.args.push(ty);
|
2024-09-01 19:38:11 -05:00
|
|
|
let sym = parser::find_symbol(&fast.symbols, carg.id);
|
|
|
|
let loc = if sym.flags & idfl::COMPTIME == 0 {
|
|
|
|
// FIXME: could fuck us
|
|
|
|
Loc::default()
|
|
|
|
} else {
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty,
|
2024-09-30 12:09:17 -05:00
|
|
|
ty::Id::TYPE,
|
2024-09-01 19:38:11 -05:00
|
|
|
"TODO: we dont support anything except type generics"
|
|
|
|
);
|
|
|
|
let arg = self.expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
2024-10-12 15:21:20 -05:00
|
|
|
self.tys.tmp.args.push(arg.loc.to_ty().unwrap());
|
2024-09-01 19:38:11 -05:00
|
|
|
arg.loc
|
|
|
|
};
|
|
|
|
|
2024-09-20 09:37:51 -05:00
|
|
|
self.ci.vars.push(Variable { id: carg.id, value: Value { ty, loc } });
|
2024-09-01 19:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let args = self.pack_args(pos, arg_base);
|
|
|
|
let ret = self.ty(ret);
|
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let sym = SymKey::FuncInst(*func, args);
|
2024-10-01 15:53:03 -05:00
|
|
|
let ct = |ins: &mut crate::TypeIns| {
|
|
|
|
let func_id = ins.funcs.len();
|
|
|
|
let fuc = &ins.funcs[*func as usize];
|
|
|
|
ins.funcs.push(Func {
|
2024-09-01 19:38:11 -05:00
|
|
|
file: fuc.file,
|
2024-10-01 15:53:03 -05:00
|
|
|
name: fuc.name,
|
|
|
|
base: Some(*func),
|
2024-09-01 19:38:11 -05:00
|
|
|
sig: Some(Sig { args, ret }),
|
|
|
|
expr: fuc.expr,
|
2024-09-13 11:22:27 -05:00
|
|
|
..Default::default()
|
2024-09-01 19:38:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
ty::Kind::Func(func_id as _).compress()
|
|
|
|
};
|
2024-10-01 15:53:03 -05:00
|
|
|
*func = self.tys.syms.get_or_insert(sym, &mut self.tys.ins, ct).expand().inner();
|
2024-09-01 19:38:11 -05:00
|
|
|
|
|
|
|
Sig { args, ret }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn has_ct(&self, expr: &Expr) -> bool {
|
|
|
|
expr.has_ct(&self.cfile().symbols)
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
fn infer_type(&mut self, expr: &Expr) -> ty::Id {
|
2024-09-13 11:22:27 -05:00
|
|
|
// FIXME: very inneficient
|
2024-09-09 12:36:53 -05:00
|
|
|
let mut ci = ItemCtx {
|
|
|
|
file: self.ci.file,
|
|
|
|
id: self.ci.id,
|
|
|
|
ret: self.ci.ret,
|
|
|
|
task_base: self.ci.task_base,
|
2024-10-01 08:28:18 -05:00
|
|
|
..self.pool.cis.pop().unwrap_or_default()
|
2024-09-09 12:36:53 -05:00
|
|
|
};
|
2024-10-01 08:28:18 -05:00
|
|
|
ci.loops.extend(self.ci.loops.iter());
|
|
|
|
ci.vars.extend(self.ci.vars.iter().map(|v| Variable {
|
|
|
|
id: v.id,
|
|
|
|
value: Value { ty: v.value.ty, loc: v.value.loc.as_ref() },
|
|
|
|
}));
|
|
|
|
ci.stack_relocs.extend(self.ci.stack_relocs.iter());
|
|
|
|
ci.ret_relocs.extend(self.ci.ret_relocs.iter());
|
|
|
|
ci.loop_relocs.extend(self.ci.loop_relocs.iter());
|
2024-09-09 12:36:53 -05:00
|
|
|
ci.regs.init();
|
2024-10-01 08:28:18 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(&mut self.ci, &mut ci);
|
2024-09-09 12:36:53 -05:00
|
|
|
let value = self.expr(expr).unwrap();
|
|
|
|
self.ci.free_loc(value.loc);
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(&mut self.ci, &mut ci);
|
2024-10-01 08:28:18 -05:00
|
|
|
|
|
|
|
ci.loops.clear();
|
|
|
|
ci.vars.clear();
|
|
|
|
ci.stack_relocs.clear();
|
|
|
|
ci.ret_relocs.clear();
|
|
|
|
ci.loop_relocs.clear();
|
|
|
|
ci.code.clear();
|
|
|
|
ci.relocs.clear();
|
|
|
|
self.pool.cis.push(ci);
|
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
value.ty
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:08:58 -05:00
|
|
|
fn eval_const(&mut self, expr: &Expr, ty: impl Into<ty::Id>) -> u64 {
|
2024-07-19 05:00:55 -05:00
|
|
|
self.eval_const_low(expr, Some(ty.into())).0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_const_low(&mut self, expr: &Expr, mut ty: Option<ty::Id>) -> (u64, ty::Id) {
|
2024-07-08 11:08:58 -05:00
|
|
|
let mut ci = ItemCtx {
|
|
|
|
file: self.ci.file,
|
2024-09-03 19:35:09 -05:00
|
|
|
id: ty::Kind::Builtin(u32::MAX),
|
2024-07-19 05:00:55 -05:00
|
|
|
ret: ty,
|
2024-07-08 11:08:58 -05:00
|
|
|
..self.pool.cis.pop().unwrap_or_default()
|
|
|
|
};
|
|
|
|
ci.vars.append(&mut self.ci.vars);
|
|
|
|
|
|
|
|
let loc = self.ct_eval(ci, |s, prev| {
|
2024-09-13 11:22:27 -05:00
|
|
|
s.ci.emit_prelude();
|
2024-07-08 11:08:58 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if s.ci.ret.map_or(true, |r| s.tys.size_of(r) > 16) {
|
|
|
|
let reg = s.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
s.ci.emit(instrs::cp(reg.get(), 1));
|
2024-07-19 05:00:55 -05:00
|
|
|
s.ci.ret_reg = reg;
|
|
|
|
};
|
|
|
|
|
2024-10-13 07:11:17 -05:00
|
|
|
let ctx = Ctx { ty: s.ci.ret, ..Default::default() };
|
2024-07-19 05:00:55 -05:00
|
|
|
if s.expr_ctx(&Expr::Return { pos: 0, val: Some(expr) }, ctx).is_some() {
|
2024-07-08 11:08:58 -05:00
|
|
|
s.report(expr.pos(), "we fucked up");
|
|
|
|
};
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
ty = s.ci.ret;
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
s.complete_call_graph();
|
2024-07-08 11:08:58 -05:00
|
|
|
|
|
|
|
prev.vars.append(&mut s.ci.vars);
|
2024-09-13 11:22:27 -05:00
|
|
|
s.ci.finalize();
|
|
|
|
s.ci.emit(tx());
|
2024-07-08 11:08:58 -05:00
|
|
|
|
|
|
|
Ok(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
match loc {
|
2024-07-19 05:00:55 -05:00
|
|
|
Ok(i) | Err(i) => {
|
|
|
|
(self.ct.vm.read_reg(i).cast::<u64>(), ty.expect("you have died (in brahmaputra)"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_ct_pattern(&mut self, pat: &Expr, ty: ty::Id, offset: *mut u8) -> bool {
|
|
|
|
let size = self.tys.size_of(ty);
|
|
|
|
match *pat {
|
|
|
|
Expr::Ident { id, .. }
|
|
|
|
if find_symbol(&self.cfile().symbols, id).flags & idfl::REFERENCED == 0
|
|
|
|
&& size <= 8 =>
|
|
|
|
{
|
|
|
|
let loc = Loc::ct(load_value(offset, size));
|
2024-09-20 09:37:51 -05:00
|
|
|
self.ci.vars.push(Variable { id, value: Value { ty, loc } });
|
2024-07-19 05:00:55 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
Expr::Ident { id, .. } => {
|
2024-09-20 09:37:51 -05:00
|
|
|
let var = Variable { id, value: Value { ty, loc: Loc::ct_ptr(offset as _) } };
|
2024-07-19 05:00:55 -05:00
|
|
|
self.ci.vars.push(var);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ref pat => self.report_unhandled_ast(pat, "comptime pattern"),
|
2024-07-08 11:08:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
fn assign_pattern(&mut self, pat: &Expr, right: Value) -> Option<Value> {
|
|
|
|
match *pat {
|
|
|
|
Expr::Ident { id, .. } => {
|
|
|
|
let mut loc = self.make_loc_owned(right.loc, right.ty);
|
2024-09-09 12:36:53 -05:00
|
|
|
let sym = parser::find_symbol(&self.cfile().symbols, id).flags;
|
|
|
|
if sym & idfl::REFERENCED != 0 {
|
2024-07-08 00:22:53 -05:00
|
|
|
loc = self.spill(loc, self.tys.size_of(right.ty));
|
|
|
|
}
|
2024-09-20 09:37:51 -05:00
|
|
|
self.ci.vars.push(Variable { id, value: Value { ty: right.ty, loc } });
|
2024-07-08 00:22:53 -05:00
|
|
|
}
|
|
|
|
Expr::Ctor { pos, fields, .. } => {
|
|
|
|
let ty::Kind::Struct(idx) = right.ty.expand() else {
|
|
|
|
self.report(pos, "can't use struct destruct on non struct value (TODO: shold work with modules)");
|
|
|
|
};
|
|
|
|
|
|
|
|
for &CtorField { pos, name, ref value } in fields {
|
2024-09-30 12:09:17 -05:00
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(&self.tys, idx, name) else {
|
2024-07-08 00:22:53 -05:00
|
|
|
self.report(pos, format_args!("field not found: {name:?}"));
|
|
|
|
};
|
|
|
|
let loc = self.ci.dup_loc(&right.loc).offset(offset);
|
|
|
|
self.assign_pattern(value, Value::new(ty, loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.free_loc(right.loc);
|
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
ref pat => self.report_unhandled_ast(pat, "pattern"),
|
2024-07-08 00:22:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(Value::void())
|
|
|
|
}
|
|
|
|
|
2024-07-07 12:16:15 -05:00
|
|
|
fn prepare_struct_ctor(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
2024-09-17 10:59:03 -05:00
|
|
|
ctx: &mut Ctx,
|
2024-07-07 12:16:15 -05:00
|
|
|
ty: Option<&Expr>,
|
|
|
|
field_len: usize,
|
2024-07-08 11:08:58 -05:00
|
|
|
) -> (ty::Id, Loc) {
|
|
|
|
let Some(mut ty) = ty.map(|ty| self.ty(ty)).or(ctx.ty) else {
|
2024-07-07 12:16:15 -05:00
|
|
|
self.report(pos, "expected type, (it cannot be inferred)");
|
|
|
|
};
|
|
|
|
|
2024-10-13 06:30:00 -05:00
|
|
|
if let Some(expected) = ctx.ty {
|
2024-10-13 07:11:17 -05:00
|
|
|
_ = self.assert_ty(pos, ty, expected, TyCheck::Assign, "struct");
|
2024-10-13 06:30:00 -05:00
|
|
|
}
|
|
|
|
|
2024-07-08 11:08:58 -05:00
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Struct(stru) => {
|
2024-09-30 12:09:17 -05:00
|
|
|
let field_count = self.tys.struct_field_range(stru).len();
|
2024-07-08 11:08:58 -05:00
|
|
|
if field_count != field_len {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
format_args!("expected {field_count} fields, got {field_len}"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Kind::Slice(arr) => {
|
2024-10-01 14:36:23 -05:00
|
|
|
let arr = &self.tys.ins.arrays[arr as usize];
|
2024-07-08 11:08:58 -05:00
|
|
|
if arr.len == ArrayLen::MAX {
|
|
|
|
ty = self.tys.make_array(arr.ty, field_len as _);
|
|
|
|
} else if arr.len != field_len as u32 {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
|
|
|
"literal has {} elements, but explicit array type has {} elements",
|
|
|
|
arr.len, field_len
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-10-12 15:03:53 -05:00
|
|
|
_ => self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
2024-10-12 15:21:20 -05:00
|
|
|
"expected expression to evaluate to struct (or array maybe) but it evaluated to {}",
|
2024-10-12 15:03:53 -05:00
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
),
|
2024-07-07 12:16:15 -05:00
|
|
|
}
|
|
|
|
|
2024-07-08 11:08:58 -05:00
|
|
|
let size = self.tys.size_of(ty);
|
2024-09-17 10:59:03 -05:00
|
|
|
if ctx.loc.as_ref().map_or(true, |l| l.is_reg()) {
|
|
|
|
(ty, Loc::stack(self.ci.stack.allocate(size)))
|
|
|
|
} else {
|
|
|
|
(ty, ctx.loc.take().unwrap_or_else(|| Loc::stack(self.ci.stack.allocate(size))))
|
|
|
|
}
|
2024-07-07 12:16:15 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn struct_op(
|
|
|
|
&mut self,
|
|
|
|
op: TokenKind,
|
|
|
|
ty: ty::Id,
|
|
|
|
ctx: Ctx,
|
|
|
|
left: Loc,
|
|
|
|
mut right: Loc,
|
|
|
|
) -> Option<Value> {
|
|
|
|
if let ty::Kind::Struct(stuct) = ty.expand() {
|
|
|
|
let loc = ctx
|
|
|
|
.loc
|
|
|
|
.or_else(|| right.take_owned())
|
|
|
|
.unwrap_or_else(|| Loc::stack(self.ci.stack.allocate(self.tys.size_of(ty))));
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut oiter = OffsetIter::new(stuct, &self.tys);
|
2024-09-28 14:56:39 -05:00
|
|
|
while let Some((ty, offset)) = oiter.next_ty(&self.tys) {
|
2024-07-08 00:22:53 -05:00
|
|
|
let ctx = Ctx::from(Value { ty, loc: loc.as_ref().offset(offset) });
|
2024-06-23 02:09:33 -05:00
|
|
|
let left = left.as_ref().offset(offset);
|
|
|
|
let right = right.as_ref().offset(offset);
|
|
|
|
let value = self.struct_op(op, ty, ctx, left, right)?;
|
|
|
|
self.ci.free_loc(value.loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.free_loc(left);
|
|
|
|
self.ci.free_loc(right);
|
|
|
|
|
|
|
|
return Some(Value { ty, loc });
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = self.tys.size_of(ty);
|
|
|
|
let signed = ty.is_signed();
|
|
|
|
let lhs = self.loc_to_reg(left, size);
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if let Loc::Ct { value, derefed: false } = right
|
2024-09-15 13:14:56 -05:00
|
|
|
&& let Some(op) = op.imm_binop(signed, size)
|
2024-06-23 02:09:33 -05:00
|
|
|
{
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(lhs.get(), lhs.get(), value.0));
|
2024-06-23 02:09:33 -05:00
|
|
|
return Some(if let Some(value) = ctx.into_value() {
|
|
|
|
self.store_typed(Loc::reg(lhs.as_ref()), value.loc, value.ty);
|
|
|
|
Value::void()
|
|
|
|
} else {
|
2024-07-08 00:22:53 -05:00
|
|
|
Value { ty, loc: Loc::reg(lhs) }
|
2024-06-23 02:09:33 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let rhs = self.loc_to_reg(right, size);
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
if let Some(op) = op.binop(signed, size) {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(op(lhs.get(), lhs.get(), rhs.get()));
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.free(rhs);
|
|
|
|
return if let Some(value) = ctx.into_value() {
|
|
|
|
self.store_typed(Loc::reg(lhs), value.loc, value.ty);
|
|
|
|
Some(Value::void())
|
|
|
|
} else {
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty, loc: Loc::reg(lhs) })
|
2024-06-23 02:09:33 -05:00
|
|
|
};
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
unimplemented!("{:#?}", op)
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn handle_global(&mut self, id: ty::Global) -> Option<Value> {
|
|
|
|
let ptr = self.ci.regs.allocate();
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let reloc = Reloc::new(self.ci.code.len(), 3, 4);
|
2024-10-01 14:39:23 -05:00
|
|
|
let global = &mut self.tys.ins.globals[id as usize];
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.relocs.push(TypedReloc { target: ty::Kind::Global(id).compress(), reloc });
|
|
|
|
self.ci.emit(instrs::lra(ptr.get(), 0, 0));
|
2024-05-16 06:29:16 -05:00
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
Some(Value { ty: global.ty, loc: Loc::reg(ptr).into_derefed() })
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn spill(&mut self, loc: Loc, size: Size) -> Loc {
|
2024-09-09 12:36:53 -05:00
|
|
|
if loc.is_ref() || !loc.is_stack() {
|
|
|
|
let stack = Loc::stack(self.ci.stack.allocate(size));
|
|
|
|
self.store_sized(loc, &stack, size);
|
|
|
|
stack
|
|
|
|
} else {
|
|
|
|
loc
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_loc_owned(&mut self, loc: Loc, ty: ty::Id) -> Loc {
|
|
|
|
let size = self.tys.size_of(ty);
|
|
|
|
match size {
|
|
|
|
0 => Loc::default(),
|
|
|
|
1..=8 => Loc::reg(self.loc_to_reg(loc, size)),
|
|
|
|
_ if loc.is_ref() => {
|
|
|
|
let new_loc = Loc::stack(self.ci.stack.allocate(size));
|
|
|
|
self.store_sized(loc, &new_loc, size);
|
|
|
|
new_loc
|
|
|
|
}
|
|
|
|
_ => loc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
fn complete_call_graph(&mut self) {
|
2024-06-23 02:09:33 -05:00
|
|
|
while self.ci.task_base < self.tasks.len()
|
|
|
|
&& let Some(task_slot) = self.tasks.pop()
|
2024-05-16 06:29:16 -05:00
|
|
|
{
|
2024-06-23 02:09:33 -05:00
|
|
|
let Some(task) = task_slot else { continue };
|
|
|
|
self.handle_task(task);
|
2024-05-16 06:29:16 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-16 06:29:16 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
fn handle_task(&mut self, FTask { file, id }: FTask) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let func = &self.tys.ins.funcs[id as usize];
|
2024-09-01 16:51:59 -05:00
|
|
|
debug_assert!(func.file == file);
|
2024-06-24 10:26:00 -05:00
|
|
|
let sig = func.sig.unwrap();
|
|
|
|
let ast = self.files[file as usize].clone();
|
|
|
|
let expr = func.expr.get(&ast).unwrap();
|
2024-07-19 05:00:55 -05:00
|
|
|
let ct_stack_base = self.ct.vm.read_reg(reg::STACK_PTR).0;
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let repl = ItemCtx {
|
|
|
|
file,
|
|
|
|
id: ty::Kind::Func(id),
|
2024-07-19 05:00:55 -05:00
|
|
|
ret: Some(sig.ret),
|
2024-06-23 02:09:33 -05:00
|
|
|
..self.pool.cis.pop().unwrap_or_default()
|
|
|
|
};
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_ci = core::mem::replace(&mut self.ci, repl);
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.init();
|
|
|
|
|
|
|
|
let Expr::BinOp {
|
2024-09-09 12:36:53 -05:00
|
|
|
left: Expr::Ident { .. },
|
2024-06-23 02:09:33 -05:00
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { body, args, .. },
|
|
|
|
} = expr
|
|
|
|
else {
|
2024-09-30 12:09:17 -05:00
|
|
|
unreachable!("{}", self.ast_display(expr))
|
2024-06-23 02:09:33 -05:00
|
|
|
};
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit_prelude();
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
let mut parama = self.tys.parama(sig.ret);
|
2024-06-24 10:26:00 -05:00
|
|
|
let mut sig_args = sig.args.range();
|
|
|
|
for arg in args.iter() {
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[sig_args.next().unwrap()];
|
2024-09-09 12:36:53 -05:00
|
|
|
let sym = parser::find_symbol(&ast.symbols, arg.id).flags;
|
|
|
|
let loc = match sym & idfl::COMPTIME != 0 {
|
2024-10-01 15:53:03 -05:00
|
|
|
true => Loc::ty(self.tys.ins.args[sig_args.next().unwrap()]),
|
2024-09-09 12:36:53 -05:00
|
|
|
false => self.load_arg(sym, ty, &mut parama),
|
2024-06-24 10:26:00 -05:00
|
|
|
};
|
2024-09-20 09:37:51 -05:00
|
|
|
self.ci.vars.push(Variable { id: arg.id, value: Value { ty, loc } });
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if self.tys.size_of(sig.ret) > 16 {
|
2024-06-23 02:09:33 -05:00
|
|
|
let reg = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(instrs::cp(reg.get(), 1));
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.ret_reg = reg;
|
|
|
|
} else {
|
|
|
|
self.ci.ret_reg = reg::Id::RET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.expr(body).is_some() {
|
|
|
|
self.report(body.pos(), "expected all paths in the fucntion to return");
|
|
|
|
}
|
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let mut vars = core::mem::take(&mut self.ci.vars);
|
2024-10-01 08:28:18 -05:00
|
|
|
for var in vars.drain(..) {
|
|
|
|
self.ci.free_loc(var.value.loc);
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-10-01 08:28:18 -05:00
|
|
|
self.ci.vars = vars;
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.finalize();
|
|
|
|
self.ci.emit(jala(ZERO, RET_ADDR, 0));
|
2024-09-30 12:09:17 -05:00
|
|
|
self.ci.regs.free(core::mem::take(&mut self.ci.ret_reg));
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.ins.funcs[id as usize].code.append(&mut self.ci.code);
|
2024-10-04 14:44:29 -05:00
|
|
|
self.tys.ins.funcs[id as usize].relocs = self.ci.relocs.drain(..).collect();
|
2024-09-30 12:09:17 -05:00
|
|
|
self.pool.cis.push(core::mem::replace(&mut self.ci, prev_ci));
|
2024-07-19 05:00:55 -05:00
|
|
|
self.ct.vm.write_reg(reg::STACK_PTR, ct_stack_base);
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn load_arg(&mut self, flags: parser::IdentFlags, ty: ty::Id, parama: &mut ParamAlloc) -> Loc {
|
|
|
|
let size = self.tys.size_of(ty) as Size;
|
2024-06-25 12:55:25 -05:00
|
|
|
if size == 0 {
|
|
|
|
return Loc::default();
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
let (src, dst) = match size {
|
|
|
|
0 => (Loc::default(), Loc::default()),
|
|
|
|
..=8 if flags & idfl::REFERENCED == 0 => {
|
|
|
|
(Loc::reg(parama.next()), Loc::reg(self.ci.regs.allocate()))
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
1..=8 => (Loc::reg(parama.next()), Loc::stack(self.ci.stack.allocate(size))),
|
|
|
|
9..=16 => (Loc::reg(parama.next_wide()), Loc::stack(self.ci.stack.allocate(size))),
|
2024-06-23 02:09:33 -05:00
|
|
|
_ if flags & (idfl::MUTABLE | idfl::REFERENCED) == 0 => {
|
|
|
|
let ptr = parama.next();
|
|
|
|
let reg = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(instrs::cp(reg.get(), ptr));
|
2024-06-23 02:09:33 -05:00
|
|
|
return Loc::reg(reg).into_derefed();
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
_ => (Loc::reg(parama.next()).into_derefed(), Loc::stack(self.ci.stack.allocate(size))),
|
2024-06-23 02:09:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.store_sized(src, &dst, size);
|
|
|
|
dst
|
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn eca(&mut self, trap: trap::Trap, ret: impl Into<ty::Id>) -> Value {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.write_trap(trap);
|
2024-07-08 00:22:53 -05:00
|
|
|
Value { ty: ret.into(), loc: Loc::reg(1) }
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
|
2024-09-01 19:38:11 -05:00
|
|
|
fn alloc_ret(&mut self, ret: ty::Id, ctx: Ctx, custom_ret_reg: bool) -> Loc {
|
2024-06-23 02:09:33 -05:00
|
|
|
let size = self.tys.size_of(ret);
|
2024-09-01 15:07:45 -05:00
|
|
|
if size == 0 {
|
|
|
|
debug_assert!(ctx.loc.is_none(), "{}", self.ty_display(ret));
|
|
|
|
return Loc::default();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.loc.is_some() && size < 16 {
|
|
|
|
return ctx.loc.unwrap();
|
|
|
|
}
|
|
|
|
|
2024-05-12 13:10:50 -05:00
|
|
|
match size {
|
2024-06-23 02:09:33 -05:00
|
|
|
0 => Loc::default(),
|
2024-09-01 19:38:11 -05:00
|
|
|
1..=8 if custom_ret_reg => Loc::reg(self.ci.regs.allocate()),
|
2024-06-23 02:09:33 -05:00
|
|
|
1..=8 => Loc::reg(1),
|
|
|
|
9..=16 => Loc::stack(self.ci.stack.allocate(size)),
|
2024-09-01 19:38:11 -05:00
|
|
|
17.. => {
|
2024-07-08 00:22:53 -05:00
|
|
|
let loc = ctx.loc.unwrap_or_else(|| Loc::stack(self.ci.stack.allocate(size)));
|
|
|
|
let Loc::Rt { reg, stack, offset, .. } = &loc else {
|
2024-06-24 10:45:58 -05:00
|
|
|
todo!("old man with the beard looks at the sky scared");
|
|
|
|
};
|
|
|
|
self.stack_offset(1, reg.get(), stack.as_ref(), *offset);
|
|
|
|
loc
|
2024-05-12 04:52:58 -05:00
|
|
|
}
|
2024-05-11 05:51:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:26:03 -05:00
|
|
|
fn loc_to_reg(&mut self, loc: impl Into<LocCow>, size: Size) -> reg::Id {
|
|
|
|
match loc.into() {
|
2024-07-08 00:22:53 -05:00
|
|
|
LocCow::Owned(Loc::Rt { derefed: false, mut reg, offset, stack }) => {
|
2024-06-23 02:09:33 -05:00
|
|
|
debug_assert!(stack.is_none(), "TODO");
|
|
|
|
assert_eq!(offset, 0, "TODO");
|
|
|
|
if reg.is_ref() {
|
|
|
|
let new_reg = self.ci.regs.allocate();
|
2024-07-20 11:52:24 -05:00
|
|
|
debug_assert_ne!(reg.get(), 0);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(cp(new_reg.get(), reg.get()));
|
2024-06-23 02:09:33 -05:00
|
|
|
reg = new_reg;
|
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
reg
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
LocCow::Ref(&Loc::Rt { derefed: false, ref reg, offset, ref stack }) => {
|
2024-06-23 02:26:03 -05:00
|
|
|
debug_assert!(stack.is_none(), "TODO");
|
|
|
|
assert_eq!(offset, 0, "TODO");
|
|
|
|
reg.as_ref()
|
|
|
|
}
|
2024-06-23 06:55:48 -05:00
|
|
|
loc => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let reg = self.ci.regs.allocate();
|
|
|
|
self.store_sized(loc, Loc::reg(reg.as_ref()), size);
|
|
|
|
reg
|
2024-05-12 13:10:50 -05:00
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn load_ret(&mut self, ty: ty::Id, loc: &Loc) {
|
|
|
|
let size = self.tys.size_of(ty);
|
|
|
|
if let 1..=16 = size {
|
|
|
|
self.store_sized(Loc::reg(1), loc, size);
|
|
|
|
}
|
|
|
|
}
|
2024-06-01 13:30:07 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn pass_arg(&mut self, value: &Value, parama: &mut ParamAlloc) {
|
|
|
|
self.pass_arg_low(&value.loc, self.tys.size_of(value.ty), parama)
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn pass_arg_low(&mut self, loc: &Loc, size: Size, parama: &mut ParamAlloc) {
|
|
|
|
if size > 16 {
|
2024-07-08 00:22:53 -05:00
|
|
|
let Loc::Rt { reg, stack, offset, .. } = loc else { unreachable!() };
|
2024-06-23 06:55:48 -05:00
|
|
|
self.stack_offset(parama.next(), reg.get(), stack.as_ref(), *offset as _);
|
2024-06-23 02:09:33 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let dst = match size {
|
|
|
|
0 => return,
|
|
|
|
9..=16 => Loc::reg(parama.next_wide()),
|
|
|
|
_ => Loc::reg(parama.next()),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.store_sized(loc, dst, size);
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn store_typed(&mut self, src: impl Into<LocCow>, dst: impl Into<LocCow>, ty: ty::Id) {
|
|
|
|
self.store_sized(src, dst, self.tys.size_of(ty) as _)
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn store_sized(&mut self, src: impl Into<LocCow>, dst: impl Into<LocCow>, size: Size) {
|
|
|
|
self.store_sized_low(src.into(), dst.into(), size);
|
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
|
2024-10-13 08:49:14 -05:00
|
|
|
fn store_sized_low(&mut self, src: LocCow, dst: LocCow, mut size: Size) {
|
2024-06-23 02:09:33 -05:00
|
|
|
macro_rules! lpat {
|
|
|
|
($der:literal, $reg:ident, $off:pat, $sta:pat) => {
|
|
|
|
&Loc::Rt { derefed: $der, reg: ref $reg, offset: $off, stack: $sta }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:55:25 -05:00
|
|
|
if size == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
src.as_ref().assert_valid();
|
|
|
|
dst.as_ref().assert_valid();
|
|
|
|
|
|
|
|
match (src.as_ref(), dst.as_ref()) {
|
2024-07-19 05:00:55 -05:00
|
|
|
(&Loc::Ct { value, derefed }, lpat!(true, reg, off, ref sta)) => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let ct = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(li64(ct.get(), ensure_loaded(value, derefed, size)));
|
2024-06-23 02:09:33 -05:00
|
|
|
let off = self.opt_stack_reloc(sta.as_ref(), off, 3);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(st(ct.get(), reg.get(), off, size as _));
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.free(ct);
|
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
(&Loc::Ct { value, derefed }, lpat!(false, reg, 0, None)) => {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(li64(reg.get(), ensure_loaded(value, derefed, size)))
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
(&Loc::Ct { value, derefed }, lpat!(false, reg, 8, None))
|
|
|
|
if reg.get() == 1 && size == 8 =>
|
|
|
|
{
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(li64(reg.get() + 1, ensure_loaded(value, derefed, size)));
|
2024-07-18 10:55:55 -05:00
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
(&Loc::Ct { value, derefed }, lpat!(false, reg, off, None)) if reg.get() == 1 => {
|
2024-07-18 10:55:55 -05:00
|
|
|
let freg = reg.get() + (off / 8) as u8;
|
|
|
|
let mask = !(((1u64 << (8 * size)) - 1) << (8 * (off % 8)));
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(andi(freg, freg, mask));
|
2024-07-19 05:00:55 -05:00
|
|
|
let value = ensure_loaded(value, derefed, size) << (8 * (off % 8));
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(ori(freg, freg, value));
|
2024-07-18 10:55:55 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
(lpat!(true, src, soff, ref ssta), lpat!(true, dst, doff, ref dsta)) => {
|
|
|
|
// TODO: some oportuinies to ellit more optimal code
|
2024-09-09 18:15:18 -05:00
|
|
|
let src_off = if src.is_ref() { self.ci.regs.allocate() } else { src.as_ref() };
|
|
|
|
let dst_off = if dst.is_ref() { self.ci.regs.allocate() } else { dst.as_ref() };
|
2024-06-23 06:55:48 -05:00
|
|
|
self.stack_offset(src_off.get(), src.get(), ssta.as_ref(), soff);
|
|
|
|
self.stack_offset(dst_off.get(), dst.get(), dsta.as_ref(), doff);
|
2024-10-13 08:49:14 -05:00
|
|
|
loop {
|
|
|
|
match u16::try_from(size) {
|
|
|
|
Ok(o) => {
|
|
|
|
self.ci.emit(bmc(src_off.get(), dst_off.get(), o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
self.ci.emit(bmc(src_off.get(), dst_off.get(), u16::MAX));
|
|
|
|
self.ci.emit(addi64(src_off.get(), src_off.get(), u16::MAX as _));
|
|
|
|
self.ci.emit(addi64(dst_off.get(), dst_off.get(), u16::MAX as _));
|
|
|
|
size -= u16::MAX as u32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.regs.free(src_off);
|
|
|
|
self.ci.regs.free(dst_off);
|
|
|
|
}
|
|
|
|
(lpat!(false, src, 0, None), lpat!(false, dst, 0, None)) => {
|
|
|
|
if src != dst {
|
2024-07-20 11:52:24 -05:00
|
|
|
debug_assert_ne!(src.get(), 0);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(cp(dst.get(), src.get()));
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
(lpat!(true, src, soff, ref ssta), lpat!(false, dst, 0, None)) => {
|
|
|
|
if size < 8 {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(cp(dst.get(), 0));
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
let off = self.opt_stack_reloc(ssta.as_ref(), soff, 3);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(ld(dst.get(), src.get(), off, size as _));
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
(lpat!(false, src, 0, None), lpat!(true, dst, doff, ref dsta)) => {
|
|
|
|
let off = self.opt_stack_reloc(dsta.as_ref(), doff, 3);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(st(src.get(), dst.get(), off, size as _))
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
|
|
|
(a, b) => unreachable!("{a:?} {b:?}"),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.free_loc(src);
|
|
|
|
self.ci.free_loc(dst);
|
|
|
|
}
|
|
|
|
|
2024-06-23 06:55:48 -05:00
|
|
|
fn stack_offset(&mut self, dst: u8, op: u8, stack: Option<&stack::Id>, off: Offset) {
|
2024-06-23 02:09:33 -05:00
|
|
|
let Some(stack) = stack else {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit_addi(dst, op, off as _);
|
2024-06-23 02:09:33 -05:00
|
|
|
return;
|
2024-05-20 07:11:58 -05:00
|
|
|
};
|
2024-06-23 02:09:33 -05:00
|
|
|
|
|
|
|
let off = self.stack_reloc(stack, off, 3);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(addi64(dst, op, off));
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
fn opt_stack_reloc(&mut self, stack: Option<&stack::Id>, off: Offset, sub_offset: u8) -> u64 {
|
2024-07-08 00:22:53 -05:00
|
|
|
stack.map(|s| self.stack_reloc(s, off, sub_offset)).unwrap_or(off as _)
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
fn stack_reloc(&mut self, stack: &stack::Id, off: Offset, sub_offset: u8) -> u64 {
|
2024-09-13 11:22:27 -05:00
|
|
|
let offset = self.ci.code.len();
|
2024-09-03 19:35:09 -05:00
|
|
|
self.ci.stack_relocs.push(Reloc::new(offset, sub_offset, 8));
|
2024-06-23 02:09:33 -05:00
|
|
|
Reloc::pack_srel(stack, off)
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn ty(&mut self, expr: &Expr) -> ty::Id {
|
2024-10-01 10:43:15 -05:00
|
|
|
let ty = self.tys.ty(self.ci.file, expr, &self.files);
|
|
|
|
let evaled_ty = ty::Id::from(self.eval_const(expr, ty::TYPE));
|
|
|
|
if let Some(ty) = ty {
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty,
|
|
|
|
evaled_ty,
|
|
|
|
"{} {}",
|
|
|
|
self.ty_display(ty),
|
|
|
|
self.ty_display(evaled_ty)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
evaled_ty
|
2024-05-12 06:13:36 -05:00
|
|
|
}
|
|
|
|
|
2024-09-03 19:35:09 -05:00
|
|
|
fn read_trap(addr: u64) -> Option<&'static trap::Trap> {
|
|
|
|
// TODO: make this debug only
|
|
|
|
if unsafe { *(addr as *const u8) } != 255 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(unsafe { &*((addr + 1) as *const trap::Trap) })
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn handle_ecall(&mut self) {
|
2024-09-03 19:35:09 -05:00
|
|
|
let trap = Self::read_trap(self.ct.vm.pc.get()).unwrap();
|
|
|
|
self.ct.vm.pc = self.ct.vm.pc.wrapping_add(trap.size() + 1);
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-09-20 09:37:51 -05:00
|
|
|
let mut code_index = self.ct.vm.pc.get() as usize - self.ct.code.as_ptr() as usize;
|
|
|
|
debug_assert!(code_index < self.ct.code.len());
|
2024-07-07 12:16:15 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
match *trap {
|
|
|
|
trap::Trap::MakeStruct(trap::MakeStruct { file, struct_expr }) => {
|
2024-06-23 02:09:33 -05:00
|
|
|
let cfile = self.files[file as usize].clone();
|
2024-09-22 11:17:30 -05:00
|
|
|
let &Expr::Struct { fields, captured, packed, .. } =
|
|
|
|
struct_expr.get(&cfile).unwrap()
|
2024-06-23 02:09:33 -05:00
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2024-05-12 13:10:50 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let prev_len = self.ci.vars.len();
|
2024-05-12 13:10:50 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let mut values = self.ct.vm.read_reg(2).0 as *const u8;
|
|
|
|
for &id in captured {
|
2024-09-30 12:09:17 -05:00
|
|
|
let ty: ty::Id = unsafe { core::ptr::read_unaligned(values.cast()) };
|
2024-06-23 02:09:33 -05:00
|
|
|
unsafe { values = values.add(4) };
|
|
|
|
let size = self.tys.size_of(ty) as usize;
|
|
|
|
let mut imm = [0u8; 8];
|
|
|
|
assert!(size <= imm.len(), "TODO");
|
2024-09-30 12:09:17 -05:00
|
|
|
unsafe { core::ptr::copy_nonoverlapping(values, imm.as_mut_ptr(), size) };
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.vars.push(Variable {
|
|
|
|
id,
|
2024-07-19 05:00:55 -05:00
|
|
|
value: Value::new(ty, Loc::ct(u64::from_ne_bytes(imm))),
|
2024-06-23 02:09:33 -05:00
|
|
|
});
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let stru = ty::Kind::Struct(self.build_struct(
|
|
|
|
self.ci.file,
|
|
|
|
packed.then_some(1),
|
|
|
|
None,
|
|
|
|
fields,
|
|
|
|
))
|
|
|
|
.compress();
|
2024-06-23 02:09:33 -05:00
|
|
|
self.ci.vars.truncate(prev_len);
|
|
|
|
self.ct.vm.write_reg(1, stru.repr() as u64);
|
2024-05-12 13:10:50 -05:00
|
|
|
}
|
2024-07-19 05:00:55 -05:00
|
|
|
trap::Trap::MomizedCall(trap::MomizedCall { func }) => {
|
2024-10-01 15:53:03 -05:00
|
|
|
if let Some(ty) = self.tys.ins.funcs[func as usize].computed {
|
2024-07-19 05:00:55 -05:00
|
|
|
self.ct.vm.write_reg(1, ty.repr());
|
|
|
|
} else {
|
|
|
|
self.run_vm();
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.ins.funcs[func as usize].computed =
|
|
|
|
Some(self.ct.vm.read_reg(1).0.into());
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
2024-09-20 09:37:51 -05:00
|
|
|
code_index += jal(0, 0, 0).0 + tx().0;
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
2024-05-12 13:10:50 -05:00
|
|
|
}
|
2024-07-07 11:21:07 -05:00
|
|
|
|
2024-09-20 09:37:51 -05:00
|
|
|
let offset = code_index + self.ct.code.as_ptr() as usize;
|
|
|
|
self.ct.vm.pc = hbvm::mem::Address::new(offset as _);
|
2024-05-12 13:10:50 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn find_or_declare(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
|
|
|
file: FileId,
|
|
|
|
name: Result<Ident, &str>,
|
|
|
|
lit_name: &str,
|
|
|
|
) -> ty::Kind {
|
2024-09-30 12:27:00 -05:00
|
|
|
log::trace!("find_or_declare: {lit_name} {file}");
|
2024-10-01 10:43:15 -05:00
|
|
|
if let Some(ty) = self.tys.find_type(file, name, &self.files) {
|
|
|
|
return ty.expand();
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
let f = self.files[file as usize].clone();
|
|
|
|
let Some((expr, ident)) = f.find_decl(name) else {
|
|
|
|
match name {
|
2024-10-13 05:25:12 -05:00
|
|
|
Ok(_) => self.report(pos, format_args!("undefined identifier: {lit_name}")),
|
2024-09-30 12:09:17 -05:00
|
|
|
Err("main") => self.report(pos, format_args!("missing main function")),
|
2024-09-01 16:14:48 -05:00
|
|
|
Err(name) => self.report(pos, format_args!("undefined indentifier: {name}")),
|
2024-05-12 06:13:36 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
};
|
2024-05-12 16:19:45 -05:00
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let key = SymKey::Decl(file, ident);
|
2024-10-01 15:53:03 -05:00
|
|
|
if let Some(existing) = self.tys.syms.get(key, &self.tys.ins) {
|
2024-06-23 02:09:33 -05:00
|
|
|
if let ty::Kind::Func(id) = existing.expand()
|
2024-10-01 15:53:03 -05:00
|
|
|
&& let func = &mut self.tys.ins.funcs[id as usize]
|
2024-06-23 02:09:33 -05:00
|
|
|
&& let Err(idx) = task::unpack(func.offset)
|
2024-09-13 11:22:27 -05:00
|
|
|
&& idx < self.tasks.len()
|
2024-06-23 02:09:33 -05:00
|
|
|
{
|
|
|
|
func.offset = task::id(self.tasks.len());
|
|
|
|
let task = self.tasks[idx].take();
|
|
|
|
self.tasks.push(task);
|
|
|
|
}
|
|
|
|
return existing.expand();
|
2024-05-12 16:45:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_file = core::mem::replace(&mut self.ci.file, file);
|
2024-06-23 02:09:33 -05:00
|
|
|
let sym = match expr {
|
|
|
|
Expr::BinOp {
|
2024-10-01 15:53:03 -05:00
|
|
|
left: &Expr::Ident { id, .. },
|
2024-06-23 02:09:33 -05:00
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { pos, args, ret, .. },
|
|
|
|
} => {
|
|
|
|
let func = Func {
|
2024-07-06 16:02:04 -05:00
|
|
|
file,
|
2024-10-01 15:53:03 -05:00
|
|
|
name: id,
|
2024-07-06 16:02:04 -05:00
|
|
|
sig: 'b: {
|
2024-10-12 15:21:20 -05:00
|
|
|
let arg_base = self.tys.tmp.args.len();
|
2024-06-23 02:09:33 -05:00
|
|
|
for arg in args {
|
2024-06-24 10:26:00 -05:00
|
|
|
let sym = find_symbol(&self.files[file as usize].symbols, arg.id);
|
|
|
|
if sym.flags & idfl::COMPTIME != 0 {
|
2024-10-13 06:30:00 -05:00
|
|
|
self.tys.tmp.args.truncate(arg_base);
|
2024-06-24 10:26:00 -05:00
|
|
|
break 'b None;
|
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
let ty = self.ty(&arg.ty);
|
2024-10-12 15:21:20 -05:00
|
|
|
self.tys.tmp.args.push(ty);
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
|
|
|
|
let args = self.pack_args(pos, arg_base);
|
|
|
|
let ret = self.ty(ret);
|
2024-07-20 11:52:24 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
Some(Sig { args, ret })
|
2024-06-23 02:09:33 -05:00
|
|
|
},
|
2024-09-01 16:51:59 -05:00
|
|
|
expr: {
|
|
|
|
let refr = ExprRef::new(expr);
|
|
|
|
debug_assert!(refr.get(&f).is_some());
|
|
|
|
refr
|
|
|
|
},
|
2024-09-13 11:22:27 -05:00
|
|
|
..Default::default()
|
2024-05-14 16:07:32 -05:00
|
|
|
};
|
2024-09-01 16:51:59 -05:00
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let id = self.tys.ins.funcs.len() as _;
|
|
|
|
self.tys.ins.funcs.push(func);
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
ty::Kind::Func(id)
|
|
|
|
}
|
|
|
|
Expr::BinOp {
|
2024-09-30 15:47:00 -05:00
|
|
|
left: &Expr::Ident { id, .. },
|
2024-06-23 02:09:33 -05:00
|
|
|
op: TokenKind::Decl,
|
2024-09-30 15:15:40 -05:00
|
|
|
right: stru @ Expr::Struct { .. },
|
2024-09-30 15:47:00 -05:00
|
|
|
} => {
|
|
|
|
let str = self.ty(stru).expand().inner();
|
2024-10-01 14:36:23 -05:00
|
|
|
self.tys.ins.structs[str as usize].name = id;
|
2024-09-30 15:47:00 -05:00
|
|
|
ty::Kind::Struct(str)
|
|
|
|
}
|
2024-07-08 00:22:53 -05:00
|
|
|
Expr::BinOp { left, op: TokenKind::Decl, right } => {
|
2024-10-01 14:39:23 -05:00
|
|
|
let gid = self.tys.ins.globals.len() as ty::Global;
|
|
|
|
self.tys.ins.globals.push(Global { file, name: ident, ..Default::default() });
|
2024-05-16 09:50:29 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
let ci = ItemCtx {
|
|
|
|
file,
|
2024-09-03 19:35:09 -05:00
|
|
|
id: ty::Kind::Builtin(u32::MAX),
|
2024-06-24 10:26:00 -05:00
|
|
|
..self.pool.cis.pop().unwrap_or_default()
|
|
|
|
};
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
_ = left.find_pattern_path(ident, right, |expr| {
|
2024-10-01 14:39:23 -05:00
|
|
|
self.tys.ins.globals[gid as usize] = self
|
2024-09-03 19:35:09 -05:00
|
|
|
.ct_eval(ci, |s, _| Ok::<_, !>(s.generate_global(expr, file, ident)))
|
|
|
|
.into_ok();
|
2024-07-08 00:22:53 -05:00
|
|
|
});
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
ty::Kind::Global(gid)
|
|
|
|
}
|
|
|
|
e => unimplemented!("{e:#?}"),
|
|
|
|
};
|
2024-07-20 11:52:24 -05:00
|
|
|
self.ci.file = prev_file;
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.syms.insert(key, sym.compress(), &self.tys.ins);
|
2024-06-24 10:26:00 -05:00
|
|
|
sym
|
|
|
|
}
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-09-01 19:38:11 -05:00
|
|
|
fn make_func_reachable(&mut self, func: ty::Func) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &mut self.tys.ins.funcs[func as usize];
|
2024-09-01 19:38:11 -05:00
|
|
|
if fuc.offset == u32::MAX {
|
|
|
|
fuc.offset = task::id(self.tasks.len() as _);
|
|
|
|
self.tasks.push(Some(FTask { file: fuc.file, id: func }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 19:35:09 -05:00
|
|
|
fn generate_global(&mut self, expr: &Expr, file: FileId, name: Ident) -> Global {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit_prelude();
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
let ret = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(instrs::cp(ret.get(), 1));
|
2024-06-24 10:26:00 -05:00
|
|
|
self.ci.task_base = self.tasks.len();
|
|
|
|
|
|
|
|
let ctx = Ctx::default().with_loc(Loc::reg(ret).into_derefed());
|
|
|
|
let Some(ret) = self.expr_ctx(expr, ctx) else {
|
|
|
|
self.report(expr.pos(), "expression is not reachable");
|
|
|
|
};
|
2024-05-20 07:11:58 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.complete_call_graph();
|
2024-06-12 09:29:41 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
let mut data = vec![0; self.tys.size_of(ret.ty) as usize];
|
2024-05-13 04:05:35 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.finalize();
|
|
|
|
self.ci.emit(tx());
|
2024-05-13 04:05:35 -05:00
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ct.vm.write_reg(1, data.as_mut_ptr() as u64);
|
2024-05-14 05:17:39 -05:00
|
|
|
|
2024-09-01 15:07:45 -05:00
|
|
|
self.ci.free_loc(ret.loc);
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
Global { ty: ret.ty, file, name, data, ..Default::default() }
|
2024-06-24 10:26:00 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
fn ct_eval<T, E>(
|
|
|
|
&mut self,
|
|
|
|
ci: ItemCtx,
|
|
|
|
compile: impl FnOnce(&mut Self, &mut ItemCtx) -> Result<T, E>,
|
|
|
|
) -> Result<T, E> {
|
2024-09-30 12:27:00 -05:00
|
|
|
log::trace!("eval");
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut prev_ci = core::mem::replace(&mut self.ci, ci);
|
2024-06-24 10:26:00 -05:00
|
|
|
self.ci.task_base = self.tasks.len();
|
|
|
|
self.ci.regs.init();
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
let ret = compile(self, &mut prev_ci);
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut rr = core::mem::take(&mut self.ci.ret_reg);
|
2024-07-19 05:00:55 -05:00
|
|
|
let is_on_stack = !rr.is_ref();
|
|
|
|
if !rr.is_ref() {
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(instrs::cp(1, rr.get()));
|
2024-07-19 05:00:55 -05:00
|
|
|
let rref = rr.as_ref();
|
2024-09-30 12:09:17 -05:00
|
|
|
self.ci.regs.free(core::mem::replace(&mut rr, rref));
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
2024-06-23 02:09:33 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
if ret.is_ok() {
|
2024-10-01 15:53:03 -05:00
|
|
|
let last_fn = self.tys.ins.funcs.len();
|
|
|
|
self.tys.ins.funcs.push(Default::default());
|
2024-09-13 11:22:27 -05:00
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.ins.funcs[last_fn].code = core::mem::take(&mut self.ci.code);
|
|
|
|
self.tys.ins.funcs[last_fn].relocs = core::mem::take(&mut self.ci.relocs);
|
2024-09-13 11:22:27 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
if is_on_stack {
|
|
|
|
let size =
|
|
|
|
self.tys.size_of(self.ci.ret.expect("you have died (colaterall fuck up)"));
|
|
|
|
let slot = self.ct.vm.read_reg(reg::STACK_PTR).0;
|
|
|
|
self.ct.vm.write_reg(reg::STACK_PTR, slot.wrapping_add(size as _));
|
|
|
|
self.ct.vm.write_reg(1, slot);
|
|
|
|
}
|
|
|
|
|
2024-09-13 11:22:27 -05:00
|
|
|
self.tys.dump_reachable(last_fn as _, &mut self.ct.code);
|
2024-10-01 15:53:03 -05:00
|
|
|
let entry =
|
|
|
|
&mut self.ct.code[self.tys.ins.funcs[last_fn].offset as usize] as *mut _ as _;
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_pc = core::mem::replace(&mut self.ct.vm.pc, hbvm::mem::Address::new(entry))
|
2024-09-20 09:37:51 -05:00
|
|
|
- self.ct.code.as_ptr() as usize;
|
2024-09-03 19:35:09 -05:00
|
|
|
|
2024-09-09 16:31:22 -05:00
|
|
|
#[cfg(debug_assertions)]
|
2024-09-03 19:35:09 -05:00
|
|
|
{
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut vc = String::new();
|
2024-09-13 11:22:27 -05:00
|
|
|
if let Err(e) = self.tys.disasm(&self.ct.code, &self.files, &mut vc, |bts| {
|
|
|
|
if let Some(trap) = Self::read_trap(bts.as_ptr() as _) {
|
|
|
|
bts.take(..trap.size() + 1).unwrap();
|
|
|
|
}
|
|
|
|
}) {
|
2024-09-30 12:09:17 -05:00
|
|
|
panic!("{e} {}", vc);
|
2024-09-09 16:31:22 -05:00
|
|
|
} else {
|
2024-09-30 12:27:00 -05:00
|
|
|
log::trace!("{}", vc);
|
2024-09-04 10:56:59 -05:00
|
|
|
}
|
2024-09-03 19:35:09 -05:00
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
self.run_vm();
|
2024-09-20 09:37:51 -05:00
|
|
|
self.ct.vm.pc = prev_pc + self.ct.code.as_ptr() as usize;
|
2024-09-13 11:22:27 -05:00
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let func = self.tys.ins.funcs.pop().unwrap();
|
2024-10-01 08:28:18 -05:00
|
|
|
self.ci.code = func.code;
|
|
|
|
self.ci.code.clear();
|
|
|
|
self.ci.relocs = func.relocs;
|
|
|
|
self.ci.relocs.clear();
|
2024-06-24 10:26:00 -05:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
self.pool.cis.push(core::mem::replace(&mut self.ci, prev_ci));
|
2024-06-24 10:26:00 -05:00
|
|
|
|
2024-09-30 12:27:00 -05:00
|
|
|
log::trace!("eval-end");
|
2024-07-06 16:02:04 -05:00
|
|
|
|
2024-06-24 10:26:00 -05:00
|
|
|
ret
|
2024-05-13 04:05:35 -05:00
|
|
|
}
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
pub fn disasm(&mut self, output: &mut String) -> Result<(), DisasmError> {
|
2024-09-13 11:22:27 -05:00
|
|
|
let mut bin = Vec::new();
|
2024-09-13 13:31:05 -05:00
|
|
|
self.assemble(&mut bin);
|
2024-09-13 11:22:27 -05:00
|
|
|
self.tys.disasm(&bin, &self.files, output, |_| {})
|
2024-09-03 19:35:09 -05:00
|
|
|
}
|
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
fn run_vm(&mut self) {
|
|
|
|
loop {
|
2024-10-12 06:07:49 -05:00
|
|
|
match self.ct.vm.run().unwrap_or_else(|e| panic!("{e:?}")) {
|
2024-07-19 05:00:55 -05:00
|
|
|
hbvm::VmRunOk::End => break,
|
|
|
|
hbvm::VmRunOk::Timer => unreachable!(),
|
|
|
|
hbvm::VmRunOk::Ecall => self.handle_ecall(),
|
|
|
|
hbvm::VmRunOk::Breakpoint => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn ty_display(&self, ty: ty::Id) -> ty::Display {
|
|
|
|
ty::Display::new(&self.tys, &self.files, ty)
|
|
|
|
}
|
2024-05-11 09:04:13 -05:00
|
|
|
|
2024-10-01 08:28:18 -05:00
|
|
|
fn ast_display<'a>(&'a self, ast: &'a Expr<'a>) -> parser::Display<'a> {
|
|
|
|
parser::Display::new(&self.cfile().file, ast)
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
#[must_use]
|
2024-07-19 05:00:55 -05:00
|
|
|
#[track_caller]
|
2024-10-13 07:11:17 -05:00
|
|
|
fn assert_ty(
|
|
|
|
&self,
|
|
|
|
pos: Pos,
|
|
|
|
ty: ty::Id,
|
|
|
|
expected: ty::Id,
|
|
|
|
kind: TyCheck,
|
|
|
|
hint: impl Display,
|
|
|
|
) -> ty::Id {
|
|
|
|
if let Some(res) = ty.try_upcast(expected, kind) {
|
2024-06-23 02:09:33 -05:00
|
|
|
res
|
|
|
|
} else {
|
2024-09-20 09:37:51 -05:00
|
|
|
let dty = self.ty_display(ty);
|
|
|
|
let dexpected = self.ty_display(expected);
|
2024-10-13 08:22:16 -05:00
|
|
|
log::info!("mode: {:?}", kind);
|
2024-09-20 09:37:51 -05:00
|
|
|
self.report(pos, format_args!("expected {hint} of type {dexpected}, got {dty}",));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_arg_count(&self, pos: Pos, got: usize, expected: usize, hint: impl Display) {
|
|
|
|
if got != expected {
|
|
|
|
let s = if expected != 1 { "s" } else { "" };
|
|
|
|
self.report(pos, format_args!("{hint} expected {expected} argument{s}, got {got}"))
|
2024-06-23 02:09:33 -05:00
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-05-17 12:53:59 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
#[track_caller]
|
2024-09-30 12:09:17 -05:00
|
|
|
fn report(&self, pos: Pos, msg: impl core::fmt::Display) -> ! {
|
2024-10-12 06:07:49 -05:00
|
|
|
log::error!("{}", self.cfile().report(pos, msg));
|
2024-06-23 02:09:33 -05:00
|
|
|
unreachable!();
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-07-19 05:00:55 -05:00
|
|
|
#[track_caller]
|
|
|
|
fn report_unhandled_ast(&self, ast: &Expr, hint: &str) -> ! {
|
2024-10-10 12:01:12 -05:00
|
|
|
log::debug!("{ast:#?}");
|
2024-10-12 06:07:49 -05:00
|
|
|
self.report(ast.pos(), format_args!("compiler does not (yet) know how to handle ({hint})",))
|
2024-07-19 05:00:55 -05:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:09:33 -05:00
|
|
|
fn cfile(&self) -> &parser::Ast {
|
|
|
|
&self.files[self.ci.file as usize]
|
2024-05-10 08:29:11 -05:00
|
|
|
}
|
2024-06-24 10:26:00 -05:00
|
|
|
|
|
|
|
fn pack_args(&mut self, pos: Pos, arg_base: usize) -> ty::Tuple {
|
2024-10-12 15:21:20 -05:00
|
|
|
let base = self.tys.ins.args.len();
|
|
|
|
self.tys.ins.args.extend(self.tys.tmp.args.drain(arg_base..));
|
|
|
|
let needle = &self.tys.ins.args[base..];
|
2024-06-24 10:26:00 -05:00
|
|
|
if needle.is_empty() {
|
|
|
|
return ty::Tuple::empty();
|
|
|
|
}
|
|
|
|
let len = needle.len();
|
|
|
|
// FIXME: maybe later when this becomes a bottleneck we use more
|
|
|
|
// efficient search (SIMD?, indexing?)
|
2024-10-01 15:53:03 -05:00
|
|
|
let sp = self.tys.ins.args.windows(needle.len()).position(|val| val == needle).unwrap();
|
2024-10-12 15:21:20 -05:00
|
|
|
self.tys.ins.args.truncate((sp + needle.len()).max(base));
|
2024-06-24 10:26:00 -05:00
|
|
|
ty::Tuple::new(sp, len)
|
|
|
|
.unwrap_or_else(|| self.report(pos, "amount of arguments not supported"))
|
|
|
|
}
|
2024-09-09 18:15:18 -05:00
|
|
|
|
|
|
|
fn cow_reg(&mut self, rhs: reg::Id) -> reg::Id {
|
|
|
|
if rhs.is_ref() {
|
|
|
|
let reg = self.ci.regs.allocate();
|
2024-09-13 11:22:27 -05:00
|
|
|
self.ci.emit(cp(reg.get(), rhs.get()));
|
2024-09-09 18:15:18 -05:00
|
|
|
reg
|
|
|
|
} else {
|
|
|
|
rhs
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 11:22:27 -05:00
|
|
|
|
|
|
|
pub fn assemble(&mut self, buf: &mut Vec<u8>) {
|
2024-10-12 06:07:49 -05:00
|
|
|
self.tys.reassemble(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assemble_comptime(mut self) -> Comptime {
|
|
|
|
self.ct.code.clear();
|
|
|
|
self.tys.reassemble(&mut self.ct.code);
|
|
|
|
self.ct.reset();
|
|
|
|
self.ct
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
2024-05-19 11:20:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-09-30 12:09:17 -05:00
|
|
|
use alloc::{string::String, vec::Vec};
|
|
|
|
|
2024-06-15 03:37:50 -05:00
|
|
|
fn generate(ident: &'static str, input: &'static str, output: &mut String) {
|
2024-10-01 15:53:03 -05:00
|
|
|
_ = log::set_logger(&crate::fs::Logger);
|
2024-10-13 08:22:16 -05:00
|
|
|
log::set_max_level(log::LevelFilter::Debug);
|
2024-10-01 15:53:03 -05:00
|
|
|
|
2024-10-13 13:01:18 -05:00
|
|
|
let (files, embeds) = crate::test_parse_files(ident, input);
|
|
|
|
let mut codegen = super::Codegen { files, ..Default::default() };
|
|
|
|
codegen.push_embeds(embeds);
|
2024-07-08 04:00:35 -05:00
|
|
|
|
2024-10-13 05:25:12 -05:00
|
|
|
codegen.generate(0);
|
2024-05-10 14:33:42 -05:00
|
|
|
let mut out = Vec::new();
|
2024-09-13 11:22:27 -05:00
|
|
|
codegen.assemble(&mut out);
|
2024-05-10 08:29:11 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let err = codegen.tys.disasm(&out, &codegen.files, output, |_| {});
|
2024-09-04 10:56:59 -05:00
|
|
|
if err.is_err() {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-07 06:42:48 -05:00
|
|
|
|
2024-09-13 07:30:23 -05:00
|
|
|
crate::test_run_vm(&out, output);
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
|
|
|
|
2024-05-09 16:41:59 -05:00
|
|
|
crate::run_tests! { generate:
|
2024-09-28 09:34:08 -05:00
|
|
|
arithmetic;
|
|
|
|
variables;
|
|
|
|
functions;
|
|
|
|
comments;
|
|
|
|
if_statements;
|
|
|
|
loops;
|
|
|
|
//fb_driver;
|
|
|
|
pointers;
|
|
|
|
structs;
|
|
|
|
different_types;
|
|
|
|
struct_operators;
|
|
|
|
directives;
|
|
|
|
global_variables;
|
|
|
|
generic_types;
|
|
|
|
generic_functions;
|
2024-10-12 15:03:53 -05:00
|
|
|
inlined_generic_functions;
|
2024-09-28 09:34:08 -05:00
|
|
|
c_strings;
|
|
|
|
idk;
|
|
|
|
struct_patterns;
|
|
|
|
arrays;
|
|
|
|
struct_return_from_module_function;
|
|
|
|
//comptime_pointers;
|
|
|
|
sort_something_viredly;
|
|
|
|
hex_octal_binary_literals;
|
|
|
|
//comptime_min_reg_leak;
|
|
|
|
// structs_in_registers;
|
|
|
|
comptime_function_from_another_file;
|
|
|
|
inline;
|
|
|
|
inline_test;
|
|
|
|
some_generic_code;
|
|
|
|
integer_inference_issues;
|
|
|
|
writing_into_string;
|
|
|
|
request_page;
|
|
|
|
tests_ptr_to_ptr_copy;
|
|
|
|
wide_ret;
|
2024-05-09 11:16:01 -05:00
|
|
|
}
|
|
|
|
}
|