Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- and handleArrayExp(ll_env, cg_ctxt, size, init, ty) =
- let
- val _ = print("HandleArrayExp called. \n")
- (************ FROM PDF ********
- Array allocation happens via another runtime function,
- called initArray. Similarly to allocRecord, it returns
- the pointer to the allocated memory. Indexing into
- arrays happens via GEP.
- *)
- (*evalute the sub exps size and init*)
- val (operInit, b_init) = cgExp ll_env cg_ctxt init
- val init_llTy = ty_to_llty ((#ty o #aug) init )
- val (operSize, b_size) = cgExp ll_env cg_ctxt size
- val size_llTy = ty_to_llty ((#ty o #aug) size )
- (* Determine the size of each element in the array !!!*)
- (* Get a pointer that points to address sizeof(init_llty) *)
- val (oper_elem_size, b1) = build_gep ll_env "array_size_ptr" (init_llTy, ll.Null, [ll.Const 1])
- (* Get the addr of the pointer as an int. We now have sizeof(ll_array) *)
- val elemSizePtrToInt_insn = ll.Ptrtoint(init_llTy, oper_elem_size, ll.I64)
- val (elemSize, b2) = emit_mk_PP ll_env ("array_size", elemSizePtrToInt_insn)
- (* To fill the array with the init value, we must supply initArray() with a void* that points to an adress with the value *)
- (* Make a pointer, *)
- val init_ptr_uid = mk_uid ll_env "array_init_void_ptr"
- val b_alloca_init = emit_alloca (init_ptr_uid, init_llTy)
- (* that points to the init, of type void* *)
- val b_store_init = build_store (init_llTy, operInit, ll.Id init_ptr_uid)
- val (oper_init_ptr_cast, b_init_ptr_cast) = build_bitcast ll_env "array_init_void_ptr" (ll.Ptr init_llTy, ll.Id init_ptr_uid, ll.Ptr ll.I8)
- (*Make argument to build call looking like this:
- - % declare i8* @initArray (i64, i64, i8* ) oper_init_bitcast
- - void *initArray(long size, long elem_size, void* init)
- - Array layout: [S, elm0, elm1, ..., elmS-1]. Returning pointer
- to elm0, which means that the size S may be ignored---but it
- is available in case array bounds checking code is generated
- *)
- (* Allocate memory of size sizeof(ll_array) *)
- val callAlloc_insn = ll.Call(ll.Ptr ll.I8, ll.Gid (S.symbol "initArray"), [(ll.I64, operSize), (ll.I64, elemSize), (ll.Ptr (ll.I8), oper_init_ptr_cast)])
- val (oper_init_array_ptr, b_init_array_ptr) = emit_mk_PP ll_env ("array_ptr", callAlloc_insn)
- val b_ = seq_buildlets[
- b_init,
- b_size,
- b1, b2,
- b_alloca_init,
- b_store_init,
- b_init_ptr_cast,
- b_init_array_ptr
- ]
- in
- (oper_init_array_ptr, b_)
- end
Advertisement
Add Comment
Please, Sign In to add comment