Skip to content

Commit

Permalink
Add Bytes.create
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Jan 5, 2019
1 parent c5e7c62 commit 1d60e08
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,12 @@ let rec generate (letfuncs, strings, typedefs, exps) =
appfmt buf "mov rax, %d" @@ tagged_int 0 ;
appstr buf "ret" ;
appstr buf "" ;
appstr buf "aqaml_string_create:" ;
appstr buf @@ untag_int "rax" ;
appstr buf "mov rdi, rax" ;
appstr buf "call aqaml_string_create_detail@PLT" ;
appstr buf "ret" ;
appstr buf "" ;
appstr buf "aqaml_string_blit:" ;
appstr buf @@ untag_int "rbx" ;
appstr buf @@ untag_int "rsi" ;
Expand Down
4 changes: 4 additions & 0 deletions stdlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module Bytes = struct

external set : bytes -> int -> char -> unit = "aqaml_string_set"

external create : int -> bytes = "aqaml_string_create"

external blit :
bytes -> int -> bytes -> int -> int -> unit
= "aqaml_string_blit"
Expand All @@ -39,6 +41,8 @@ module String = struct

external set : string -> int -> char -> unit = "aqaml_string_set"

external create : int -> bytes = "aqaml_string_create"

external blit :
string -> int -> bytes -> int -> int -> unit
= "aqaml_string_blit"
Expand Down
8 changes: 8 additions & 0 deletions test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1517,3 +1517,11 @@ let dst = Bytes.of_string "def " in
String.blit src 1 dst 2 3 ;
test src "abcd" ;
test dst @@ Bytes.of_string "debcd"

;;
let src = "abcd" in
let dst = Bytes.create 5 in
dst.[0] <- 'd' ;
dst.[1] <- 'e' ;
String.blit src 1 dst 2 3 ;
test dst @@ Bytes.of_string "debcd"
17 changes: 12 additions & 5 deletions utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct AQamlValue {
} AQamlValue;

uint64_t aqaml_string_length_detail(uint64_t ptr);
uint64_t aqaml_string_create_detail(uint64_t len);

void *aqaml_malloc_detail(uint32_t size)
{
Expand Down Expand Up @@ -103,17 +104,13 @@ uint64_t aqaml_concat_string_detail(uint64_t lhs_src, uint64_t rhs_src)
AQamlValue lhs = get_value(lhs_src), rhs = get_value(rhs_src);
// assert(lhs.kind == AQAML_STRING && rhs.kind == AQAML_STRING);

uint64_t ret_src = aqaml_alloc_block((lhs_len + rhs_len) / 8 + 1, 0, 252);
uint64_t ret_src = aqaml_string_create_detail(lhs_len + rhs_len);
AQamlValue ret = get_value(ret_src);
uint64_t space = 7 - (lhs_len + rhs_len) % 8;

for (uint64_t i = 0; i < lhs_len; i++)
ret.string->str[i] = lhs.string->str[i];
for (uint64_t i = 0; i < rhs_len; i++)
ret.string->str[i + lhs_len] = rhs.string->str[i];
for (uint64_t i = 0; i < space; i++)
ret.string->str[i + lhs_len + rhs_len] = 0u;
ret.string->str[lhs_len + rhs_len + space] = space;

return ret_src;
}
Expand Down Expand Up @@ -145,6 +142,16 @@ void aqaml_string_set_detail(uint64_t str_src, uint64_t index, uint64_t chr)
val.string->str[index] = (uint8_t)chr;
}

uint64_t aqaml_string_create_detail(uint64_t len)
{
uint64_t ret_src = aqaml_alloc_block(len / 8 + 1, 0, 252);
uint64_t space = 7 - len % 8;
AQamlValue ret = get_value(ret_src);
for (uint64_t i = 0; i < space; i++) ret.string->str[len + i] = 0;
ret.string->str[len + space] = space;
return ret_src;
}

void aqaml_string_blit_detail(uint64_t src_src, uint64_t srcoff,
uint64_t dst_src, uint64_t dstoff, uint64_t len)
{
Expand Down

0 comments on commit 1d60e08

Please sign in to comment.