Remove boilerplate code (general binaryOp methods in Compiler)

This commit is contained in:
Joshua Warner 2013-12-18 15:11:30 -07:00
parent b66a8549fd
commit d5c1a094ca
4 changed files with 60 additions and 141 deletions

View File

@ -129,22 +129,15 @@ class Compiler {
virtual void jmp(Operand* address) = 0; virtual void jmp(Operand* address) = 0;
virtual void exit(Operand* address) = 0; virtual void exit(Operand* address) = 0;
virtual Operand* add(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* sub(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* binaryOp(lir::TernaryOperation type, unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* mul(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* div(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* rem(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* fadd(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* fadd(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* fsub(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* fsub(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* fmul(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* fmul(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* fdiv(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* fdiv(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* frem(unsigned size, Operand* a, Operand* b) = 0; virtual Operand* frem(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* shl(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* shr(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* ushr(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* and_(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* or_(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* xor_(unsigned size, Operand* a, Operand* b) = 0;
virtual Operand* neg(unsigned size, Operand* a) = 0; virtual Operand* neg(unsigned size, Operand* a) = 0;
virtual Operand* fneg(unsigned size, Operand* a) = 0; virtual Operand* fneg(unsigned size, Operand* a) = 0;
virtual Operand* abs(unsigned size, Operand* a) = 0; virtual Operand* abs(unsigned size, Operand* a) = 0;

View File

@ -112,6 +112,14 @@ inline bool isGeneralBranch(lir::TernaryOperation op) {
return isBranch(op) && !isFloatBranch(op); return isBranch(op) && !isFloatBranch(op);
} }
inline bool isGeneralBinaryOp(lir::TernaryOperation op) {
return op < FloatAdd;
}
inline bool isFloatBinaryOp(lir::TernaryOperation op) {
return op > FloatAdd && op <= FloatMin;
}
class Operand { }; class Operand { };
class Constant: public Operand { class Constant: public Operand {

View File

@ -2611,47 +2611,14 @@ class MyCompiler: public Compiler {
appendJump(&c, lir::Jump, static_cast<Value*>(address), true); appendJump(&c, lir::Jump, static_cast<Value*>(address), true);
} }
virtual Operand* add(unsigned size, Operand* a, Operand* b) { virtual Operand* binaryOp(lir::TernaryOperation type, unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral assert(&c,
and static_cast<Value*>(b)->type == lir::ValueGeneral); (isGeneralBinaryOp(type) and isGeneralValue(a) and isGeneralValue(b))
Value* result = value(&c, lir::ValueGeneral); or (isFloatBinaryOp(type) and isFloatValue(a) and isFloatValue(b)));
appendCombine(&c, lir::Add, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* sub(unsigned size, Operand* a, Operand* b) { Value* result = value(&c, static_cast<Value*>(a)->type);
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral
and static_cast<Value*>(b)->type == lir::ValueGeneral); appendCombine(&c, type, size, static_cast<Value*>(a),
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Subtract, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* mul(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral
and static_cast<Value*>(b)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Multiply, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* div(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral
and static_cast<Value*>(b)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Divide, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* rem(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral
and static_cast<Value*>(b)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Remainder, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result); size, static_cast<Value*>(b), size, result);
return result; return result;
} }
@ -2704,55 +2671,6 @@ class MyCompiler: public Compiler {
return result; return result;
} }
virtual Operand* shl(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::ShiftLeft, TargetBytesPerWord, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* shr(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::ShiftRight, TargetBytesPerWord, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* ushr(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine
(&c, lir::UnsignedShiftRight, TargetBytesPerWord, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* and_(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::And, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* or_(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Or, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* xor_(unsigned size, Operand* a, Operand* b) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral);
appendCombine(&c, lir::Xor, size, static_cast<Value*>(a),
size, static_cast<Value*>(b), size, result);
return result;
}
virtual Operand* neg(unsigned size, Operand* a) { virtual Operand* neg(unsigned size, Operand* a) {
assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral); assert(&c, static_cast<Value*>(a)->type == lir::ValueGeneral);
Value* result = value(&c, lir::ValueGeneral); Value* result = value(&c, lir::ValueGeneral);

View File

@ -1408,8 +1408,8 @@ class Frame {
object pointer = makePointer(t, p); object pointer = makePointer(t, p);
bc->constants = makeTriple(t, o, pointer, bc->constants); bc->constants = makeTriple(t, o, pointer, bc->constants);
return c->add return c->binaryOp(lir::Add,
(TargetBytesPerWord, c->memory TargetBytesPerWord, c->memory
(c->register_(t->arch->thread()), Compiler::AddressType, (c->register_(t->arch->thread()), Compiler::AddressType,
TARGET_THREAD_HEAPIMAGE), c->promiseConstant TARGET_THREAD_HEAPIMAGE), c->promiseConstant
(p, Compiler::AddressType)); (p, Compiler::AddressType));
@ -1638,8 +1638,8 @@ class Frame {
Value absoluteAddressOperand(avian::codegen::Promise* p) { Value absoluteAddressOperand(avian::codegen::Promise* p) {
return context->bootContext return context->bootContext
? c->add ? c->binaryOp(lir::Add,
(TargetBytesPerWord, c->memory TargetBytesPerWord, c->memory
(c->register_(t->arch->thread()), Compiler::AddressType, (c->register_(t->arch->thread()), Compiler::AddressType,
TARGET_THREAD_CODEIMAGE), c->promiseConstant TARGET_THREAD_CODEIMAGE), c->promiseConstant
(new(&context->zone) (new(&context->zone)
@ -4380,10 +4380,10 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
0, 0,
Compiler::VoidType, Compiler::VoidType,
4, c->register_(t->arch->thread()), array, 4, c->register_(t->arch->thread()), array,
c->add c->binaryOp(lir::Add,
(4, c->constant(TargetArrayBody, Compiler::IntegerType), 4, c->constant(TargetArrayBody, Compiler::IntegerType),
c->shl c->binaryOp(lir::ShiftLeft,
(4, c->constant(log(TargetBytesPerWord), Compiler::IntegerType), 4, c->constant(log(TargetBytesPerWord), Compiler::IntegerType),
index)), index)),
value); value);
} break; } break;
@ -5018,13 +5018,13 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case iadd: { case iadd: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->add(4, a, b)); frame->pushInt(c->binaryOp(lir::Add, 4, a, b));
} break; } break;
case iand: { case iand: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->and_(4, a, b)); frame->pushInt(c->binaryOp(lir::And, 4, a, b));
} break; } break;
case iconst_m1: case iconst_m1:
@ -5064,7 +5064,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
frame->trace(0, 0); frame->trace(0, 0);
} }
frame->pushInt(c->div(4, a, b)); frame->pushInt(c->binaryOp(lir::Divide, 4, a, b));
} break; } break;
case if_acmpeq: case if_acmpeq:
@ -5200,8 +5200,8 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
storeLocal storeLocal
(context, 1, (context, 1,
c->add c->binaryOp(lir::Add,
(4, c->constant(count, Compiler::IntegerType), 4, c->constant(count, Compiler::IntegerType),
loadLocal(context, 1, index)), loadLocal(context, 1, index)),
index); index);
} break; } break;
@ -5234,7 +5234,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case imul: { case imul: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->mul(4, a, b)); frame->pushInt(c->binaryOp(lir::Multiply, 4, a, b));
} break; } break;
case ineg: { case ineg: {
@ -5422,8 +5422,8 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
Compiler::Operand* result = c->stackCall Compiler::Operand* result = c->stackCall
(c->memory (c->memory
(c->and_ (c->binaryOp(lir::And,
(TargetBytesPerWord, c->constant TargetBytesPerWord, c->constant
(TargetPointerMask, Compiler::IntegerType), (TargetPointerMask, Compiler::IntegerType),
c->memory(instance, Compiler::ObjectType, 0, 0, 1)), c->memory(instance, Compiler::ObjectType, 0, 0, 1)),
Compiler::ObjectType, offset, 0, 1), Compiler::ObjectType, offset, 0, 1),
@ -5470,7 +5470,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case ior: { case ior: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->or_(4, a, b)); frame->pushInt(c->binaryOp(lir::Or, 4, a, b));
} break; } break;
case irem: { case irem: {
@ -5482,7 +5482,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
frame->trace(0, 0); frame->trace(0, 0);
} }
frame->pushInt(c->rem(4, a, b)); frame->pushInt(c->binaryOp(lir::Remainder, 4, a, b));
} break; } break;
case ireturn: case ireturn:
@ -5494,13 +5494,13 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case ishl: { case ishl: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->shl(4, a, b)); frame->pushInt(c->binaryOp(lir::ShiftLeft, 4, a, b));
} break; } break;
case ishr: { case ishr: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->shr(4, a, b)); frame->pushInt(c->binaryOp(lir::ShiftRight, 4, a, b));
} break; } break;
case istore: case istore:
@ -5531,19 +5531,19 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case isub: { case isub: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->sub(4, a, b)); frame->pushInt(c->binaryOp(lir::Subtract, 4, a, b));
} break; } break;
case iushr: { case iushr: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->ushr(4, a, b)); frame->pushInt(c->binaryOp(lir::UnsignedShiftRight, 4, a, b));
} break; } break;
case ixor: { case ixor: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popInt(); Compiler::Operand* b = frame->popInt();
frame->pushInt(c->xor_(4, a, b)); frame->pushInt(c->binaryOp(lir::Xor, 4, a, b));
} break; } break;
case jsr: case jsr:
@ -5587,13 +5587,13 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case ladd: { case ladd: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->add(8, a, b)); frame->pushLong(c->binaryOp(lir::Add, 8, a, b));
} break; } break;
case land: { case land: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->and_(8, a, b)); frame->pushLong(c->binaryOp(lir::And, 8, a, b));
} break; } break;
case lcmp: { case lcmp: {
@ -5705,7 +5705,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
frame->trace(0, 0); frame->trace(0, 0);
} }
frame->pushLong(c->div(8, a, b)); frame->pushLong(c->binaryOp(lir::Divide, 8, a, b));
} break; } break;
case lload: case lload:
@ -5736,7 +5736,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case lmul: { case lmul: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->mul(8, a, b)); frame->pushLong(c->binaryOp(lir::Multiply, 8, a, b));
} break; } break;
case lneg: case lneg:
@ -5786,8 +5786,8 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
c->constant(pairCount, Compiler::IntegerType), default_); c->constant(pairCount, Compiler::IntegerType), default_);
c->jmp c->jmp
(context->bootContext ? c->add (context->bootContext ? c->binaryOp(lir::Add,
(TargetBytesPerWord, c->memory TargetBytesPerWord, c->memory
(c->register_(t->arch->thread()), Compiler::AddressType, (c->register_(t->arch->thread()), Compiler::AddressType,
TARGET_THREAD_CODEIMAGE), address) TARGET_THREAD_CODEIMAGE), address)
: address); : address);
@ -5806,7 +5806,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case lor: { case lor: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->or_(8, a, b)); frame->pushLong(c->binaryOp(lir::Or, 8, a, b));
} break; } break;
case lrem: { case lrem: {
@ -5818,7 +5818,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
frame->trace(0, 0); frame->trace(0, 0);
} }
frame->pushLong(c->rem(8, a, b)); frame->pushLong(c->binaryOp(lir::Remainder, 8, a, b));
} break; } break;
case lreturn: case lreturn:
@ -5830,13 +5830,13 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case lshl: { case lshl: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->shl(8, a, b)); frame->pushLong(c->binaryOp(lir::ShiftLeft, 8, a, b));
} break; } break;
case lshr: { case lshr: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->shr(8, a, b)); frame->pushLong(c->binaryOp(lir::ShiftRight, 8, a, b));
} break; } break;
case lstore: case lstore:
@ -5867,19 +5867,19 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
case lsub: { case lsub: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->sub(8, a, b)); frame->pushLong(c->binaryOp(lir::Subtract, 8, a, b));
} break; } break;
case lushr: { case lushr: {
Compiler::Operand* a = frame->popInt(); Compiler::Operand* a = frame->popInt();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->ushr(8, a, b)); frame->pushLong(c->binaryOp(lir::UnsignedShiftRight, 8, a, b));
} break; } break;
case lxor: { case lxor: {
Compiler::Operand* a = frame->popLong(); Compiler::Operand* a = frame->popLong();
Compiler::Operand* b = frame->popLong(); Compiler::Operand* b = frame->popLong();
frame->pushLong(c->xor_(8, a, b)); frame->pushLong(c->binaryOp(lir::Xor, 8, a, b));
} break; } break;
case monitorenter: { case monitorenter: {
@ -6332,8 +6332,8 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
storeLocal storeLocal
(context, 1, (context, 1,
c->add c->binaryOp(lir::Add,
(4, c->constant(count, Compiler::IntegerType), 4, c->constant(count, Compiler::IntegerType),
loadLocal(context, 1, index)), loadLocal(context, 1, index)),
index); index);
} break; } break;
@ -6409,7 +6409,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
Compiler::Operand* normalizedKey Compiler::Operand* normalizedKey
= (s->bottom = (s->bottom
? c->sub(4, c->constant(s->bottom, Compiler::IntegerType), s->key) ? c->binaryOp(lir::Subtract, 4, c->constant(s->bottom, Compiler::IntegerType), s->key)
: s->key); : s->key);
Compiler::Operand* entry = c->memory Compiler::Operand* entry = c->memory
@ -6419,8 +6419,8 @@ compile(MyThread* t, Frame* initialFrame, unsigned initialIp,
c->jmp c->jmp
(c->load (c->load
(TargetBytesPerWord, TargetBytesPerWord, context->bootContext (TargetBytesPerWord, TargetBytesPerWord, context->bootContext
? c->add ? c->binaryOp(lir::Add,
(TargetBytesPerWord, c->memory TargetBytesPerWord, c->memory
(c->register_(t->arch->thread()), Compiler::AddressType, (c->register_(t->arch->thread()), Compiler::AddressType,
TARGET_THREAD_CODEIMAGE), entry) TARGET_THREAD_CODEIMAGE), entry)
: entry, : entry,