diff --git a/mips/src/ast/stmt.rs b/mips/src/ast/stmt.rs index c3d16cd..c6dedc0 100644 --- a/mips/src/ast/stmt.rs +++ b/mips/src/ast/stmt.rs @@ -285,7 +285,7 @@ def_stmt!( (Ceil, "ceil", 2, "reg,num", [R, N]), (Cos, "cos", 2, "reg,num", [R, N]), (Div, "div", 3, "reg,num,num", [R, N, N]), - (Exp, "expr", 2, "reg,num", [R, N]), + (Exp, "exp", 2, "reg,num", [R, N]), (Floor, "floor", 2, "reg,num", [R, N]), (Log, "log", 2, "reg,num", [R, N]), (Max, "max", 3, "reg,num,num", [R, N, N]), diff --git a/mips/src/lib.rs b/mips/src/lib.rs index de468d7..6e182b4 100644 --- a/mips/src/lib.rs +++ b/mips/src/lib.rs @@ -423,8 +423,11 @@ impl Mips { } } - // Define/alias/tag replacement pass + // Define/alias/tag replacement and comment removal pass for (_i, line) in mips.lines.iter_mut().enumerate() { + if conf.remove_comments { + line.comment_opt = None; + } match line { // Replace defines Line { @@ -465,56 +468,52 @@ impl Mips { } } // Argument and comment replacement pass - if conf.remove_comments { - for line in mips.lines.iter_mut() { - line.comment_opt = None; - - for arg in line.stmt.iter_args_mut() { - match arg { - Arg::LineAbs(LineAbs(num)) if conf.remove_tags => { - if let Some(key) = num.as_alias() { - let i = tag_lines.get(key).expect(key); - *num = Num::Lit(*i as f64); - } + for line in mips.lines.iter_mut() { + for arg in line.stmt.iter_args_mut() { + match arg { + Arg::LineAbs(LineAbs(num)) if conf.remove_tags => { + if let Some(key) = num.as_alias() { + let i = tag_lines.get(key).expect(key); + *num = Num::Lit(*i as f64); } - Arg::Num(num) => { - if let Some(key) = num.as_alias() { - if let Some(alias) = mips.aliases.get(key) { - match alias { - Alias::Num(n) => { - if conf.remove_defines { - *arg = Arg::Num(Num::Lit(*n)); - } - } - Alias::Reg(reg_base) => { - if conf.remove_reg_aliases { - *arg = Arg::Reg(Reg::Base(*reg_base)); - } + } + Arg::Num(num) => { + if let Some(key) = num.as_alias() { + if let Some(alias) = mips.aliases.get(key) { + match alias { + Alias::Num(n) => { + if conf.remove_defines { + *arg = Arg::Num(Num::Lit(*n)); } - Alias::Dev(_) => { - panic!(); + } + Alias::Reg(reg_base) => { + if conf.remove_reg_aliases { + *arg = Arg::Reg(Reg::Base(*reg_base)); } } + Alias::Dev(_) => { + panic!(); + } } - // *num = Num::Lit() } + // *num = Num::Lit() } - Arg::Reg(Reg::Alias { key, .. }) => { - if conf.remove_reg_aliases { - let reg_base = mips.aliases.try_get_reg_base(key).unwrap(); - *arg = Arg::Reg(Reg::Base(reg_base)); - } + } + Arg::Reg(Reg::Alias { key, .. }) => { + if conf.remove_reg_aliases { + let reg_base = mips.aliases.try_get_reg_base(key).unwrap(); + *arg = Arg::Reg(Reg::Base(reg_base)); } - Arg::Dev(Dev::Alias(key)) => { - if conf.remove_dev_aliases { - let dev_base = mips.aliases.try_get_dev_base(key).unwrap(); - *arg = Arg::Dev(Dev::Base(dev_base)); - } + } + Arg::Dev(Dev::Alias(key)) => { + if conf.remove_dev_aliases { + let dev_base = mips.aliases.try_get_dev_base(key).unwrap(); + *arg = Arg::Dev(Dev::Base(dev_base)); } - // Arg::String(key) => { - // } - _ => {} } + // Arg::String(key) => { + // } + _ => {} } } } diff --git a/myps/src/ast/func.rs b/myps/src/ast/func.rs index ee496c4..73542a8 100644 --- a/myps/src/ast/func.rs +++ b/myps/src/ast/func.rs @@ -129,6 +129,7 @@ def_func!( (Abs, 1, "abs", "expr", [E ]), (Acos, 1, "acos", "nexpr", [E ]), (Asin, 1, "asin", "expr", [E ]), + (Atan, 1, "atan", "expr", [E ]), (Ceil, 1, "ceil", "expr", [E ]), (Cos, 1, "cos", "expr", [E ]), (Exp, 1, "exp", "expr", [E ]), diff --git a/myps/test-scripts/elmo/furnace.mips b/myps/test-scripts/elmo/furnace.mips new file mode 100644 index 0000000..14de77a --- /dev/null +++ b/myps/test-scripts/elmo/furnace.mips @@ -0,0 +1,135 @@ +alias WastePump d0 +alias CoolingPump d1 +alias HeatPump d2 +alias WasteAnalyzer d3 +alias SPTemp d4 +alias SPPressure d5 + +alias pk r0 +alias tk r1 +alias tw r2 +alias pk1 r3 +alias tk1 r4 +alias vs r5 +alias vo r6 +alias vc r7 +alias vh r8 +alias vw r9 +alias tct r10 +alias tc r11 +alias pp r12 +alias th r13 + +move r13 2500 +move r11 150 + +Start: + yield + l r10 db Setting + lb r0 545937711 Pressure 0 + lb r1 545937711 Temperature 0 + l r3 d5 Setting + l r4 d4 Setting + l r2 d3 Temperature + l r12 d3 Pressure + sgt r14 r10 r1 + select r13 r14 r10 r13 + slt r14 r10 r1 + select r11 r14 r10 r11 + move r10 r13 + jal CalcTemp + + move r8 r10 + move r10 r11 + jal CalcTemp + + move r7 r10 + mul r14 0.9 500 + sub r15 r3 r0 + mul r14 r14 r15 + div r14 r14 r12 + sub r14 r14 r8 + sub r14 r14 r7 + max r5 0 r14 + mul r7 0.9 r7 + mul r8 0.9 r8 + move r10 r13 + jal CalcVE + + bltz r6 c + bgt r6 100 Scale + bgt r9 100 Scale + add r8 r8 r6 + j Continue + +c: + move r10 r11 + jal CalcVE + bgt r6 100 Scale + bgt r9 100 Scale + add r7 r7 r6 + +Continue: + min r7 r7 100 + min r8 r8 100 + min r9 r9 100 + add r14 r7 r8 + add r5 r14 r9 +# vs = vs / OutputFactor # VS factor + sub r14 r0 r3 + mul r14 500 r14 + mul r15 r12 r5 + add r14 r14 r15 + mul r6 500 r14 + mul r5 r5 r12 + mul r14 0.9 r6 + mul r15 500 r0 + add r15 r15 r5 + div r14 r14 r15 + min r14 100 r14 + max r6 0 r14 + + lb r14 545937711 On 0 + s d0 On r14 + s d2 On r14 + s d1 On r14 + + s d0 Setting r9 + s d2 Setting r8 + s d1 Setting r7 + + sb 545937711 SettingOutput r6 + j Start + +Scale: + max r14 r6 r9 + div r14 100 r14 + mul r5 r5 r14 + +CalcVE: + mul r14 r10 r5 + sub r15 r1 r2 + mul r6 r14 r15 + sub r14 r10 r2 + mul r14 r1 r14 + div r6 r6 r14 + mul r14 r2 r5 + sub r15 r10 r1 + mul r9 r14 r15 + sub r14 r10 r2 + mul r14 r1 r14 + div r9 r9 r14 + j ra + +CalcTemp: + mul r14 500 r10 + mul r14 r14 r0 + sub r15 r1 r4 + mul r14 r14 r15 + mul r15 r12 r1 + sub r16 r4 r10 + mul r15 r15 r16 + div r14 r14 r15 + max r10 0 r14 + j ra + diff --git a/myps/test-scripts/elmo/furnace.myps b/myps/test-scripts/elmo/furnace.myps new file mode 100644 index 0000000..81e3d21 --- /dev/null +++ b/myps/test-scripts/elmo/furnace.myps @@ -0,0 +1,107 @@ +WastePump = d0 +CoolingPump = d1 +HeatPump = d2 +WasteAnalyzer = d3 +SPTemp = d4 +SPPressure = d5 + +def InputFactor = 0.9 +def OutputFactor = 0.9 +def VSMax = 100 +def FurnaceHash = 545937711 + +# alias pk r1 +# alias tk r2 +# alias tw r3 +# alias pk1 r4 +# alias tk1 r5 +# alias vs r6 +# alias vo r7 +# alias vc r8 +# alias vh r9 +# alias vw r10 +# alias vo r11 +# alias tct r12 +# alias tc r13 +# alias pp r14 +# alias th r15 + +fix pk, tk, tw, pk1, tk1, vs, vo, vc, vh, vw, tct, tc, pp, th + +th = 2500 +tc = 150 + +tag Start: + yield() + tct = db.Setting + pk = FurnaceHash.all.Pressure.0 + tk = FurnaceHash.all.Temperature.0 + pk1 = SPPressure.Setting + tk1 = SPTemp.Setting + tw = WasteAnalyzer.Temperature + pp = WasteAnalyzer.Pressure + th = (tct > tk) ? tct : th + tc = (tct < tk) ? tct : tc + tct = th + jal(CalcTemp) + + vh = tct + tct = tc + jal(CalcTemp) + + vc = tct + vs = max(0, InputFactor * 500 * (pk1 - pk) / pp - vh - vc) + vc = InputFactor * vc + vh = InputFactor * vh + tct = th + jal(CalcVE) + + bltz(vo, c) + bgt(vo, VSMax, Scale) + bgt(vw, VSMax, Scale) + vh += vo + j(Continue) + + tag c: + tct = tc + jal(CalcVE) + bgt(vo, VSMax, Scale) + bgt(vw, VSMax, Scale) + vc += vo + + tag Continue: + vc = min(vc, 100) + vh = min(vh, 100) + vw = min(vw, 100) + vs = vc + vh + vw + # vs = vs / OutputFactor # VS factor + vo = 500 * (500 * (pk - pk1) + pp * vs) + vs *= pp + vo = max(0, min(100, OutputFactor * vo / (500 * pk + vs))) + + furnaceOn = FurnaceHash.all.On.0 + WastePump .On = furnaceOn + HeatPump .On = furnaceOn + CoolingPump .On = furnaceOn + + WastePump .Setting = vw + HeatPump .Setting = vh + CoolingPump .Setting = vc + + FurnaceHash.all.SettingOutput = vo + j(Start) + + tag Scale: + vs *= (VSMax / max(vo, vw)) + + tag CalcVE: + vo = tct * vs * (tk - tw) + vo = vo / (tk * (tct - tw)) + vw = tw * vs * (tct - tk) + vw = vw / (tk * (tct- tw)) + j(ra) + + tag CalcTemp: + tct = max(0, (500 * tct * pk * (tk - tk1)) / (pp * tk * (tk1 - tct))) + j(ra) + diff --git a/myps/test-scripts/elmo/furnace.original.mips b/myps/test-scripts/elmo/furnace.original.mips new file mode 100644 index 0000000..2b9e19b --- /dev/null +++ b/myps/test-scripts/elmo/furnace.original.mips @@ -0,0 +1,134 @@ +alias WastePump d0 +alias CoolingPump d1 +alias HeatPump d2 +alias Wasteanalyzer d3 +alias SPTemp d4 +alias SPPressure d5 +alias pk r1 +alias tk r2 +alias tw r3 +alias pk1 r4 +alias tk1 r5 +alias vs r6 +alias vo r7 +alias vc r8 +alias vh r9 +alias vw r10 +alias vo r11 +alias tct r12 +alias tc r13 +alias pp r14 +alias th r15 +define InputFactor 0.9 +define OuputFactor 0.9 +define vsMax 100 +define FurnaceHash 545937711 +move th 2500 +move tc 150 + +Start: + yield + l tct db Setting + lb pk FurnaceHash Pressure 0 + lb tk FurnaceHash Temperature 0 + l pk1 SPPressure Setting + l tk1 SPTemp Setting + l tw Wasteanalyzer Temperature + l pp Wasteanalyzer Pressure + sgt r0 tct tk + select th r0 tct th + slt r0 tct tk + select tc r0 tct tc + move tct th + jal CalcTemp + + move vh tct + move tct tc + jal CalcTemp + + move vc tct + sub vs pk1 pk + mul vs vs 500 + div vs vs pp + mul vs vs InputFactor + sub vs vs vh + sub vs vs vc + max vs vs 0 + mul vc vc InputFactor + mul vh vh InputFactor + move tct th + + jal CalcVE + bltz vo c + bgt vo vsMax Scale + bgt vw vsMax Scale + add vh vh vo + j Continue + +c: + move tct tc + jal CalcVE + bgt vo vsMax Scale + bgt vw vsMax Scale + add vc vc vo + +Continue: + min vc vc 100 + min vh vh 100 + min vw vw 100 + add vs vc vh + add vs vs vw +#div vs vs OuputFactor ### VS factor + sub vo pk pk1 ### VO start + mul vo vo 500 + mul r0 pp vs + add vo vo r0 + mul vo vo 500 + mul vs vs pp + mul r0 pk 500 + add r0 vs r0 + div vo vo r0 + mul vo vo OuputFactor + min vo vo 100 + max vo vo 0### vo end + lb r0 FurnaceHash On 0 + s WastePump On r0 + s HeatPump On r0 + s CoolingPump On r0 + s WastePump Setting vw + s HeatPump Setting vh + s CoolingPump Setting vc + sb FurnaceHash SettingOutput vo + j Start + +Scale: + max r0 vo vw + div r0 vsMax r0 + mul vs r0 vs + +CalcVE: + sub vo tk tw + mul vo vo vs + mul vo vo tct + sub r0 tct tw + mul r0 r0 tk + div vo vo r0 + sub vw tct tk #### r0 VW start + mul vw vw vs + mul vw vw tw + sub r0 tct tw + mul r0 r0 tk + div vw vw r0 + j ra + +CalcTemp: + sub r0 tk tk1 + mul r0 r0 pk + mul r0 r0 tct + mul r0 r0 500 + sub tct tk1 tct + mul tct tct tk + mul tct tct pp + div tct r0 tct + max tct tct 0 + j ra diff --git a/myps/test-scripts/factory/furnace/selector.myps b/myps/test-scripts/factory/furnace/selector.myps new file mode 100644 index 0000000..e69de29 diff --git a/myps/test-scripts/solar.myps b/myps/test-scripts/solar.myps index 0d16f3a..f556b1c 100644 --- a/myps/test-scripts/solar.myps +++ b/myps/test-scripts/solar.myps @@ -1,4 +1,6 @@ sensor = d0 +# provides a multiple of 90 offset to the horizontal +# just to make setting up easier offsetDial = d1 def Normal = -2045627372 diff --git a/myps/test-scripts/test.myps b/myps/test-scripts/test.myps index f6409bd..985bb48 100644 --- a/myps/test-scripts/test.myps +++ b/myps/test-scripts/test.myps @@ -1,2 +1,9 @@ -if 1 == 1: +a = 0 +v = d0.Setting +if v == 0: a = 1 +elif v == 1: + a = 2 +else: + a = 3 + diff --git a/translator/src/lib.rs b/translator/src/lib.rs index dd9eb04..496a0d6 100644 --- a/translator/src/lib.rs +++ b/translator/src/lib.rs @@ -437,7 +437,7 @@ impl Translator { let a = Arg::Num(Num::Reg(i_reg_base)); let b = Arg::Num(e_num); let c = Arg::LineRel(0.into()); - vec![Stmt::Brlt([a, b, c])] + vec![Stmt::Brge([a, b, c])] }; // Translate body items to lines let mut body_lines = self.translate_items(items).unwrap(); @@ -954,6 +954,7 @@ impl Translator { Func::Abs ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Abs), Func::Acos ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Acos), Func::Asin ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Asin), + Func::Atan ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Atan), Func::Ceil ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Ceil), Func::Cos ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Cos), Func::Exp ([a ]) => translate_fun!(self, reg_base_opt, [a ], Stmt::Exp), diff --git a/translator/src/main.rs b/translator/src/main.rs index a2c55b5..4cded2d 100644 --- a/translator/src/main.rs +++ b/translator/src/main.rs @@ -43,8 +43,8 @@ fn main() { optimize_registers: true, // optimize_registers: false, - // remove_comments: true, - remove_comments: false, + remove_comments: true, + // remove_comments: false, remove_empty: true, // remove_empty: false, @@ -55,8 +55,8 @@ fn main() { remove_reg_aliases: true, // remove_reg_aliases: false, - // remove_dev_aliases: true, - remove_dev_aliases: false, + remove_dev_aliases: true, + // remove_dev_aliases: false, remove_defines: true, // remove_defines: false,