factor/vm/quotations.cpp

389 lines
9.9 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
2009-05-02 05:04:19 -04:00
/* Simple non-optimizing compiler.
This is one of the two compilers implementing Factor; the second one is written
2009-11-03 05:56:58 -05:00
in Factor and performs advanced optimizations. See basis/compiler/compiler.factor.
2009-05-02 05:04:19 -04:00
The non-optimizing compiler compiles a quotation at a time by concatenating
machine code chunks; prolog, epilog, call word, jump to word, etc. These machine
2009-11-03 05:56:58 -05:00
code chunks are generated from Factor code in basis/cpu/.../bootstrap.factor.
2009-05-02 05:04:19 -04:00
Calls to words and constant quotations (referenced by conditionals and dips)
are direct jumps to machine code blocks. Literals are also referenced directly
without going through the literal table.
It actually does do a little bit of very simple optimization:
1) Tail call optimization.
2) If a quotation is determined to not call any other words (except for a few
special words which are open-coded, see below), then no prolog/epilog is
generated.
3) When in tail position and immediately preceded by literal arguments, the
'if' is generated inline, instead of as a call to the 'if' word.
4) When preceded by a quotation, calls to 'dip', '2dip' and '3dip' are
open-coded as retain stack manipulation surrounding a subroutine call.
5) Sub-primitives are primitive words which are implemented in assembly and not
in the VM. They are open-coded and no subroutine call is generated. This
includes stack shufflers, some fixnum arithmetic words, and words such as tag,
slot and eq?. A primitive call is relatively expensive (two subroutine calls)
so this results in a big speedup for relatively little effort. */
void quotation_jit::init_quotation(cell quot)
{
elements = untag<quotation>(quot)->array;
}
bool quotation_jit::primitive_call_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
2009-10-23 01:33:53 -04:00
return (i + 2) == length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_PRIMITIVE_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::fast_if_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
return (i + 3) == length
2009-05-04 05:50:24 -04:00
&& tagged<object>(array_nth(elements.untagged(),i + 1)).type_p(QUOTATION_TYPE)
2009-10-23 01:33:53 -04:00
&& array_nth(elements.untagged(),i + 2) == parent->special_objects[JIT_IF_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::fast_dip_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
2009-10-23 01:33:53 -04:00
return (i + 2) <= length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_DIP_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::fast_2dip_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
2009-10-23 01:33:53 -04:00
return (i + 2) <= length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_2DIP_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::fast_3dip_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
2009-10-23 01:33:53 -04:00
return (i + 2) <= length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_3DIP_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::mega_lookup_p(cell i, cell length)
2009-05-02 05:04:19 -04:00
{
return (i + 4) <= length
2009-05-04 05:50:24 -04:00
&& tagged<object>(array_nth(elements.untagged(),i + 1)).type_p(FIXNUM_TYPE)
&& tagged<object>(array_nth(elements.untagged(),i + 2)).type_p(ARRAY_TYPE)
2009-10-23 01:33:53 -04:00
&& array_nth(elements.untagged(),i + 3) == parent->special_objects[MEGA_LOOKUP_WORD];
2009-05-02 05:04:19 -04:00
}
bool quotation_jit::declare_p(cell i, cell length)
{
return (i + 2) <= length
2009-10-23 01:33:53 -04:00
&& array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_DECLARE_WORD];
}
bool quotation_jit::word_stack_frame_p(cell obj)
{
return to_boolean(untag<word>(obj)->subprimitive)
|| obj == parent->special_objects[JIT_PRIMITIVE_WORD];
}
2009-05-02 10:19:09 -04:00
bool quotation_jit::stack_frame_p()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
fixnum length = array_capacity(elements.untagged());
2009-05-02 05:04:19 -04:00
for(fixnum i = 0; i < length; i++)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell obj = array_nth(elements.untagged(),i);
switch(tagged<object>(obj).type())
2009-05-02 05:04:19 -04:00
{
case WORD_TYPE:
if(i != length - 1 || word_stack_frame_p(obj))
2009-05-02 05:04:19 -04:00
return true;
break;
case QUOTATION_TYPE:
if(fast_dip_p(i,length) || fast_2dip_p(i,length) || fast_3dip_p(i,length))
2009-05-02 05:04:19 -04:00
return true;
break;
default:
break;
2009-05-02 05:04:19 -04:00
}
}
return false;
}
bool quotation_jit::trivial_quotation_p(array *elements)
{
return array_capacity(elements) == 1 && tagged<object>(array_nth(elements,0)).type_p(WORD_TYPE);
}
void quotation_jit::emit_quot(cell quot_)
{
data_root<quotation> quot(quot_,parent);
array *elements = untag<array>(quot->array);
/* If the quotation consists of a single word, compile a direct call
to the word. */
if(trivial_quotation_p(elements))
literal(array_nth(elements,0));
else
{
if(compiling) parent->jit_compile_quot(quot.value(),relocate);
literal(quot.value());
}
}
2009-05-02 05:04:19 -04:00
/* Allocates memory */
2009-05-02 10:19:09 -04:00
void quotation_jit::iterate_quotation()
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
bool stack_frame = stack_frame_p();
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
set_position(0);
2009-05-02 05:04:19 -04:00
if(stack_frame)
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_PROLOG]);
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell i;
cell length = array_capacity(elements.untagged());
2009-05-02 05:04:19 -04:00
bool tail_call = false;
for(i = 0; i < length; i++)
{
2009-05-02 10:19:09 -04:00
set_position(i);
2009-05-02 05:04:19 -04:00
data_root<object> obj(array_nth(elements.untagged(),i),parent);
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
switch(obj.type())
2009-05-02 05:04:19 -04:00
{
case WORD_TYPE:
/* Sub-primitives */
if(to_boolean(obj.as<word>()->subprimitive))
2009-05-02 05:04:19 -04:00
{
tail_call = emit_subprimitive(obj.value(), /* word */
i == length - 1, /* tail_call_p */
stack_frame); /* stack_frame_p */
2009-05-02 05:04:19 -04:00
}
/* Everything else */
else if(i == length - 1)
2009-05-02 05:04:19 -04:00
{
if(stack_frame) emit(parent->special_objects[JIT_EPILOG]);
tail_call = true;
word_jump(obj.value());
2009-05-02 05:04:19 -04:00
}
else
word_call(obj.value());
2009-05-02 05:04:19 -04:00
break;
case WRAPPER_TYPE:
2009-05-04 05:50:24 -04:00
push(obj.as<wrapper>()->object);
2009-05-02 05:04:19 -04:00
break;
case FIXNUM_TYPE:
/* Primitive calls */
if(primitive_call_p(i,length))
2009-05-02 05:04:19 -04:00
{
/* On PowerPC, the VM pointer is stored as a register; on other
platforms, the RT_VM relocation is used and it needs an offset
parameter */
#ifndef FACTOR_PPC
parameter(tag_fixnum(0));
#endif
parameter(obj.value());
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_PRIMITIVE]);
2009-05-02 05:04:19 -04:00
i++;
}
else
push(obj.value());
break;
2009-05-02 05:04:19 -04:00
case QUOTATION_TYPE:
/* 'if' preceeded by two literal quotations (this is why if and ? are
mutually recursive in the library, but both still work) */
if(fast_if_p(i,length))
2009-05-02 05:04:19 -04:00
{
2009-10-23 01:33:53 -04:00
if(stack_frame) emit(parent->special_objects[JIT_EPILOG]);
2009-05-02 10:19:09 -04:00
tail_call = true;
2009-05-02 05:04:19 -04:00
emit_quot(array_nth(elements.untagged(),i));
emit_quot(array_nth(elements.untagged(),i + 1));
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_IF]);
2009-05-02 05:04:19 -04:00
i += 2;
}
/* dip */
else if(fast_dip_p(i,length))
2009-05-02 05:04:19 -04:00
{
emit_quot(obj.value());
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_DIP]);
2009-05-02 05:04:19 -04:00
i++;
}
/* 2dip */
else if(fast_2dip_p(i,length))
2009-05-02 05:04:19 -04:00
{
emit_quot(obj.value());
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_2DIP]);
2009-05-02 05:04:19 -04:00
i++;
}
/* 3dip */
else if(fast_3dip_p(i,length))
2009-05-02 05:04:19 -04:00
{
emit_quot(obj.value());
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_3DIP]);
2009-05-02 05:04:19 -04:00
i++;
}
else
push(obj.value());
break;
2009-05-02 05:04:19 -04:00
case ARRAY_TYPE:
/* Method dispatch */
if(mega_lookup_p(i,length))
2009-05-02 05:04:19 -04:00
{
if(stack_frame) emit(parent->special_objects[JIT_EPILOG]);
tail_call = true;
2009-05-02 10:19:09 -04:00
emit_mega_cache_lookup(
2009-05-04 05:50:24 -04:00
array_nth(elements.untagged(),i),
untag_fixnum(array_nth(elements.untagged(),i + 1)),
array_nth(elements.untagged(),i + 2));
2009-05-02 05:04:19 -04:00
i += 3;
}
/* Non-optimizing compiler ignores declarations */
else if(declare_p(i,length))
i++;
else
push(obj.value());
break;
2009-05-02 05:04:19 -04:00
default:
2009-05-02 10:19:09 -04:00
push(obj.value());
2009-05-02 05:04:19 -04:00
break;
}
}
if(!tail_call)
{
2009-05-02 10:19:09 -04:00
set_position(length);
2009-05-02 05:04:19 -04:00
if(stack_frame) emit(parent->special_objects[JIT_EPILOG]);
2009-10-23 01:33:53 -04:00
emit(parent->special_objects[JIT_RETURN]);
2009-05-02 05:04:19 -04:00
}
}
2009-09-23 14:05:46 -04:00
void factor_vm::set_quot_xt(quotation *quot, code_block *code)
2009-05-02 05:04:19 -04:00
{
quot->code = code;
2009-05-04 05:50:24 -04:00
quot->xt = code->xt();
2009-05-02 05:04:19 -04:00
}
/* Allocates memory */
code_block *factor_vm::jit_compile_quot(cell owner_, cell quot_, bool relocating)
2009-05-02 05:04:19 -04:00
{
data_root<object> owner(owner_,this);
data_root<quotation> quot(quot_,this);
2009-05-02 05:04:19 -04:00
quotation_jit compiler(owner.value(),true,relocating,this);
compiler.init_quotation(quot.value());
2009-05-04 05:50:24 -04:00
compiler.iterate_quotation();
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
code_block *compiled = compiler.to_code_block();
2009-05-02 05:04:19 -04:00
if(relocating) initialize_code_block(compiled);
return compiled;
}
void factor_vm::jit_compile_quot(cell quot_, bool relocating)
{
data_root<quotation> quot(quot_,this);
if(!quot_compiled_p(quot.untagged()))
{
code_block *compiled = jit_compile_quot(quot.value(),quot.value(),relocating);
set_quot_xt(quot.untagged(),compiled);
}
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_jit_compile()
2009-05-02 05:04:19 -04:00
{
jit_compile_quot(ctx->pop(),true);
2009-05-02 05:04:19 -04:00
}
code_block *factor_vm::lazy_jit_compile_block()
{
return untag<word>(special_objects[LAZY_JIT_COMPILE_WORD])->code;
}
2009-05-02 05:04:19 -04:00
/* push a new quotation on the stack */
void factor_vm::primitive_array_to_quotation()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
quotation *quot = allot<quotation>(sizeof(quotation));
quot->array = ctx->peek();
quot->cached_effect = false_object;
quot->cache_counter = false_object;
set_quot_xt(quot,lazy_jit_compile_block());
ctx->replace(tag<quotation>(quot));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_quotation_xt()
2009-05-02 05:04:19 -04:00
{
quotation *quot = untag_check<quotation>(ctx->peek());
ctx->replace(allot_cell((cell)quot->xt));
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
/* Allocates memory */
2009-09-23 14:05:46 -04:00
fixnum factor_vm::quot_code_offset_to_scan(cell quot_, cell offset)
2009-05-02 10:19:09 -04:00
{
data_root<quotation> quot(quot_,this);
data_root<array> array(quot->array,this);
2009-05-02 10:19:09 -04:00
2009-08-17 16:37:09 -04:00
quotation_jit compiler(quot.value(),false,false,this);
compiler.init_quotation(quot.value());
2009-05-04 06:07:14 -04:00
compiler.compute_position(offset);
compiler.iterate_quotation();
2009-05-02 10:19:09 -04:00
2009-05-04 06:07:14 -04:00
return compiler.get_position();
2009-05-02 10:19:09 -04:00
}
2009-12-23 07:37:24 -05:00
cell factor_vm::lazy_jit_compile(cell quot_)
{
data_root<quotation> quot(quot_,this);
jit_compile_quot(quot.value(),true);
return quot.value();
}
2009-05-04 02:46:13 -04:00
2009-12-23 07:37:24 -05:00
VM_C_API cell lazy_jit_compile(cell quot, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
2009-12-23 07:37:24 -05:00
return parent->lazy_jit_compile(quot);
2009-08-17 16:37:08 -04:00
}
bool factor_vm::quot_compiled_p(quotation *quot)
{
return quot->code != NULL && quot->code != lazy_jit_compile_block();
}
void factor_vm::primitive_quot_compiled_p()
{
tagged<quotation> quot(ctx->pop());
2009-08-17 16:37:15 -04:00
quot.untag_check(this);
ctx->push(tag_boolean(quot_compiled_p(quot.untagged())));
}
cell factor_vm::find_all_quotations()
{
return instances(QUOTATION_TYPE);
}
void factor_vm::initialize_all_quotations()
{
data_root<array> quotations(find_all_quotations(),this);
cell length = array_capacity(quotations.untagged());
for(cell i = 0; i < length; i++)
{
data_root<quotation> quot(array_nth(quotations.untagged(),i),this);
if(!quot->code)
set_quot_xt(quot.untagged(),lazy_jit_compile_block());
}
}
2009-05-04 02:46:13 -04:00
}