1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14##include <iostream> 15 16#struct Obj { 17# int data; 18# Obj* next; 19#}; 20# 21#void addNext(Obj* o, int data) { 22# Obj* newObj = new Obj(); 23# newObj->data = data; 24# o->next = newObj; 25#} 26 27 28#int main() { 29 30# Obj* head = new Obj(); 31# head->data = -1; 32# head->next = nullptr; 33# Obj* cur; 34# cur = head; 35# for (int i = 0; i < 100000; i++) { 36# addNext(cur, i); 37# cur = cur->next; 38# } 39 40# int ret = 0; 41# cur = head; 42# for (int i = 0; i < 100000; i++) { 43# ret += cur->data; 44# cur = cur->next; 45# } 46# std::cout << ret; 47# if(ret != 4999850000) return 1; 48# return 0; 49#} 50 51.record IO <external> 52.record Obj { 53 i32 data 54 Obj next 55} 56.function void IO.printI64(i64 a0) <external> 57.function void addNext(Obj a0, i32 a1) { 58 newobj v0, Obj 59 lda a1 #data 60 stobj v0, Obj.data 61 lda.obj v0 62 stobj.obj a0, Obj.next 63 return.void 64} 65 66.function i32 main() { 67 movi v10, 100000 68 movi.64 v15, 4999850000 69 newobj v0, Obj 70 ldai -1 71 stobj v0, Obj.data 72 mov.obj v1, v0 #curr 73 movi v9, 0 74 ldai 0 75loop: 76 jeq v10, loop_exit 77 call.short addNext, v1, v9 78 ldobj.obj v1, Obj.next 79 sta.obj v1 80 inci v9, 1 81 lda v9 82 jmp loop 83loop_exit: 84 movi.64 v2, 0 #ret 85 mov.obj v1, v0 86 movi v9, 0 87 ldai 0 88loop2: 89 jeq v10, loop2_exit 90 ldobj v1, Obj.data 91 i32toi64 92 add2.64 v2 93 sta.64 v2 94 ldobj.obj v1, Obj.next 95 sta.obj v1 96 inci v9, 1 97 lda v9 98 jmp loop2 99loop2_exit: 100 call.short IO.printI64, v2 101 lda.64 v2 102 cmp.64 v15 103 jnez exit_failure 104 ldai 0 105 return 106exit_failure: 107 ldai 1 108 return 109} 110