1b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
3b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
4b1994897Sopenharmony_ci# You may obtain a copy of the License at
5b1994897Sopenharmony_ci#
6b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
7b1994897Sopenharmony_ci#
8b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
9b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
10b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
12b1994897Sopenharmony_ci# limitations under the License.
13b1994897Sopenharmony_ci
14b1994897Sopenharmony_ci.record panda.String <external>
15b1994897Sopenharmony_ci.record IO <external>
16b1994897Sopenharmony_ci.function void IO.printString(panda.String a0) <external>
17b1994897Sopenharmony_ci.function void IO.printI32(i32 a0) <external>
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci# void do() {
20b1994897Sopenharmony_ci#    String[] v6 = new String[5];
21b1994897Sopenharmony_ci#    for(int i = 0; i < 5; i++) {
22b1994897Sopenharmony_ci#       String[] s = allocString();
23b1994897Sopenharmony_ci#       v6[i] = s[0];
24b1994897Sopenharmony_ci#   }
25b1994897Sopenharmony_ci#   for(int i = 0; i < 5; i++) {
26b1994897Sopenharmony_ci#       // we expect that v6[i] is still reachable
27b1994897Sopenharmony_ci#       printString(v6[i]);
28b1994897Sopenharmony_ci#   }
29b1994897Sopenharmony_ci# }
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ci# String[] allocString() {
32b1994897Sopenharmony_ci#   String[] v1 = new String[1];
33b1994897Sopenharmony_ci#   v1[0] = "asdf"
34b1994897Sopenharmony_ci#   return v1
35b1994897Sopenharmony_ci#}
36b1994897Sopenharmony_ci
37b1994897Sopenharmony_ci.function i32 main(){
38b1994897Sopenharmony_ci    call.short do
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci    # safe-point for trigger GC in the end
41b1994897Sopenharmony_ci    lda.str "finish test\n"
42b1994897Sopenharmony_ci    sta.obj v2
43b1994897Sopenharmony_ci    call.short IO.printString, v2, v2
44b1994897Sopenharmony_ci
45b1994897Sopenharmony_ci    ldai 0
46b1994897Sopenharmony_ci    return
47b1994897Sopenharmony_ci}
48b1994897Sopenharmony_ci
49b1994897Sopenharmony_ci# String[] v1 = new String[1];
50b1994897Sopenharmony_ci# v1[0] = "asdf"
51b1994897Sopenharmony_ci# return v1
52b1994897Sopenharmony_ci.function panda.String[] allocString(){
53b1994897Sopenharmony_ci    lda.str "asdf\n"
54b1994897Sopenharmony_ci    sta.obj v2
55b1994897Sopenharmony_ci
56b1994897Sopenharmony_ci    movi v0, 1
57b1994897Sopenharmony_ci    # v1 = new String[v0]
58b1994897Sopenharmony_ci    newarr v1, v0, panda.String[]
59b1994897Sopenharmony_ci
60b1994897Sopenharmony_ci    # v1[v3] = v2
61b1994897Sopenharmony_ci    lda.obj v2
62b1994897Sopenharmony_ci    movi v3, 0
63b1994897Sopenharmony_ci    starr.obj v1, v3
64b1994897Sopenharmony_ci
65b1994897Sopenharmony_ci    lda.obj v1
66b1994897Sopenharmony_ci    return.obj
67b1994897Sopenharmony_ci}
68b1994897Sopenharmony_ci
69b1994897Sopenharmony_ci.function void do (){
70b1994897Sopenharmony_ci    movi v0, 5
71b1994897Sopenharmony_ci
72b1994897Sopenharmony_ci    # v6 = new String[v0]
73b1994897Sopenharmony_ci    newarr v6, v0, panda.String[]
74b1994897Sopenharmony_ci
75b1994897Sopenharmony_ci    # (for int v2=0; v2 != v0; v2++)
76b1994897Sopenharmony_ci    movi v2, 0
77b1994897Sopenharmony_ci    ldai 0
78b1994897Sopenharmony_ci    loop:
79b1994897Sopenharmony_ci        jeq v0, loop_exit
80b1994897Sopenharmony_ci
81b1994897Sopenharmony_ci
82b1994897Sopenharmony_ci        # v5=allocString()[0]  + print v5
83b1994897Sopenharmony_ci        call.short allocString
84b1994897Sopenharmony_ci        sta.obj v3
85b1994897Sopenharmony_ci
86b1994897Sopenharmony_ci        movi v4, 0
87b1994897Sopenharmony_ci        lda v4
88b1994897Sopenharmony_ci        ldarr.obj v3
89b1994897Sopenharmony_ci        sta.obj v5
90b1994897Sopenharmony_ci        #call.short IO.printString, v5, v5
91b1994897Sopenharmony_ci
92b1994897Sopenharmony_ci        # v6[v2] = v5
93b1994897Sopenharmony_ci        lda.obj v5
94b1994897Sopenharmony_ci        starr.obj v6, v2
95b1994897Sopenharmony_ci
96b1994897Sopenharmony_ci        inci v2, 1
97b1994897Sopenharmony_ci        lda v2
98b1994897Sopenharmony_ci        jmp loop
99b1994897Sopenharmony_ci    loop_exit:
100b1994897Sopenharmony_ci        subi 1
101b1994897Sopenharmony_ci        sta v2
102b1994897Sopenharmony_ci
103b1994897Sopenharmony_ci
104b1994897Sopenharmony_ci    movi v0, 5
105b1994897Sopenharmony_ci    movi v2, 0
106b1994897Sopenharmony_ci    ldai 0
107b1994897Sopenharmony_ci    loop2:
108b1994897Sopenharmony_ci        jeq v0, loop2_exit
109b1994897Sopenharmony_ci
110b1994897Sopenharmony_ci        movi v4, 0
111b1994897Sopenharmony_ci        lda v4
112b1994897Sopenharmony_ci        ldarr.obj v6
113b1994897Sopenharmony_ci        sta.obj v5
114b1994897Sopenharmony_ci        call.short IO.printString, v5, v5
115b1994897Sopenharmony_ci
116b1994897Sopenharmony_ci        inci v2,1
117b1994897Sopenharmony_ci        lda v2
118b1994897Sopenharmony_ci        jmp loop2
119b1994897Sopenharmony_ci    loop2_exit:
120b1994897Sopenharmony_ci        subi 1
121b1994897Sopenharmony_ci        sta v2
122b1994897Sopenharmony_ci    return.void
123b1994897Sopenharmony_ci}
124