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##include <iostream>
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#struct Obj {
17b1994897Sopenharmony_ci#    int data;
18b1994897Sopenharmony_ci#    Obj* next;
19b1994897Sopenharmony_ci#};
20b1994897Sopenharmony_ci#
21b1994897Sopenharmony_ci#void addNext(Obj* o, int data) {
22b1994897Sopenharmony_ci#    Obj* newObj = new Obj();
23b1994897Sopenharmony_ci#    newObj->data = data;
24b1994897Sopenharmony_ci#    o->next = newObj;
25b1994897Sopenharmony_ci#}
26b1994897Sopenharmony_ci
27b1994897Sopenharmony_ci
28b1994897Sopenharmony_ci#int main() {
29b1994897Sopenharmony_ci
30b1994897Sopenharmony_ci#    Obj* head = new Obj();
31b1994897Sopenharmony_ci#    head->data = -1;
32b1994897Sopenharmony_ci#    head->next = nullptr;
33b1994897Sopenharmony_ci#    Obj* cur;
34b1994897Sopenharmony_ci#    cur = head;
35b1994897Sopenharmony_ci#    for (int i = 0; i < 100000; i++) {
36b1994897Sopenharmony_ci#        addNext(cur, i);
37b1994897Sopenharmony_ci#        cur = cur->next;
38b1994897Sopenharmony_ci#    }
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci#    int ret = 0;
41b1994897Sopenharmony_ci#    cur = head;
42b1994897Sopenharmony_ci#    for (int i = 0; i < 100000; i++) {
43b1994897Sopenharmony_ci#        ret += cur->data;
44b1994897Sopenharmony_ci#        cur = cur->next;
45b1994897Sopenharmony_ci#    }
46b1994897Sopenharmony_ci#    std::cout << ret;
47b1994897Sopenharmony_ci#    if(ret != 4999850000) return 1;
48b1994897Sopenharmony_ci#    return 0;
49b1994897Sopenharmony_ci#}
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_ci.record IO <external>
52b1994897Sopenharmony_ci.record Obj {
53b1994897Sopenharmony_ci    i32 data
54b1994897Sopenharmony_ci    Obj next
55b1994897Sopenharmony_ci}
56b1994897Sopenharmony_ci.function void IO.printI64(i64 a0) <external>
57b1994897Sopenharmony_ci.function void addNext(Obj a0, i32 a1) {
58b1994897Sopenharmony_ci    newobj v0, Obj
59b1994897Sopenharmony_ci    lda a1          #data
60b1994897Sopenharmony_ci    stobj v0, Obj.data
61b1994897Sopenharmony_ci    lda.obj v0
62b1994897Sopenharmony_ci    stobj.obj a0, Obj.next
63b1994897Sopenharmony_ci    return.void
64b1994897Sopenharmony_ci}
65b1994897Sopenharmony_ci
66b1994897Sopenharmony_ci.function i32 main() {
67b1994897Sopenharmony_ci    movi v10, 100000
68b1994897Sopenharmony_ci    movi.64 v15, 4999850000
69b1994897Sopenharmony_ci    newobj v0, Obj
70b1994897Sopenharmony_ci    ldai -1
71b1994897Sopenharmony_ci    stobj v0, Obj.data
72b1994897Sopenharmony_ci    mov.obj v1, v0     #curr
73b1994897Sopenharmony_ci    movi v9, 0
74b1994897Sopenharmony_ci    ldai 0
75b1994897Sopenharmony_ciloop:
76b1994897Sopenharmony_ci    jeq v10, loop_exit
77b1994897Sopenharmony_ci    call.short addNext, v1, v9
78b1994897Sopenharmony_ci    ldobj.obj v1, Obj.next
79b1994897Sopenharmony_ci    sta.obj v1
80b1994897Sopenharmony_ci    inci v9, 1
81b1994897Sopenharmony_ci    lda v9
82b1994897Sopenharmony_ci    jmp loop
83b1994897Sopenharmony_ciloop_exit:
84b1994897Sopenharmony_ci    movi.64 v2, 0       #ret
85b1994897Sopenharmony_ci    mov.obj v1, v0
86b1994897Sopenharmony_ci    movi v9, 0
87b1994897Sopenharmony_ci    ldai 0
88b1994897Sopenharmony_ciloop2:
89b1994897Sopenharmony_ci    jeq v10, loop2_exit
90b1994897Sopenharmony_ci    ldobj v1, Obj.data
91b1994897Sopenharmony_ci    i32toi64
92b1994897Sopenharmony_ci    add2.64 v2
93b1994897Sopenharmony_ci    sta.64 v2
94b1994897Sopenharmony_ci    ldobj.obj v1, Obj.next
95b1994897Sopenharmony_ci    sta.obj v1
96b1994897Sopenharmony_ci    inci v9, 1
97b1994897Sopenharmony_ci    lda v9
98b1994897Sopenharmony_ci    jmp loop2
99b1994897Sopenharmony_ciloop2_exit:
100b1994897Sopenharmony_ci    call.short IO.printI64, v2
101b1994897Sopenharmony_ci    lda.64 v2
102b1994897Sopenharmony_ci    cmp.64 v15
103b1994897Sopenharmony_ci    jnez exit_failure
104b1994897Sopenharmony_ci    ldai 0
105b1994897Sopenharmony_ci    return
106b1994897Sopenharmony_ciexit_failure:
107b1994897Sopenharmony_ci    ldai 1
108b1994897Sopenharmony_ci    return
109b1994897Sopenharmony_ci}
110