| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #if WASM_ENABLE_STRINGREF != 0
- #include "aot_emit_stringref.h"
- #include "aot_emit_exception.h"
- #include "aot_emit_memory.h"
- #include "aot_emit_gc.h"
- #include "aot.h"
- #include "aot_compiler.h"
- #include "aot_emit_memory.h"
- #include "gc_object.h"
- #include "string_object.h"
- #define BUILD_ISNULL(ptr, res, name) \
- do { \
- if (!(res = LLVMBuildIsNull(comp_ctx->builder, ptr, name))) { \
- aot_set_last_error("llvm build isnull failed."); \
- goto fail; \
- } \
- } while (0)
- #define BUILD_ISNOTNULL(ptr, res, name) \
- do { \
- if (!(res = LLVMBuildIsNotNull(comp_ctx->builder, ptr, name))) { \
- aot_set_last_error("llvm build isnotnull failed."); \
- goto fail; \
- } \
- } while (0)
- #define ADD_BASIC_BLOCK(block, name) \
- do { \
- if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
- func_ctx->func, name))) { \
- aot_set_last_error("llvm add basic block failed."); \
- goto fail; \
- } \
- } while (0)
- #define CURR_BLOCK() LLVMGetInsertBlock(comp_ctx->builder)
- #define MOVE_BLOCK_AFTER(llvm_block, llvm_block_after) \
- LLVMMoveBasicBlockAfter(llvm_block, llvm_block_after)
- #define MOVE_BLOCK_AFTER_CURR(llvm_block) \
- LLVMMoveBasicBlockAfter(llvm_block, CURR_BLOCK())
- #define DEFINE_STRINGREF_CHECK_VAR() \
- LLVMBasicBlockRef check_string_obj_succ, check_stringref_obj_succ; \
- LLVMValueRef cmp
- #define CHECK_STRING_OBJ(str_obj) \
- do { \
- ADD_BASIC_BLOCK(check_string_obj_succ, "check string obj succ"); \
- MOVE_BLOCK_AFTER_CURR(check_string_obj_succ); \
- \
- BUILD_ISNULL(str_obj, cmp, "cmp_string_obj"); \
- if (!aot_emit_exception(comp_ctx, func_ctx, \
- EXCE_FAILED_TO_CREATE_STRING, true, cmp, \
- check_string_obj_succ)) \
- goto fail; \
- } while (0)
- #define CHECK_STRINGREF_INTERNAL(stringref_obj, exce_id, name) \
- do { \
- ADD_BASIC_BLOCK(check_stringref_obj_succ, "check " name " obj succ"); \
- MOVE_BLOCK_AFTER(check_stringref_obj_succ, check_string_obj_succ); \
- \
- BUILD_ISNULL(stringref_obj, cmp, "cmp_" name "_obj"); \
- if (!aot_emit_exception(comp_ctx, func_ctx, exce_id, true, cmp, \
- check_stringref_obj_succ)) \
- goto fail; \
- } while (0)
- #define CHECK_STRINGREF_OBJ(stringref_obj) \
- CHECK_STRINGREF_INTERNAL(stringref_obj, EXCE_FAILED_TO_CREATE_STRINGREF, \
- "stringref")
- #define CHECK_STRINGVIEW_OBJ(stringview_obj) \
- CHECK_STRINGREF_INTERNAL(stringview_obj, EXCE_FAILED_TO_CREATE_STRINGVIEW, \
- "stringview")
- #define CHECK_STRING_ENCODE(value) \
- do { \
- ADD_BASIC_BLOCK(check_string_encode_succ, "check string encode succ"); \
- MOVE_BLOCK_AFTER_CURR(check_string_encode_succ); \
- \
- if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntSLT, value, \
- I32_ZERO, "cmp_string_encode"))) { \
- aot_set_last_error("llvm build icmp failed."); \
- goto fail; \
- } \
- \
- if (!aot_emit_exception(comp_ctx, func_ctx, \
- EXCE_FAILED_TO_ENCODE_STRING, true, cmp, \
- check_string_encode_succ)) \
- goto fail; \
- } while (0)
- static bool
- aot_call_wasm_stringref_obj_new(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, LLVMValueRef str_obj,
- uint32 stringref_type, uint32 pos,
- LLVMValueRef *stringref_obj)
- {
- LLVMValueRef param_values[3], func, value, res;
- LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
- uint32 argc = 2;
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = INT8_PTR_TYPE;
- param_types[2] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- if (stringref_type == WASM_TYPE_STRINGREF) {
- GET_AOT_FUNCTION(wasm_stringref_obj_new, argc);
- }
- else if (stringref_type == WASM_TYPE_STRINGVIEWWTF8) {
- GET_AOT_FUNCTION(wasm_stringview_wtf8_obj_new, argc);
- }
- else if (stringref_type == WASM_TYPE_STRINGVIEWWTF16) {
- GET_AOT_FUNCTION(wasm_stringview_wtf16_obj_new, argc);
- }
- else {
- argc = 3;
- GET_AOT_FUNCTION(wasm_stringview_iter_obj_new, argc);
- }
- param_values[0] = func_ctx->exec_env;
- param_values[1] = str_obj;
- if (stringref_type == WASM_TYPE_STRINGVIEWITER) {
- param_values[2] = I32_CONST(pos);
- }
- if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
- argc, "create_stringref"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- *stringref_obj = res;
- return true;
- fail:
- return false;
- }
- static LLVMValueRef
- aot_stringref_obj_get_value(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- LLVMValueRef stringref_obj)
- {
- LLVMValueRef str_obj_ptr, str_obj, host_ptr_offset;
- /* header */
- host_ptr_offset = I32_CONST(comp_ctx->pointer_size);
- if (!(stringref_obj =
- LLVMBuildBitCast(comp_ctx->builder, stringref_obj, INT8_PTR_TYPE,
- "stringref_obj_i8p"))) {
- aot_set_last_error("llvm build bitcast failed.");
- goto fail;
- }
- if (!(str_obj_ptr =
- LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, stringref_obj,
- &host_ptr_offset, 1, "str_obj_i8p"))) {
- aot_set_last_error("llvm build gep failed.");
- goto fail;
- }
- if (!(str_obj_ptr = LLVMBuildBitCast(comp_ctx->builder, str_obj_ptr,
- GC_REF_PTR_TYPE, "str_obj_gcref_p"))) {
- aot_set_last_error("llvm build bitcast failed.");
- goto fail;
- }
- if (!(str_obj = LLVMBuildLoad2(comp_ctx->builder, GC_REF_TYPE, str_obj_ptr,
- "str_obj"))) {
- aot_set_last_error("llvm build load failed.");
- goto fail;
- }
- LLVMSetAlignment(str_obj, 4);
- return str_obj;
- fail:
- return NULL;
- }
- static LLVMValueRef
- get_stringview_iter_pos_addr(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- LLVMValueRef stringview_iter_obj)
- {
- LLVMValueRef iter_pos_ptr, host_ptr_offset;
- /* header + str_obj */
- host_ptr_offset = I32_CONST(comp_ctx->pointer_size * 2);
- if (!(stringview_iter_obj =
- LLVMBuildBitCast(comp_ctx->builder, stringview_iter_obj,
- INT8_PTR_TYPE, "stringview_iter_obj_i8p"))) {
- aot_set_last_error("llvm build bitcast failed.");
- goto fail;
- }
- if (!(iter_pos_ptr = LLVMBuildInBoundsGEP2(
- comp_ctx->builder, INT8_TYPE, stringview_iter_obj,
- &host_ptr_offset, 1, "iter_pos_i8p"))) {
- aot_set_last_error("llvm build gep failed.");
- goto fail;
- }
- if (!(iter_pos_ptr = LLVMBuildBitCast(comp_ctx->builder, iter_pos_ptr,
- INT32_PTR_TYPE, "iter_pos_i32p"))) {
- aot_set_last_error("llvm build bitcast failed.");
- goto fail;
- }
- return iter_pos_ptr;
- fail:
- return NULL;
- }
- static LLVMValueRef
- aot_call_wasm_string_measure(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- LLVMValueRef stringref_obj, uint32 encoding)
- {
- LLVMValueRef param_values[3], func, value, str_obj;
- LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_measure, 2);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = I32_CONST(encoding);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "string_measure"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- return value;
- fail:
- return NULL;
- }
- static LLVMValueRef
- aot_call_wasm_string_create_view(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- LLVMValueRef stringref_obj, uint32 encoding)
- {
- LLVMValueRef param_values[3], func, value, str_obj;
- LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_create_view, 2);
- /* Call function wasm_string_create_view() */
- param_values[0] = str_obj;
- param_values[1] = I32_CONST(encoding);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "string_create_view"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- return value;
- fail:
- return NULL;
- }
- static LLVMValueRef
- aot_call_wasm_string_advance(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- LLVMValueRef stringref_obj, LLVMValueRef bytes,
- LLVMValueRef pos)
- {
- LLVMValueRef param_values[4], func, value, str_obj;
- LLVMTypeRef param_types[4], ret_type, func_type, func_ptr_type;
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT32_PTR_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_advance, 4);
- /* Call function wasm_string_advance() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- param_values[2] = bytes;
- param_values[3] = I8_PTR_NULL;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 4, "string_advance"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- return value;
- fail:
- return NULL;
- }
- static LLVMValueRef
- aot_call_wasm_string_slice(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- LLVMValueRef stringref_obj, LLVMValueRef start,
- LLVMValueRef end, StringViewType stringview_type)
- {
- LLVMValueRef param_values[4], func, value, str_obj;
- LLVMTypeRef param_types[4], ret_type, func_type, func_ptr_type;
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_slice, 4);
- /* Call function wasm_string_slice() */
- param_values[0] = str_obj;
- param_values[1] = start;
- param_values[2] = end;
- param_values[3] = I32_CONST(stringview_type);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 4, "string_slice"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- return value;
- fail:
- return NULL;
- }
- bool
- aot_compile_op_string_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- uint32 encoding)
- {
- LLVMValueRef maddr, byte_length, offset, str_obj, stringref_obj;
- LLVMValueRef param_values[5], func, value;
- LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_I32(byte_length);
- POP_I32(offset);
- if (!(maddr = check_bulk_memory_overflow(comp_ctx, func_ctx, offset,
- byte_length)))
- goto fail;
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_new_with_encoding, 3);
- /* Call function wasm_struct_obj_new() */
- param_values[0] = maddr;
- param_values[1] = byte_length;
- param_values[2] = I32_CONST(encoding);
- if (!(str_obj = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 3, "wasm_string_new"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj);
- PUSH_GC_REF(stringref_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- uint32 contents)
- {
- LLVMValueRef param_values[2], func, value, str_obj, stringref_obj;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_new_const, 2);
- bh_assert(contents < comp_ctx->comp_data->string_literal_count);
- param_values[0] = LLVMConstIntToPtr(
- I64_CONST((unsigned long long)(uintptr_t)
- comp_ctx->comp_data->string_literal_ptrs_wp[contents]),
- INT8_PTR_TYPE);
- param_values[1] =
- I32_CONST(comp_ctx->comp_data->string_literal_lengths_wp[contents]);
- if (!(str_obj = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "create_stringref"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj);
- PUSH_GC_REF(stringref_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_measure(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, uint32 encoding)
- {
- LLVMValueRef stringref_obj, value;
- POP_GC_REF(stringref_obj);
- if (!(value = aot_call_wasm_string_measure(comp_ctx, func_ctx,
- stringref_obj, encoding))) {
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_encode(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
- uint32 mem_idx, uint32 encoding)
- {
- LLVMValueRef param_values[6], func, value, offset, length, maddr, str_obj,
- stringref_obj;
- LLVMTypeRef param_types[6], ret_type, func_type, func_ptr_type;
- LLVMBasicBlockRef check_string_encode_succ;
- LLVMValueRef cmp;
- POP_I32(offset);
- POP_GC_REF(stringref_obj);
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- if (!(length = aot_call_wasm_string_measure(comp_ctx, func_ctx,
- stringref_obj, encoding))) {
- goto fail;
- }
- if (!(maddr =
- check_bulk_memory_overflow(comp_ctx, func_ctx, offset, length)))
- goto fail;
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT8_PTR_TYPE;
- param_types[4] = INT8_PTR_TYPE;
- param_types[5] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_encode, 6);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = I32_ZERO;
- param_values[2] = length;
- param_values[3] = maddr;
- param_values[4] = I8_PTR_NULL;
- param_values[5] = I32_CONST(encoding);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 6, "string_encode"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_ENCODE(value);
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_concat(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[2], func, value, str_obj_lhs, str_obj_rhs,
- stringref_obj_lhs, stringref_obj_rhs, stringref_obj_new;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_GC_REF(stringref_obj_rhs);
- POP_GC_REF(stringref_obj_lhs);
- if (!(str_obj_lhs = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringref_obj_lhs))) {
- goto fail;
- }
- if (!(str_obj_rhs = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringref_obj_rhs))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = INT8_PTR_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_concat, 2);
- /* Call function wasm_string_concat() */
- param_values[0] = str_obj_lhs;
- param_values[1] = str_obj_rhs;
- if (!(str_obj_lhs = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "string_concat"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj_lhs);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj_lhs,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj_new)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj_new);
- PUSH_GC_REF(stringref_obj_new);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_eq(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[2], func, value, str_obj_lhs, str_obj_rhs,
- stringref_obj_lhs, stringref_obj_rhs;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- POP_GC_REF(stringref_obj_lhs);
- POP_GC_REF(stringref_obj_rhs);
- if (!(str_obj_lhs = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringref_obj_lhs))) {
- goto fail;
- }
- if (!(str_obj_rhs = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringref_obj_rhs))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = INT8_PTR_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_eq, 2);
- /* Call function wasm_string_eq() */
- param_values[0] = str_obj_lhs;
- param_values[1] = str_obj_rhs;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "string_eq"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_is_usv_sequence(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[1], func, value, str_obj, stringref_obj;
- LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
- POP_GC_REF(stringref_obj);
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_is_usv_sequence, 1);
- /* Call function wasm_string_is_usv_sequence() */
- param_values[0] = str_obj;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 1, "string_is_usv_sequence"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_as_wtf8(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef str_obj, stringref_obj, stringview_wtf8_obj;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_GC_REF(stringref_obj);
- if (!(str_obj = aot_call_wasm_string_create_view(
- comp_ctx, func_ctx, stringref_obj, STRING_VIEW_WTF8))) {
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj,
- WASM_TYPE_STRINGVIEWWTF8, 0,
- &stringview_wtf8_obj)) {
- goto fail;
- }
- CHECK_STRINGVIEW_OBJ(stringref_obj);
- PUSH_GC_REF(stringview_wtf8_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf8_advance(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef stringref_obj, bytes, pos, value;
- POP_I32(bytes);
- POP_I32(pos);
- POP_GC_REF(stringref_obj);
- if (!(value = aot_call_wasm_string_advance(comp_ctx, func_ctx,
- stringref_obj, bytes, pos))) {
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf8_encode(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, uint32 mem_idx,
- uint32 encoding)
- {
- LLVMValueRef param_values[6], func, value, offset, maddr, str_obj,
- stringref_obj;
- LLVMValueRef bytes, pos, next_pos;
- LLVMTypeRef param_types[6], ret_type, func_type, func_ptr_type;
- LLVMBasicBlockRef check_string_encode_succ;
- LLVMValueRef cmp;
- POP_I32(bytes);
- POP_I32(pos);
- POP_I32(offset);
- next_pos = LLVMBuildAlloca(comp_ctx->builder, I32_TYPE, "next_pos");
- if (!next_pos) {
- aot_set_last_error("failed to build alloca");
- goto fail;
- }
- if (!(maddr =
- check_bulk_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
- goto fail;
- POP_GC_REF(stringref_obj);
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT8_PTR_TYPE;
- param_types[4] = INT8_PTR_TYPE;
- param_types[5] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_encode, 6);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- param_values[2] = bytes;
- param_values[3] = maddr;
- param_values[4] = next_pos;
- param_values[5] = I32_CONST(encoding);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 6, "string_encode"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_ENCODE(value);
- next_pos =
- LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, next_pos, "next_pos");
- if (!next_pos) {
- aot_set_last_error("llvm build load failed.");
- goto fail;
- }
- LLVMSetAlignment(next_pos, 4);
- PUSH_I32(next_pos);
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf8_slice(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef stringref_obj, start, end, stringref_obj_new, value;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_I32(start);
- POP_I32(end);
- POP_GC_REF(stringref_obj);
- if (!(value = aot_call_wasm_string_slice(comp_ctx, func_ctx, stringref_obj,
- start, end, STRING_VIEW_WTF8))) {
- goto fail;
- }
- CHECK_STRING_OBJ(value);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, value,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj_new)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj_new);
- PUSH_GC_REF(stringref_obj_new);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_as_wtf16(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef str_obj, stringref_obj, stringview_wtf16_obj;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_GC_REF(stringref_obj);
- if (!(str_obj = aot_call_wasm_string_create_view(
- comp_ctx, func_ctx, stringref_obj, STRING_VIEW_WTF16))) {
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj,
- WASM_TYPE_STRINGVIEWWTF16, 0,
- &stringview_wtf16_obj)) {
- goto fail;
- }
- CHECK_STRINGVIEW_OBJ(stringview_wtf16_obj);
- PUSH_GC_REF(stringview_wtf16_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf16_length(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[2], func, value, str_obj, stringview_wtf16_obj;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- POP_GC_REF(stringview_wtf16_obj);
- if (!(str_obj = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringview_wtf16_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_wtf16_get_length, 6);
- /* Call function wasm_string_wtf16_get_length() */
- param_values[0] = str_obj;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 1, "stringview_wtf16_length"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf16_get_codeunit(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[2], func, value, str_obj, stringview_wtf16_obj,
- pos;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- POP_I32(pos);
- POP_GC_REF(stringview_wtf16_obj);
- if (!(str_obj = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringview_wtf16_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_get_wtf16_codeunit, 2);
- /* Call function wasm_string_get_wtf16_codeunit() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- if (!(value =
- LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
- 2, "stringview_wtf16_get_codeunit"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf16_encode(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, uint32 mem_idx)
- {
- LLVMValueRef param_values[6], func, value, offset, maddr, str_obj,
- stringref_obj;
- LLVMValueRef len, pos;
- LLVMTypeRef param_types[6], ret_type, func_type, func_ptr_type;
- LLVMBasicBlockRef check_string_encode_succ;
- LLVMValueRef cmp;
- POP_I32(len);
- POP_I32(pos);
- POP_I32(offset);
- if (!(maddr = check_bulk_memory_overflow(
- comp_ctx, func_ctx, offset,
- LLVMBuildMul(comp_ctx->builder, len, I32_CONST(2), "wtf16_len"))))
- goto fail;
- POP_GC_REF(stringref_obj);
- if (!check_memory_alignment(comp_ctx, func_ctx, maddr, 2)) {
- goto fail;
- }
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT8_PTR_TYPE;
- param_types[4] = INT8_PTR_TYPE;
- param_types[5] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_encode, 6);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- param_values[2] = len;
- param_values[3] = maddr;
- param_values[4] = I8_PTR_NULL;
- param_values[5] = I32_CONST(WTF16);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 6, "string_encode"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_ENCODE(value);
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_wtf16_slice(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef stringref_obj, start, end, stringref_obj_new, value;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_I32(end);
- POP_I32(start);
- POP_GC_REF(stringref_obj);
- if (!(value = aot_call_wasm_string_slice(comp_ctx, func_ctx, stringref_obj,
- start, end, STRING_VIEW_WTF16))) {
- goto fail;
- }
- CHECK_STRING_OBJ(value);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, value,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj_new)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj_new);
- PUSH_GC_REF(stringref_obj_new);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_as_iter(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef stringref_obj, stringview_iter_obj, str_obj;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_GC_REF(stringref_obj);
- if (!(str_obj = aot_call_wasm_string_create_view(
- comp_ctx, func_ctx, stringref_obj, STRING_VIEW_WTF8))) {
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, stringref_obj,
- WASM_TYPE_STRINGVIEWITER, 0,
- &stringview_iter_obj)) {
- goto fail;
- }
- CHECK_STRINGVIEW_OBJ(stringview_iter_obj);
- PUSH_GC_REF(stringview_iter_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_iter_next(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef param_values[2], func, value, stringview_iter_obj, str_obj,
- iter_pos_addr, pos;
- LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
- POP_GC_REF(stringview_iter_obj);
- if (!(str_obj = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringview_iter_obj))) {
- goto fail;
- }
- if (!(iter_pos_addr = get_stringview_iter_pos_addr(comp_ctx, func_ctx,
- stringview_iter_obj))) {
- goto fail;
- }
- pos = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, iter_pos_addr,
- "get_iter_pos");
- LLVMSetAlignment(pos, 4);
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_next_codepoint, 2);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 2, "stringview_iter_next"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- static bool
- stringview_iter_advance_or_rewind(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, bool is_rewind)
- {
- LLVMValueRef param_values[4], func, value, stringview_iter_obj, str_obj,
- code_points_consumed, iter_pos_addr, pos, code_points_count, res;
- LLVMTypeRef param_types[4], ret_type, func_type, func_ptr_type;
- POP_I32(code_points_count);
- POP_GC_REF(stringview_iter_obj);
- if (!(str_obj = aot_stringref_obj_get_value(comp_ctx, func_ctx,
- stringview_iter_obj))) {
- goto fail;
- }
- if (!(iter_pos_addr = get_stringview_iter_pos_addr(comp_ctx, func_ctx,
- stringview_iter_obj))) {
- goto fail;
- }
- if (!(pos = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, iter_pos_addr,
- "get_iter_pos"))) {
- goto fail;
- }
- LLVMSetAlignment(pos, 4);
- if (!(code_points_consumed = LLVMBuildAlloca(comp_ctx->builder, I32_TYPE,
- "code_points_consumed"))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT32_PTR_TYPE;
- ret_type = I32_TYPE;
- if (is_rewind) {
- GET_AOT_FUNCTION(wasm_string_rewind, 4);
- }
- else {
- GET_AOT_FUNCTION(wasm_string_advance, 4);
- }
- /* Call function wasm_string_advance() */
- param_values[0] = str_obj;
- param_values[1] = pos;
- param_values[2] = code_points_count;
- param_values[3] = code_points_consumed;
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 4, "string_advance"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- if (!(code_points_consumed =
- LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, code_points_consumed,
- "get_code_points_consumed"))) {
- aot_set_last_error("llvm build load failed.");
- goto fail;
- }
- LLVMSetAlignment(code_points_consumed, 4);
- if (!(res = LLVMBuildStore(comp_ctx->builder, code_points_consumed,
- iter_pos_addr))) {
- aot_set_last_error("llvm build store failed.");
- goto fail;
- }
- LLVMSetAlignment(res, 4);
- PUSH_I32(code_points_consumed);
- fail:
- return false;
- }
- bool
- aot_compile_op_stringview_iter_advance(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- return stringview_iter_advance_or_rewind(comp_ctx, func_ctx, false);
- }
- bool
- aot_compile_op_stringview_iter_rewind(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- return stringview_iter_advance_or_rewind(comp_ctx, func_ctx, true);
- }
- bool
- aot_compile_op_stringview_iter_slice(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx)
- {
- LLVMValueRef stringview_iter_obj, start, end, stringref_obj_new, value,
- iter_pos_addr, code_points_count;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_I32(code_points_count);
- POP_GC_REF(stringview_iter_obj);
- if (!(iter_pos_addr = get_stringview_iter_pos_addr(comp_ctx, func_ctx,
- stringview_iter_obj))) {
- goto fail;
- }
- if (!(start = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, iter_pos_addr,
- "get_iter_pos"))) {
- goto fail;
- }
- LLVMSetAlignment(start, 4);
- if (!(end = LLVMBuildAdd(comp_ctx->builder, start, code_points_count,
- "calc_slice_end"))) {
- goto fail;
- }
- if (!(value = aot_call_wasm_string_slice(comp_ctx, func_ctx,
- stringview_iter_obj, start, end,
- STRING_VIEW_ITER))) {
- goto fail;
- }
- CHECK_STRING_OBJ(value);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, value,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj_new)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj_new);
- PUSH_GC_REF(stringref_obj_new);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_new_array(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, uint32 encoding)
- {
- LLVMValueRef start, end, count, str_obj, stringref_obj, array_obj,
- elem_data_ptr;
- LLVMValueRef param_values[5], func, value;
- LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
- DEFINE_STRINGREF_CHECK_VAR();
- if (!aot_gen_commit_values(comp_ctx->aot_frame))
- return false;
- if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
- return false;
- POP_I32(end);
- POP_I32(start);
- POP_GC_REF(array_obj);
- if (!aot_array_obj_elem_addr(
- comp_ctx, func_ctx, array_obj, start, &elem_data_ptr,
- encoding == WTF16 ? PACKED_TYPE_I16 : PACKED_TYPE_I8)) {
- goto fail;
- }
- if (!(count = LLVMBuildSub(comp_ctx->builder, end, start, "calc_count"))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- ret_type = INT8_PTR_TYPE;
- GET_AOT_FUNCTION(wasm_string_new_with_encoding, 3);
- /* Call function wasm_struct_obj_new() */
- param_values[0] = elem_data_ptr;
- param_values[1] = count;
- param_values[2] = I32_CONST(encoding);
- if (!(str_obj = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 3, "wasm_string_new"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_OBJ(str_obj);
- if (!aot_call_wasm_stringref_obj_new(comp_ctx, func_ctx, str_obj,
- WASM_TYPE_STRINGREF, 0,
- &stringref_obj)) {
- goto fail;
- }
- CHECK_STRINGREF_OBJ(stringref_obj);
- PUSH_GC_REF(stringref_obj);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_op_string_encode_array(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx, uint32 encoding)
- {
- LLVMValueRef param_values[6], func, value, count, start, str_obj,
- stringref_obj, array_obj, elem_data_ptr, array_len;
- LLVMTypeRef param_types[6], ret_type, func_type, func_ptr_type;
- LLVMBasicBlockRef check_string_encode_succ, check_array_index_succ;
- LLVMValueRef cmp;
- POP_I32(start);
- POP_GC_REF(array_obj);
- POP_GC_REF(stringref_obj);
- if (!(str_obj =
- aot_stringref_obj_get_value(comp_ctx, func_ctx, stringref_obj))) {
- goto fail;
- }
- if (!aot_array_obj_length(comp_ctx, array_obj, &array_len))
- goto fail;
- if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGE, start, array_len,
- "check_array_index"))) {
- aot_set_last_error("llvm build icmp failed.");
- goto fail;
- }
- ADD_BASIC_BLOCK(check_array_index_succ, "check array index succ");
- MOVE_BLOCK_AFTER_CURR(check_array_index_succ);
- if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true, cmp,
- check_array_index_succ)) {
- goto fail;
- }
- if (!aot_array_obj_elem_addr(
- comp_ctx, func_ctx, stringref_obj, start, &elem_data_ptr,
- encoding == WTF16 ? PACKED_TYPE_I16 : PACKED_TYPE_I8)) {
- goto fail;
- }
- if (!(count = aot_call_wasm_string_measure(comp_ctx, func_ctx,
- stringref_obj, encoding))) {
- goto fail;
- }
- param_types[0] = INT8_PTR_TYPE;
- param_types[1] = I32_TYPE;
- param_types[2] = I32_TYPE;
- param_types[3] = INT8_PTR_TYPE;
- param_types[4] = INT8_PTR_TYPE;
- param_types[5] = I32_TYPE;
- ret_type = I32_TYPE;
- GET_AOT_FUNCTION(wasm_string_encode, 6);
- /* Call function wasm_string_measure() */
- param_values[0] = str_obj;
- param_values[1] = start;
- param_values[2] = count;
- param_values[3] = elem_data_ptr;
- param_values[4] = I8_PTR_NULL;
- param_values[5] = I32_CONST(encoding);
- if (!(value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
- param_values, 6, "string_encode"))) {
- aot_set_last_error("llvm build call failed.");
- goto fail;
- }
- CHECK_STRING_ENCODE(value);
- PUSH_I32(value);
- return true;
- fail:
- return false;
- }
- #endif /* WASM_ENABLE_STRINGREF != 0 */
|