From bd7b66a8981bcf721ec62202b012b2cf43041252 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Wed, 18 Dec 2013 16:43:15 -0700 Subject: [PATCH] Remove boilerplate code (unaryOp methods in Compiler) --- include/avian/vm/codegen/compiler.h | 6 +---- include/avian/vm/codegen/lir.h | 9 ++++++- src/codegen/compiler.cpp | 39 ++++------------------------- src/compile.cpp | 16 ++++++------ 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/include/avian/vm/codegen/compiler.h b/include/avian/vm/codegen/compiler.h index 271661d19a..fd95a803ab 100644 --- a/include/avian/vm/codegen/compiler.h +++ b/include/avian/vm/codegen/compiler.h @@ -131,12 +131,8 @@ class Compiler { virtual void exit(Operand* address) = 0; virtual Operand* binaryOp(lir::TernaryOperation type, unsigned size, Operand* a, Operand* b) = 0; + virtual Operand* unaryOp(lir::BinaryOperation type, unsigned size, Operand* a) = 0; - virtual Operand* neg(unsigned size, Operand* a) = 0; - virtual Operand* fneg(unsigned size, Operand* a) = 0; - virtual Operand* abs(unsigned size, Operand* a) = 0; - virtual Operand* fabs(unsigned size, Operand* a) = 0; - virtual Operand* fsqrt(unsigned size, Operand* a) = 0; virtual Operand* f2f(unsigned aSize, unsigned resSize, Operand* a) = 0; virtual Operand* f2i(unsigned aSize, unsigned resSize, Operand* a) = 0; virtual Operand* i2f(unsigned aSize, unsigned resSize, Operand* a) = 0; diff --git a/include/avian/vm/codegen/lir.h b/include/avian/vm/codegen/lir.h index 3b6b835c3e..bb493cc387 100644 --- a/include/avian/vm/codegen/lir.h +++ b/include/avian/vm/codegen/lir.h @@ -99,7 +99,6 @@ const unsigned OperandTypeCount = MemoryOperand + 1; const int NoRegister = -1; - inline bool isBranch(lir::TernaryOperation op) { return op > FloatMin; } @@ -120,6 +119,14 @@ inline bool isFloatBinaryOp(lir::TernaryOperation op) { return op >= FloatAdd && op <= FloatMin; } +inline bool isGeneralUnaryOp(lir::BinaryOperation op) { + return op == Negate || op == Absolute; +} + +inline bool isFloatUnaryOp(lir::BinaryOperation op) { + return op == FloatNegate || op == FloatSquareRoot || op == FloatAbsolute; +} + class Operand { }; class Constant: public Operand { diff --git a/src/codegen/compiler.cpp b/src/codegen/compiler.cpp index 2e698f753f..1c612a87d1 100644 --- a/src/codegen/compiler.cpp +++ b/src/codegen/compiler.cpp @@ -2623,40 +2623,11 @@ class MyCompiler: public Compiler { return result; } - virtual Operand* neg(unsigned size, Operand* a) { - assert(&c, static_cast(a)->type == lir::ValueGeneral); - Value* result = value(&c, lir::ValueGeneral); - appendTranslate(&c, lir::Negate, size, static_cast(a), size, result); - return result; - } - - virtual Operand* fneg(unsigned size, Operand* a) { - assert(&c, static_cast(a)->type == lir::ValueFloat); - Value* result = value(&c, lir::ValueFloat); - appendTranslate(&c, lir::FloatNegate, size, static_cast(a), size, result); - return result; - } - - virtual Operand* abs(unsigned size, Operand* a) { - assert(&c, static_cast(a)->type == lir::ValueGeneral); - Value* result = value(&c, lir::ValueGeneral); - appendTranslate(&c, lir::Absolute, size, static_cast(a), size, result); - return result; - } - - virtual Operand* fabs(unsigned size, Operand* a) { - assert(&c, static_cast(a)->type == lir::ValueFloat); - Value* result = value(&c, lir::ValueFloat); - appendTranslate - (&c, lir::FloatAbsolute, size, static_cast(a), size, result); - return result; - } - - virtual Operand* fsqrt(unsigned size, Operand* a) { - assert(&c, static_cast(a)->type == lir::ValueFloat); - Value* result = value(&c, lir::ValueFloat); - appendTranslate - (&c, lir::FloatSquareRoot, size, static_cast(a), size, result); + virtual Operand* unaryOp(lir::BinaryOperation type, unsigned size, Operand* a) { + assert(&c, (isGeneralUnaryOp(type) and isGeneralValue(a))or( + isFloatUnaryOp(type) and isFloatValue(a))); + Value* result = value(&c, static_cast(a)->type); + appendTranslate(&c, type, size, static_cast(a), size, result); return result; } diff --git a/src/compile.cpp b/src/compile.cpp index eea2526db6..f3f7f93d5c 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -3954,17 +3954,17 @@ intrinsic(MyThread* t, Frame* frame, object target) if (MATCH(methodName(t, target), "sqrt") and MATCH(methodSpec(t, target), "(D)D")) { - frame->pushLong(c->fsqrt(8, frame->popLong())); + frame->pushLong(c->unaryOp(lir::FloatSquareRoot, 8, frame->popLong())); return true; } else if (MATCH(methodName(t, target), "abs")) { if (MATCH(methodSpec(t, target), "(I)I")) { - frame->pushInt(c->abs(4, frame->popInt())); + frame->pushInt(c->unaryOp(lir::Absolute, 4, frame->popInt())); return true; } else if (MATCH(methodSpec(t, target), "(J)J")) { - frame->pushLong(c->abs(8, frame->popLong())); + frame->pushLong(c->unaryOp(lir::Absolute, 8, frame->popLong())); return true; } else if (MATCH(methodSpec(t, target), "(F)F")) { - frame->pushInt(c->fabs(4, frame->popInt())); + frame->pushInt(c->unaryOp(lir::FloatAbsolute, 4, frame->popInt())); return true; } } @@ -4649,7 +4649,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp, } break; case dneg: { - frame->pushLong(c->fneg(8, frame->popLong())); + frame->pushLong(c->unaryOp(lir::FloatNegate, 8, frame->popLong())); } break; case vm::drem: { @@ -4766,7 +4766,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp, } break; case fneg: { - frame->pushInt(c->fneg(4, frame->popInt())); + frame->pushInt(c->unaryOp(lir::FloatNegate, 4, frame->popInt())); } break; case vm::frem: { @@ -5238,7 +5238,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp, } break; case ineg: { - frame->pushInt(c->neg(4, frame->popInt())); + frame->pushInt(c->unaryOp(lir::Negate, 4, frame->popInt())); } break; case instanceof: { @@ -5740,7 +5740,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp, } break; case lneg: - frame->pushLong(c->neg(8, frame->popLong())); + frame->pushLong(c->unaryOp(lir::Negate, 8, frame->popLong())); break; case lookupswitch: {