Permalink
Browse files

Style improvements for VM code

In general we prefer using C++ style casts over C ones. Also we don't
need to cast if we already have the correct type. In a few cases we
should use our proper as<> template for casting.

In the tooling interface we also have methods for casting, so we should
use them and not do it manually.

Another thing is to make sure we initialize all properties in
constructors.

Lastly we use variable array allocation where we can instead of using
alloca() manually. This creates much simpler and easier to understand
code.
  • Loading branch information...
1 parent dbe4529 commit a9359eca7e9dbf9e936c05ce7b9f1e7d446f8c0c @dbussink dbussink committed Nov 19, 2012
View
@@ -49,7 +49,7 @@ namespace rubinius {
// 'self' is passed in automatically by the primitive glue
Array* Array::allocate(STATE, Object* self) {
Array* ary = Array::create(state, 0);
- ary->klass(state, (Class*)self);
+ ary->klass(state, as<Class>(self));
return ary;
}
@@ -107,7 +107,7 @@ namespace rubinius {
}
LookupTable* CompactLookupTable::to_lookuptable(STATE) {
- LookupTable* tbl = (LookupTable*)LookupTable::create(state);
+ LookupTable* tbl = LookupTable::create(state);
for(unsigned int i = 0; i < COMPACTLOOKUPTABLE_SIZE; i += 2) {
Object* key = at(state, i);
@@ -408,12 +408,12 @@ namespace rubinius {
for(size_t i = 0; i < mcode->inline_cache_count(); i++) {
InlineCache* cache = &mcode->caches[i];
- for(int i = 0; i < cTrackedICHits; ++i) {
- MethodCacheEntry* mce = cache->cache_[i].entry();
+ for(int j = 0; j < cTrackedICHits; ++j) {
+ MethodCacheEntry* mce = cache->cache_[j].entry();
if(mce) {
tmp = mark.call(mce);
if(tmp) {
- cache->cache_[i].assign((MethodCacheEntry*)tmp);
+ cache->cache_[j].assign(reinterpret_cast<MethodCacheEntry*>(tmp));
mark.just_set(obj, tmp);
}
}
@@ -422,7 +422,7 @@ namespace rubinius {
if(cache->call_unit_) {
tmp = mark.call(cache->call_unit_);
if(tmp) {
- cache->call_unit_ = (CallUnit*)tmp;
+ cache->call_unit_ = reinterpret_cast<CallUnit*>(tmp);
mark.just_set(obj, tmp);
}
}
@@ -92,7 +92,7 @@ namespace rubinius {
}
void Executable::Info::mark_inliners(Object* obj, ObjectMark& mark) {
- Executable* exc = (Executable*)obj;
+ Executable* exc = reinterpret_cast<Executable*>(obj);
if(!exc->inliners_ || exc->inliners_ == (Inliners*)cNil) return;
Inliners* inl = exc->inliners_;
@@ -108,7 +108,7 @@ namespace rubinius {
Object* tmp = mark.call(code);
if(tmp) {
assert(kind_of<CompiledCode>(tmp));
- *i = (CompiledCode*)tmp;
+ *i = reinterpret_cast<CompiledCode*>(tmp);
mark.just_set(obj, tmp);
}
}
@@ -389,7 +389,7 @@ namespace rubinius {
ptr += offset;
-#define WRITE(type, val) *((type*)ptr) = (type)val
+#define WRITE(type, val) *(reinterpret_cast<type*>(ptr)) = (type)val
switch(type) {
case RBX_FFI_TYPE_CHAR:
View
@@ -45,7 +45,7 @@ namespace rubinius {
if(r > FIXNUM_MAX || r < FIXNUM_MIN) {
return Bignum::from(state, r);
} else {
- return Fixnum::from(r);
+ return from(r);
}
}
@@ -61,7 +61,7 @@ namespace rubinius {
if(r > FIXNUM_MAX || r < FIXNUM_MIN) {
return Bignum::from(state, r);
} else {
- return Fixnum::from(r);
+ return from(r);
}
}
@@ -236,7 +236,7 @@ namespace rubinius {
key = (key + (key << 2)) + (key << 4); // key * 21
key = key ^ (key >> 28);
key = key + (key << 31);
- return Fixnum::from(key & FIXNUM_MAX);
+ return from(key & FIXNUM_MAX);
#else
// See http://burtleburtle.net/bob/hash/integer.html
uint32_t a = (uint32_t)this;
@@ -246,7 +246,7 @@ namespace rubinius {
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
- return Fixnum::from(a & FIXNUM_MAX);
+ return from(a & FIXNUM_MAX);
#endif
}
View
@@ -379,7 +379,7 @@ namespace rubinius {
if(Bignum* big = try_as<Bignum>(amount)) {
if((size_t)mp_count_bits(big->mp_val()) > (sizeof(off_t) * 8)) {
- return (Integer*)Primitives::failure();
+ return reinterpret_cast<Integer*>(Primitives::failure());
}
}
View
@@ -42,7 +42,7 @@ namespace rubinius {
Module* Module::allocate(STATE, Object* self) {
Module* module = Module::create(state);
- module->klass(state, (Class*)self);
+ module->klass(state, as<Class>(self));
return module;
}
@@ -413,7 +413,7 @@ namespace rubinius {
IncludedModule* IncludedModule::allocate(STATE, Object* self) {
IncludedModule* imod = IncludedModule::create(state);
- imod->klass(state, (Class*)self);
+ imod->klass(state, as<Class>(self));
return imod;
}
@@ -306,7 +306,7 @@ namespace rubinius {
NativeFunction* NativeFunction::generate(STATE, Pointer* ptr, Symbol* name,
Array* args, Object* ret)
{
- int i, tot, arg_count;
+ int tot, arg_count;
Object* type;
FFIArgInfo ret_info;
FFIArgInfo* args_info;
@@ -319,7 +319,7 @@ namespace rubinius {
if(tot > 0) {
args_info = ALLOC_N(FFIArgInfo, tot);
- for(i = 0; i < tot; i++) {
+ for(int i = 0; i < tot; i++) {
type = args->get(state, i);
if(type->fixnum_p()) {
args_info[i].type = as<Integer>(type)->to_native();
@@ -612,7 +612,7 @@ namespace rubinius {
Array* NativeFunction::generate_tramp(STATE, Object* obj, Symbol* name,
Array* args, Object* ret)
{
- int i, tot;
+ int tot;
Object* type;
FFIArgInfo ret_info;
FFIArgInfo* args_info;
@@ -623,7 +623,7 @@ namespace rubinius {
if(tot > 0) {
args_info = ALLOC_N(FFIArgInfo, tot);
- for(i = 0; i < tot; i++) {
+ for(int i = 0; i < tot; i++) {
type = args->get(state, i);
if(type->fixnum_p()) {
args_info[i].type = as<Integer>(type)->to_native();
@@ -293,7 +293,7 @@ namespace rubinius {
}
case ARG_COUNT_ARGS_IN_C_ARRAY_PLUS_RECEIVER: {
- VALUE* ary = (VALUE*)alloca(sizeof(VALUE) * args.total());
+ VALUE ary[args.total()];
for (std::size_t i = 0; i < args.total(); ++i) {
ary[i] = env->get_handle(args.get_argument(i));
@@ -578,7 +578,7 @@ namespace rubinius {
case C_BLOCK_CALL: {
VALUE val;
- VALUE* ary = (VALUE*)alloca(sizeof(VALUE) * args.total());
+ VALUE ary[args.total()];
VALUE cb_data = env->get_handle(nm->get_ivar(state, state->symbol("cb_data")));
if(args.total() > 0) {
View
@@ -610,12 +610,10 @@ namespace rubinius {
}
Object* Object::set_ivar(STATE, Symbol* sym, Object* val) {
- LookupTable* tbl;
-
/* Implements the external ivars table for objects that don't
have their own space for ivars. */
if(!reference_p()) {
- tbl = try_as<LookupTable>(G(external_ivars)->fetch(state, this));
+ LookupTable* tbl = try_as<LookupTable>(G(external_ivars)->fetch(state, this));
if(!tbl) {
tbl = LookupTable::create(state);
@@ -113,7 +113,6 @@ namespace rubinius {
Bignum* big;
size_t longs;
- uint32_t* data;
if(!seed->fixnum_p())
seed = as<Bignum>(seed)->abs(state);
@@ -131,7 +130,7 @@ namespace rubinius {
}
longs = sizeof(native_uint) / 4;
- data = (uint32_t*)alloca(longs * 4);
+ uint32_t data[longs];
for(unsigned int i = 0; i < longs; i++) {
data[i] = (uint32_t)(s >> (i * 32));
}
@@ -142,7 +141,7 @@ namespace rubinius {
longs = big->into_array(state, NULL, 0);
- data = (uint32_t*)alloca(longs * 4);
+ uint32_t data[longs];
big->into_array(state, data, longs);
init_by_array(data, longs);
@@ -161,10 +160,10 @@ namespace rubinius {
Bignum* max_big = as<Bignum>(max);
size_t longs = max_big->into_array(state, NULL, 0);
- uint32_t* max_data = (uint32_t*)alloca(longs * 4);
+ uint32_t max_data[longs];
max_big->into_array(state, max_data, longs);
- uint32_t* result_data = (uint32_t*)alloca(longs * 4);
+ uint32_t result_data[longs];
retry:
uint32_t mask = 0;
View
@@ -340,7 +340,7 @@ namespace rubinius {
Regexp* Regexp::allocate(STATE, Object* self) {
Regexp* re = Regexp::create(state);
re->onig_data = NULL;
- re->klass(state, (Class*)self);
+ re->klass(state, as<Class>(self));
return re;
}
@@ -17,6 +17,9 @@
namespace rubinius {
BytecodeVerification::BytecodeVerification(CompiledCode* code)
: method_(code)
+ , ops_(NULL)
+ , total_(0)
+ , max_stack_allowed_(0)
, max_stack_seen_(0)
, stack_(0)
, locals_(0)
View
@@ -191,7 +191,7 @@ namespace rubinius {
}
NativeMethodFrame* native_method_frame() {
- if(native_method_p()) return (NativeMethodFrame*)dispatch_data;
+ if(native_method_p()) return reinterpret_cast<NativeMethodFrame*>(dispatch_data);
return 0;
}
@@ -268,7 +268,7 @@ namespace rubinius {
};
#define ALLOCA_CALLFRAME(stack_size) \
- (InterpreterCallFrame*)alloca(sizeof(InterpreterCallFrame) + (sizeof(Object*) * stack_size))
+ reinterpret_cast<InterpreterCallFrame*>(alloca(sizeof(InterpreterCallFrame) + (sizeof(Object*) * stack_size)))
};
#endif
View
@@ -246,8 +246,7 @@ namespace rubinius {
va_list varargs;
va_start(varargs, arg_count);
- Object** args = reinterpret_cast<Object**>(
- alloca(sizeof(Object*) * arg_count));
+ Object* args[arg_count];
for(int i = 0; i < arg_count; i++) {
args[i] = env->get_object(va_arg(varargs, VALUE));
@@ -513,7 +512,7 @@ extern "C" {
VALUE rb_call_super(int argc, const VALUE *argv) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
- Object** args = reinterpret_cast<Object**>(alloca(sizeof(Object*) * argc));
+ Object* args[argc];
for(int i = 0; i < argc; i++) {
args[i] = env->get_object(argv[i]);
}
@@ -527,7 +526,7 @@ extern "C" {
va_list varargs;
va_start(varargs, arg_count);
- Object** args = reinterpret_cast<Object**>(alloca(sizeof(Object*) * arg_count));
+ Object* args[arg_count];
for(int i = 0; i < arg_count; i++) {
args[i] = env->get_object(va_arg(varargs, VALUE));
@@ -551,7 +550,7 @@ extern "C" {
VALUE rb_funcall2(VALUE receiver, ID method_name, int arg_count, const VALUE* v_args) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
- Object** args = reinterpret_cast<Object**>(alloca(sizeof(Object**) * arg_count));
+ Object* args[arg_count];
for(int i = 0; i < arg_count; i++) {
args[i] = env->get_object(v_args[i]);
@@ -575,7 +574,7 @@ extern "C" {
{
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
- Object** args = reinterpret_cast<Object**>(alloca(sizeof(Object**) * arg_count));
+ Object* args[arg_count];
for(int i = 0; i < arg_count; i++) {
args[i] = env->get_object(v_args[i]);
@@ -614,7 +613,7 @@ extern "C" {
return capi_yield_backend(env, blk, 0, 0);
}
- Object** vars = reinterpret_cast<Object**>(alloca(sizeof(Object*) * n));
+ Object* vars[n];
va_list args;
va_start(args, n);
@@ -639,7 +638,7 @@ extern "C" {
if(Array* ary = try_as<Array>(env->get_object(array_handle))) {
int count = ary->size();
- Object** vars = reinterpret_cast<Object**>(alloca(sizeof(Object*) * count));
+ Object* vars[count];
for(int i = 0; i < count; i++) {
vars[i] = ary->get(env->state(), i);
View
@@ -6,11 +6,11 @@ using namespace rubinius::capi;
extern "C" {
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) {
- VALUE* args = (VALUE*)alloca(sizeof(VALUE) * (argc + 2));
-
+ VALUE args[argc + 2];
+
args[0] = obj;
args[1] = meth;
-
+
for(int i = 0; i < argc; i++) {
args[i + 2] = argv[i];
}
View
@@ -40,7 +40,7 @@ extern "C" {
VALUE sz = rb_funcall2(klass, rb_intern("length"), 0, 0);
int total = FIX2INT(sz);
- VALUE* mem = (VALUE*)alloca(sizeof(VALUE) * total);
+ VALUE mem[total];
for(int i = 0; i < total; i++) {
mem[i] = va_arg(args, VALUE);
}
@@ -743,7 +743,7 @@ def generate_marks(cpp)
{
if(target->#{name}()->reference_p()) {
Object* res = mark.call(target->#{name}());
- if(res) target->#{name}(mark.state(), (#{type}*)res);
+ if(res) target->#{name}(mark.state(), reinterpret_cast<#{type}*>(res));
}
}
View
@@ -80,6 +80,7 @@ namespace rubinius {
, argv_(argv)
, signature_(0)
, version_(0)
+ , sig_handler_(NULL)
{
#ifdef ENABLE_LLVM
if(!llvm::llvm_start_multithreaded()) {
@@ -355,8 +356,7 @@ namespace rubinius {
e = b;
while(*e && !isspace(*e)) e++;
- int len;
- if((len = e - b) > 0) {
+ if(e - b > 0) {
if(strncmp(b, "-X", 2) == 0) {
*e = 0;
config_parser.import_line(b + 2);
Oops, something went wrong.

0 comments on commit a9359ec

Please sign in to comment.