File: WordRotate.i3 Last Modified On Mon Dec 5 15:30:43 PST 1994 By kalsow Modified On Thu May 17 08:59:11 1990 By muller
MODULE; IMPORT CG, CallExpr, Expr, ExprRep, Procedure, Formal, Type, SubrangeType; IMPORT Int, IntegerExpr, Value, ProcType, CheckExpr, Target, TInt, TWord; FROM Int IMPORT T; VAR Z, ZL, ZR: CallExpr.MethodList; VAR formals, formalsL, formalsR: Value.T; PROCEDURE WordRotate Check (ce: CallExpr.T; VAR cs: Expr.CheckState) = BEGIN EVAL Formal.CheckArgs (cs, ce.args, formals, ce.proc); ce.type := T; END Check; PROCEDURECheckL (ce: CallExpr.T; VAR cs: Expr.CheckState) = BEGIN EVAL Formal.CheckArgs (cs, ce.args, formalsL, ce.proc); ce.type := T; END CheckL; PROCEDURECheckR (ce: CallExpr.T; VAR cs: Expr.CheckState) = BEGIN EVAL Formal.CheckArgs (cs, ce.args, formalsR, ce.proc); ce.type := T; END CheckR; PROCEDURECompile (ce: CallExpr.T) = BEGIN Expr.Compile (ce.args[0]); Expr.Compile (ce.args[1]); CG.Rotate (Target.Integer.cg_type); END Compile; PROCEDURECompileL (ce: CallExpr.T) = VAR max: Target.Int; b := TInt.FromInt (Target.Integer.size -1, Target.Integer.bytes, max); BEGIN <* ASSERT b *> Expr.Compile (ce.args[0]); CheckExpr.EmitChecks (ce.args[1], TInt.Zero, max, CG.RuntimeError.ValueOutOfRange); CG.Rotate_left (Target.Integer.cg_type); END CompileL; PROCEDURECompileR (ce: CallExpr.T) = VAR max: Target.Int; b := TInt.FromInt (Target.Integer.size -1, Target.Integer.bytes, max); BEGIN <* ASSERT b *> Expr.Compile (ce.args[0]); CheckExpr.EmitChecks (ce.args[1], TInt.Zero, max, CG.RuntimeError.ValueOutOfRange); CG.Rotate_right (Target.Integer.cg_type); END CompileR; PROCEDUREFold (ce: CallExpr.T): Expr.T = VAR e0, e1: Expr.T; w0, result: Target.Int; i1: INTEGER; t: Type.T; BEGIN e0 := Expr.ConstValue (ce.args[0]); e1 := Expr.ConstValue (ce.args[1]); IF (e0 # NIL) AND IntegerExpr.Split (e0, w0, t) AND (e1 # NIL) AND IntegerExpr.ToInt (e1, i1) THEN TWord.Rotate (w0, i1, result); RETURN IntegerExpr.New (T, result); END; RETURN NIL; END Fold; PROCEDUREFoldL (ce: CallExpr.T): Expr.T = VAR e0, e1: Expr.T; w0, result: Target.Int; i1: INTEGER; t: Type.T; BEGIN e0 := Expr.ConstValue (ce.args[0]); e1 := Expr.ConstValue (ce.args[1]); IF (e0 # NIL) AND IntegerExpr.Split (e0, w0, t) AND (e1 # NIL) AND IntegerExpr.ToInt (e1, i1) AND 0 <= i1 AND i1 < Target.Integer.size THEN TWord.Rotate (w0, i1, result); RETURN IntegerExpr.New (T, result); END; RETURN NIL; END FoldL; PROCEDUREFoldR (ce: CallExpr.T): Expr.T = VAR e0, e1: Expr.T; w0, result: Target.Int; i1: INTEGER; t: Type.T; BEGIN e0 := Expr.ConstValue (ce.args[0]); e1 := Expr.ConstValue (ce.args[1]); IF (e0 # NIL) AND IntegerExpr.Split (e0, w0, t) AND (e1 # NIL) AND IntegerExpr.ToInt (e1, i1) AND 0 <= i1 AND i1 < Target.Integer.size THEN TWord.Rotate (w0, -i1, result); RETURN IntegerExpr.New (T, result); END; RETURN NIL; END FoldR; PROCEDUREInitialize () = VAR max : Target.Int; b := TInt.FromInt (Target.Integer.size-1, Target.Integer.bytes, max); sub := SubrangeType.New (TInt.Zero, max, Int.T, FALSE); f0 := Formal.NewBuiltin ("x", 0, T); f1 := Formal.NewBuiltin ("n", 1, Int.T); t := ProcType.New (T, f0, f1); Lf0 := Formal.NewBuiltin ("x", 0, T); Lf1 := Formal.NewBuiltin ("n", 1, sub); Lt := ProcType.New (T, Lf0, Lf1); Rf0 := Formal.NewBuiltin ("x", 0, T); Rf1 := Formal.NewBuiltin ("n", 1, sub); Rt := ProcType.New (T, Rf0, Rf1); BEGIN <*ASSERT b*> Z := CallExpr.NewMethodList (2, 2, TRUE, TRUE, TRUE, T, NIL, CallExpr.NotAddressable, Check, CallExpr.PrepArgs, Compile, CallExpr.NoLValue, CallExpr.NoLValue, CallExpr.NotBoolean, CallExpr.NotBoolean, Fold, CallExpr.NoBounds, CallExpr.IsNever, (* writable *) CallExpr.IsNever, (* designator *) CallExpr.NotWritable (* noteWriter *)); Procedure.Define ("Rotate", Z, FALSE, t); formals := ProcType.Formals (t); ZL := CallExpr.NewMethodList (2, 2, TRUE, TRUE, TRUE, T, NIL, CallExpr.NotAddressable, CheckL, CallExpr.PrepArgs, CompileL, CallExpr.NoLValue, CallExpr.NoLValue, CallExpr.NotBoolean, CallExpr.NotBoolean, FoldL, CallExpr.NoBounds, CallExpr.IsNever, (* writable *) CallExpr.IsNever, (* designator *) CallExpr.NotWritable (* noteWriter *)); Procedure.Define ("LeftRotate", ZL, FALSE, Lt); formalsL := ProcType.Formals (Lt); ZR := CallExpr.NewMethodList (2, 2, TRUE, TRUE, TRUE, T, NIL, CallExpr.NotAddressable, CheckR, CallExpr.PrepArgs, CompileR, CallExpr.NoLValue, CallExpr.NoLValue, CallExpr.NotBoolean, CallExpr.NotBoolean, FoldR, CallExpr.NoBounds, CallExpr.IsNever, (* writable *) CallExpr.IsNever, (* designator *) CallExpr.NotWritable (* noteWriter *)); Procedure.Define ("RightRotate", ZR, FALSE, Rt); formalsR := ProcType.Formals (Rt); END Initialize; BEGIN END WordRotate.