DatStorm

Untitled

Nov 7th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1.  
  2. and handleArrayExp(ll_env, cg_ctxt, size, init, ty) =
  3. let
  4. val _ = print("HandleArrayExp called. \n")
  5.  
  6. (************ FROM PDF ********
  7. Array allocation happens via another runtime function,
  8. called initArray. Similarly to allocRecord, it returns
  9. the pointer to the allocated memory. Indexing into
  10. arrays happens via GEP.
  11. *)
  12.  
  13. (*evalute the sub exps size and init*)
  14. val (operInit, b_init) = cgExp ll_env cg_ctxt init
  15. val init_llTy = ty_to_llty ((#ty o #aug) init )
  16. val (operSize, b_size) = cgExp ll_env cg_ctxt size
  17. val size_llTy = ty_to_llty ((#ty o #aug) size )
  18.  
  19. (* Determine the size of each element in the array !!!*)
  20. (* Get a pointer that points to address sizeof(init_llty) *)
  21. val (oper_elem_size, b1) = build_gep ll_env "array_size_ptr" (init_llTy, ll.Null, [ll.Const 1])
  22.  
  23. (* Get the addr of the pointer as an int. We now have sizeof(ll_array) *)
  24. val elemSizePtrToInt_insn = ll.Ptrtoint(init_llTy, oper_elem_size, ll.I64)
  25. val (elemSize, b2) = emit_mk_PP ll_env ("array_size", elemSizePtrToInt_insn)
  26.  
  27. (* To fill the array with the init value, we must supply initArray() with a void* that points to an adress with the value *)
  28. (* Make a pointer, *)
  29. val init_ptr_uid = mk_uid ll_env "array_init_void_ptr"
  30. val b_alloca_init = emit_alloca (init_ptr_uid, init_llTy)
  31.  
  32. (* that points to the init, of type void* *)
  33. val b_store_init = build_store (init_llTy, operInit, ll.Id init_ptr_uid)
  34. 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)
  35.  
  36.  
  37. (*Make argument to build call looking like this:
  38. - % declare i8* @initArray (i64, i64, i8* ) oper_init_bitcast
  39. - void *initArray(long size, long elem_size, void* init)
  40. - Array layout: [S, elm0, elm1, ..., elmS-1]. Returning pointer
  41. to elm0, which means that the size S may be ignored---but it
  42. is available in case array bounds checking code is generated
  43. *)
  44. (* Allocate memory of size sizeof(ll_array) *)
  45. 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)])
  46. val (oper_init_array_ptr, b_init_array_ptr) = emit_mk_PP ll_env ("array_ptr", callAlloc_insn)
  47.  
  48.  
  49. val b_ = seq_buildlets[
  50. b_init,
  51. b_size,
  52. b1, b2,
  53. b_alloca_init,
  54. b_store_init,
  55. b_init_ptr_cast,
  56. b_init_array_ptr
  57. ]
  58. in
  59. (oper_init_array_ptr, b_)
  60. end
Advertisement
Add Comment
Please, Sign In to add comment