"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/src/libponyc/codegen/genbox.c" (1 Nov 2019, 1596 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "genbox.c" see the Fossies "Dox" file reference documentation.

    1 #include "genbox.h"
    2 #include "gencall.h"
    3 #include "genexpr.h"
    4 #include "genfun.h"
    5 #include "genname.h"
    6 #include "gentype.h"
    7 #include "ponyassert.h"
    8 
    9 LLVMValueRef gen_box(compile_t* c, ast_t* type, LLVMValueRef value)
   10 {
   11   LLVMTypeRef l_type = LLVMTypeOf(value);
   12 
   13   if(LLVMGetTypeKind(l_type) == LLVMPointerTypeKind)
   14     return value;
   15 
   16   reach_type_t* t = reach_type(c->reach, type);
   17   pony_assert(t != NULL);
   18   compile_type_t* c_t = (compile_type_t*)t->c_type;
   19 
   20   if(l_type != c_t->primitive && l_type != c_t->mem_type)
   21     return NULL;
   22 
   23   value = gen_assign_cast(c, c_t->mem_type, value, t->ast_cap);
   24 
   25   // Allocate the object.
   26   LLVMValueRef this_ptr = gencall_allocstruct(c, t);
   27 
   28   // Store the primitive in element 1.
   29   LLVMValueRef value_ptr = LLVMBuildStructGEP(c->builder, this_ptr, 1, "");
   30   LLVMBuildStore(c->builder, value, value_ptr);
   31 
   32   return this_ptr;
   33 }
   34 
   35 LLVMValueRef gen_unbox(compile_t* c, ast_t* type, LLVMValueRef object)
   36 {
   37   LLVMTypeRef l_type = LLVMTypeOf(object);
   38 
   39   if(LLVMGetTypeKind(l_type) != LLVMPointerTypeKind)
   40     return object;
   41 
   42   reach_type_t* t = reach_type(c->reach, type);
   43   pony_assert(t != NULL);
   44   compile_type_t* c_t = (compile_type_t*)t->c_type;
   45 
   46   if(c_t->primitive == NULL)
   47     return object;
   48 
   49   // Extract the primitive type from element 1 and return it.
   50   LLVMValueRef this_ptr = LLVMBuildBitCast(c->builder, object,
   51     c_t->structure_ptr, "");
   52   LLVMValueRef value_ptr = LLVMBuildStructGEP(c->builder, this_ptr, 1, "");
   53 
   54   LLVMValueRef value = LLVMBuildLoad(c->builder, value_ptr, "");
   55 
   56   return gen_assign_cast(c, c_t->use_type, value, t->ast_cap);
   57 }