VM: Refactor jit.cpp/hpp to Factor style
							parent
							
								
									894e181f31
								
							
						
					
					
						commit
						1eaddb0068
					
				
							
								
								
									
										113
									
								
								vm/jit.cpp
								
								
								
								
							
							
						
						
									
										113
									
								
								vm/jit.cpp
								
								
								
								
							| 
						 | 
				
			
			@ -1,7 +1,6 @@
 | 
			
		|||
#include "master.hpp"
 | 
			
		||||
 | 
			
		||||
namespace factor
 | 
			
		||||
{
 | 
			
		||||
namespace factor {
 | 
			
		||||
 | 
			
		||||
/* Simple code generator used by:
 | 
			
		||||
- quotation compiler (quotations.cpp),
 | 
			
		||||
| 
						 | 
				
			
			@ -9,9 +8,9 @@ namespace factor
 | 
			
		|||
- polymorphic inline caches (inline_cache.cpp) */
 | 
			
		||||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
 | 
			
		||||
jit::jit(code_block_type type_, cell owner_, factor_vm* vm)
 | 
			
		||||
    : type(type_),
 | 
			
		||||
	  owner(owner_,vm),
 | 
			
		||||
      owner(owner_, vm),
 | 
			
		||||
      code(vm),
 | 
			
		||||
      relocation(vm),
 | 
			
		||||
      parameters(vm),
 | 
			
		||||
| 
						 | 
				
			
			@ -19,59 +18,49 @@ jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
 | 
			
		|||
      computing_offset_p(false),
 | 
			
		||||
      position(0),
 | 
			
		||||
      offset(0),
 | 
			
		||||
	  parent(vm)
 | 
			
		||||
{
 | 
			
		||||
      parent(vm) {
 | 
			
		||||
  fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
 | 
			
		||||
  FACTOR_ASSERT(old_count >= 0);
 | 
			
		||||
	(void)old_count;
 | 
			
		||||
  (void) old_count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jit::~jit()
 | 
			
		||||
{
 | 
			
		||||
jit::~jit() {
 | 
			
		||||
  fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
 | 
			
		||||
  FACTOR_ASSERT(old_count >= 1);
 | 
			
		||||
	(void)old_count;
 | 
			
		||||
  (void) old_count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void jit::emit_relocation(cell relocation_template_)
 | 
			
		||||
{
 | 
			
		||||
	data_root<byte_array> relocation_template(relocation_template_,parent);
 | 
			
		||||
	cell capacity = array_capacity(relocation_template.untagged())
 | 
			
		||||
		/ sizeof(relocation_entry);
 | 
			
		||||
	relocation_entry *relocations = relocation_template->data<relocation_entry>();
 | 
			
		||||
	for(cell i = 0; i < capacity; i++)
 | 
			
		||||
	{
 | 
			
		||||
void jit::emit_relocation(cell relocation_template_) {
 | 
			
		||||
  data_root<byte_array> relocation_template(relocation_template_, parent);
 | 
			
		||||
  cell capacity =
 | 
			
		||||
      array_capacity(relocation_template.untagged()) / sizeof(relocation_entry);
 | 
			
		||||
  relocation_entry* relocations = relocation_template->data<relocation_entry>();
 | 
			
		||||
  for (cell i = 0; i < capacity; i++) {
 | 
			
		||||
    relocation_entry entry = relocations[i];
 | 
			
		||||
    relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
 | 
			
		||||
                               entry.rel_offset() + code.count);
 | 
			
		||||
		relocation.append_bytes(&new_entry,sizeof(relocation_entry));
 | 
			
		||||
    relocation.append_bytes(&new_entry, sizeof(relocation_entry));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
void jit::emit(cell code_template_)
 | 
			
		||||
{
 | 
			
		||||
	data_root<array> code_template(code_template_,parent);
 | 
			
		||||
void jit::emit(cell code_template_) {
 | 
			
		||||
  data_root<array> code_template(code_template_, parent);
 | 
			
		||||
 | 
			
		||||
	emit_relocation(array_nth(code_template.untagged(),0));
 | 
			
		||||
  emit_relocation(array_nth(code_template.untagged(), 0));
 | 
			
		||||
 | 
			
		||||
	data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
 | 
			
		||||
  data_root<byte_array> insns(array_nth(code_template.untagged(), 1), parent);
 | 
			
		||||
 | 
			
		||||
	if(computing_offset_p)
 | 
			
		||||
	{
 | 
			
		||||
  if (computing_offset_p) {
 | 
			
		||||
    cell size = array_capacity(insns.untagged());
 | 
			
		||||
 | 
			
		||||
		if(offset == 0)
 | 
			
		||||
		{
 | 
			
		||||
    if (offset == 0) {
 | 
			
		||||
      position--;
 | 
			
		||||
      computing_offset_p = false;
 | 
			
		||||
		}
 | 
			
		||||
		else if(offset < size)
 | 
			
		||||
		{
 | 
			
		||||
    } else if (offset < size) {
 | 
			
		||||
      position++;
 | 
			
		||||
      computing_offset_p = false;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
    } else
 | 
			
		||||
      offset -= size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -80,39 +69,36 @@ void jit::emit(cell code_template_)
 | 
			
		|||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
void jit::emit_with_literal(cell code_template_, cell argument_) {
 | 
			
		||||
	data_root<array> code_template(code_template_,parent);
 | 
			
		||||
	data_root<object> argument(argument_,parent);
 | 
			
		||||
  data_root<array> code_template(code_template_, parent);
 | 
			
		||||
  data_root<object> argument(argument_, parent);
 | 
			
		||||
  literal(argument.value());
 | 
			
		||||
  emit(code_template.value());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
void jit::emit_with_parameter(cell code_template_, cell argument_) {
 | 
			
		||||
	data_root<array> code_template(code_template_,parent);
 | 
			
		||||
	data_root<object> argument(argument_,parent);
 | 
			
		||||
  data_root<array> code_template(code_template_, parent);
 | 
			
		||||
  data_root<object> argument(argument_, parent);
 | 
			
		||||
  parameter(argument.value());
 | 
			
		||||
  emit(code_template.value());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
 | 
			
		||||
{
 | 
			
		||||
	data_root<word> word(word_,parent);
 | 
			
		||||
	data_root<array> code_template(word->subprimitive,parent);
 | 
			
		||||
	parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
 | 
			
		||||
	literals.append(untag<array>(array_nth(code_template.untagged(),1)));
 | 
			
		||||
	emit(array_nth(code_template.untagged(),2));
 | 
			
		||||
bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p) {
 | 
			
		||||
  data_root<word> word(word_, parent);
 | 
			
		||||
  data_root<array> code_template(word->subprimitive, parent);
 | 
			
		||||
  parameters.append(untag<array>(array_nth(code_template.untagged(), 0)));
 | 
			
		||||
  literals.append(untag<array>(array_nth(code_template.untagged(), 1)));
 | 
			
		||||
  emit(array_nth(code_template.untagged(), 2));
 | 
			
		||||
 | 
			
		||||
	if(array_capacity(code_template.untagged()) == 5)
 | 
			
		||||
	{
 | 
			
		||||
		if(tail_call_p)
 | 
			
		||||
		{
 | 
			
		||||
			if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
 | 
			
		||||
			emit(array_nth(code_template.untagged(),4));
 | 
			
		||||
  if (array_capacity(code_template.untagged()) == 5) {
 | 
			
		||||
    if (tail_call_p) {
 | 
			
		||||
      if (stack_frame_p)
 | 
			
		||||
        emit(parent->special_objects[JIT_EPILOG]);
 | 
			
		||||
      emit(array_nth(code_template.untagged(), 4));
 | 
			
		||||
      return true;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			emit(array_nth(code_template.untagged(),3));
 | 
			
		||||
    } else
 | 
			
		||||
      emit(array_nth(code_template.untagged(), 3));
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -120,20 +106,18 @@ bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
 | 
			
		|||
/* Facility to convert compiled code offsets to quotation offsets.
 | 
			
		||||
Call jit_compute_offset() with the compiled code offset, then emit
 | 
			
		||||
code, and at the end jit->position is the quotation position. */
 | 
			
		||||
void jit::compute_position(cell offset_)
 | 
			
		||||
{
 | 
			
		||||
void jit::compute_position(cell offset_) {
 | 
			
		||||
  computing_offset_p = true;
 | 
			
		||||
  position = 0;
 | 
			
		||||
  offset = offset_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Allocates memory */
 | 
			
		||||
code_block *jit::to_code_block(cell frame_size)
 | 
			
		||||
{
 | 
			
		||||
code_block* jit::to_code_block(cell frame_size) {
 | 
			
		||||
  /* Emit dummy GC info */
 | 
			
		||||
	code.grow_bytes(alignment_for(code.count + 4,data_alignment));
 | 
			
		||||
  code.grow_bytes(alignment_for(code.count + 4, data_alignment));
 | 
			
		||||
  u32 dummy_gc_info = 0;
 | 
			
		||||
	code.append_bytes(&dummy_gc_info,sizeof(u32));
 | 
			
		||||
  code.append_bytes(&dummy_gc_info, sizeof(u32));
 | 
			
		||||
 | 
			
		||||
  code.trim();
 | 
			
		||||
  relocation.trim();
 | 
			
		||||
| 
						 | 
				
			
			@ -141,14 +125,9 @@ code_block *jit::to_code_block(cell frame_size)
 | 
			
		|||
  literals.trim();
 | 
			
		||||
 | 
			
		||||
  return parent->add_code_block(
 | 
			
		||||
		type,
 | 
			
		||||
		code.elements.value(),
 | 
			
		||||
		false_object, /* no labels */
 | 
			
		||||
		owner.value(),
 | 
			
		||||
		relocation.elements.value(),
 | 
			
		||||
		parameters.elements.value(),
 | 
			
		||||
		literals.elements.value(),
 | 
			
		||||
		frame_size);
 | 
			
		||||
      type, code.elements.value(), false_object, /* no labels */
 | 
			
		||||
      owner.value(), relocation.elements.value(), parameters.elements.value(),
 | 
			
		||||
      literals.elements.value(), frame_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										39
									
								
								vm/jit.hpp
								
								
								
								
							
							
						
						
									
										39
									
								
								vm/jit.hpp
								
								
								
								
							| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
namespace factor
 | 
			
		||||
{
 | 
			
		||||
namespace factor {
 | 
			
		||||
 | 
			
		||||
struct jit {
 | 
			
		||||
  code_block_type type;
 | 
			
		||||
| 
						 | 
				
			
			@ -11,9 +10,9 @@ struct jit {
 | 
			
		|||
  bool computing_offset_p;
 | 
			
		||||
  fixnum position;
 | 
			
		||||
  cell offset;
 | 
			
		||||
	factor_vm *parent;
 | 
			
		||||
  factor_vm* parent;
 | 
			
		||||
 | 
			
		||||
	explicit jit(code_block_type type, cell owner, factor_vm *parent);
 | 
			
		||||
  explicit jit(code_block_type type, cell owner, factor_vm* parent);
 | 
			
		||||
  ~jit();
 | 
			
		||||
 | 
			
		||||
  void compute_position(cell offset);
 | 
			
		||||
| 
						 | 
				
			
			@ -27,14 +26,12 @@ struct jit {
 | 
			
		|||
  void literal(cell literal) { literals.add(literal); }
 | 
			
		||||
  void emit_with_literal(cell code_template_, cell literal_);
 | 
			
		||||
 | 
			
		||||
	void push(cell literal)
 | 
			
		||||
	{
 | 
			
		||||
		emit_with_literal(parent->special_objects[JIT_PUSH_IMMEDIATE],literal);
 | 
			
		||||
  void push(cell literal) {
 | 
			
		||||
    emit_with_literal(parent->special_objects[JIT_PUSH_IMMEDIATE], literal);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	void word_jump(cell word_)
 | 
			
		||||
	{
 | 
			
		||||
		data_root<word> word(word_,parent);
 | 
			
		||||
  void word_jump(cell word_) {
 | 
			
		||||
    data_root<word> word(word_, parent);
 | 
			
		||||
#ifndef FACTOR_AMD64
 | 
			
		||||
    literal(tag_fixnum(xt_tail_pic_offset));
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -42,33 +39,27 @@ struct jit {
 | 
			
		|||
    emit(parent->special_objects[JIT_WORD_JUMP]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	void word_call(cell word)
 | 
			
		||||
	{
 | 
			
		||||
		emit_with_literal(parent->special_objects[JIT_WORD_CALL],word);
 | 
			
		||||
  void word_call(cell word) {
 | 
			
		||||
    emit_with_literal(parent->special_objects[JIT_WORD_CALL], word);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p);
 | 
			
		||||
 | 
			
		||||
	fixnum get_position()
 | 
			
		||||
	{
 | 
			
		||||
		if(computing_offset_p)
 | 
			
		||||
		{
 | 
			
		||||
  fixnum get_position() {
 | 
			
		||||
    if (computing_offset_p) {
 | 
			
		||||
      /* If this is still on, emit() didn't clear it,
 | 
			
		||||
         so the offset was out of bounds */
 | 
			
		||||
      return -1;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
    } else
 | 
			
		||||
      return position;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	void set_position(fixnum position_)
 | 
			
		||||
	{
 | 
			
		||||
		if(computing_offset_p)
 | 
			
		||||
  void set_position(fixnum position_) {
 | 
			
		||||
    if (computing_offset_p)
 | 
			
		||||
      position = position_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	code_block *to_code_block(cell frame_size);
 | 
			
		||||
  code_block* to_code_block(cell frame_size);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  jit(const jit&);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue