diff --git a/lang/README.md b/lang/README.md index 8ad136f..93b3694 100644 --- a/lang/README.md +++ b/lang/README.md @@ -58,27 +58,6 @@ main := fn(): f32 { } ``` -#### advanced_floating_point_arithmetic -```hb - -SIN_TABLE := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) - -sin := fn(theta: f32): f32 { - PI := 3.14159265358979323846 - TABLE_SIZE := @as(i32, 256) - si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) - d := theta - @floatcast(@itf(si)) * 2.0 * PI / @itf(TABLE_SIZE) - ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1 - si &= TABLE_SIZE - 1 - return SIN_TABLE[@bitcast(si)] + (SIN_TABLE[@bitcast(ci)] - 0.5 * SIN_TABLE[@bitcast(si)] * d) * d -} - -main := fn(): int { - // expected result: 826 - return @fti(sin(1000.0) * 1000.0) -} -``` - #### functions ```hb main := fn(): uint { @@ -343,6 +322,15 @@ main := fn(): uint { ``` note: values of global variables are evaluated at compile time +#### constants +```hb +$some_const := 34 + +main := fn(): uint { + return some_const + 35 +} +``` + #### directives ```hb foo := @use("foo.hb") @@ -505,6 +493,28 @@ main := fn(): never { ### Incomplete Examples +#### advanced_floating_point_arithmetic +```hb + +SIN_TABLE := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) + +sin := fn(theta: f32): f32 { + PI := 3.14159265358979323846 + TABLE_SIZE := @as(i32, 256) + si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) + d := theta - @floatcast(@itf(si)) * 2.0 * PI / @itf(TABLE_SIZE) + ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1 + si &= TABLE_SIZE - 1 + return SIN_TABLE[@bitcast(si)] + (SIN_TABLE[@bitcast(ci)] - 0.5 * SIN_TABLE[@bitcast(si)] * d) * d +} + +main := fn(): int { + // expected result: 826 + return @fti(sin(1000.0) * 1000.0) +} +``` + + #### comptime_pointers ```hb main := fn(): uint { diff --git a/lang/src/son.rs b/lang/src/son.rs index 393bdce..18a1af0 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2500,12 +2500,8 @@ impl<'a> Codegen<'a> { let decl = self.find_type(pos, self.ci.file, self.ci.file, Ok(id), self.files); match decl.expand() { ty::Kind::NEVER => Value::NEVER, - ty::Kind::Global(global) => { - let gl = &self.tys.ins.globals[global]; - let value = self.ci.nodes.new_node(gl.ty, Kind::Global { global }, [VOID]); - self.ci.nodes[value].aclass = GLOBAL_ACLASS as _; - Some(Value::ptr(value).ty(gl.ty)) - } + ty::Kind::Global(global) => self.gen_global(global), + ty::Kind::Const(cnst) => self.gen_const(cnst), _ => Some(Value::new(Nid::MAX).ty(decl)), } } @@ -2626,13 +2622,8 @@ impl<'a> Codegen<'a> { .expand() { ty::Kind::NEVER => Value::NEVER, - ty::Kind::Global(global) => { - let gl = &self.tys.ins.globals[global]; - let value = - self.ci.nodes.new_node(gl.ty, Kind::Global { global }, [VOID]); - self.ci.nodes[value].aclass = GLOBAL_ACLASS as _; - Some(Value::ptr(value).ty(gl.ty)) - } + ty::Kind::Global(global) => self.gen_global(global), + ty::Kind::Const(cnst) => self.gen_const(cnst), v => Some(Value::new(Nid::MAX).ty(v.compress())), }; } @@ -3771,6 +3762,25 @@ impl<'a> Codegen<'a> { } } + fn gen_global(&mut self, global: ty::Global) -> Option { + let gl = &self.tys.ins.globals[global]; + let value = self.ci.nodes.new_node(gl.ty, Kind::Global { global }, [VOID]); + self.ci.nodes[value].aclass = GLOBAL_ACLASS as _; + Some(Value::ptr(value).ty(gl.ty)) + } + + fn gen_const(&mut self, cnst: ty::Const) -> Option { + let c = &self.tys.ins.consts[cnst]; + let f = &self.files[c.file.index()]; + let Expr::BinOp { left, right, .. } = c.ast.get(f) else { unreachable!() }; + + left.find_pattern_path(c.name, right, |expr, is_ct| { + debug_assert!(is_ct); + self.expr(expr) + }) + .unwrap_or_else(|_| unreachable!()) + } + fn add_clobbers(&mut self, value: Value, clobbered_aliases: &mut BitSet) { if let Some(base) = self.tys.base_of(value.ty) { clobbered_aliases.set(self.ci.nodes.aclass_index(value.id).0 as _); @@ -4496,7 +4506,6 @@ impl TypeParser for Codegen<'_> { let gid = self.tys.ins.globals.push(Global { file, name, ..Default::default() }); - let ty = ty::Kind::Global(gid); self.pool.push_ci(file, None, self.tys.tasks.len(), &mut self.ci); let prev_err_len = self.errors.borrow().len(); @@ -4513,7 +4522,7 @@ impl TypeParser for Codegen<'_> { self.tys.ins.globals[gid].ty = ret; self.ct.deactivate(); - ty.compress() + gid.into() } fn report(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id { @@ -4572,7 +4581,6 @@ mod tests { // Tour Examples main_fn; arithmetic; - advanced_floating_point_arithmetic; floating_point_arithmetic; functions; comments; @@ -4585,6 +4593,7 @@ mod tests { hex_octal_binary_literals; struct_operators; global_variables; + constants; directives; c_strings; struct_patterns; @@ -4600,6 +4609,7 @@ mod tests { fb_driver; // Purely Testing Examples; + advanced_floating_point_arithmetic; nullable_structure; needless_unwrap; inlining_issues; diff --git a/lang/tests/son_tests_constants.txt b/lang/tests/son_tests_constants.txt new file mode 100644 index 0000000..924b2e6 --- /dev/null +++ b/lang/tests/son_tests_constants.txt @@ -0,0 +1,6 @@ +main: + LI64 r1, 69d + JALA r0, r31, 0a +code size: 29 +ret: 69 +status: Ok(())