1# Copyright (c) 2021 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.record TreeNode { 15 TreeNode left 16 TreeNode right 17 i32 item 18} 19 20.function void TreeNode.Construct(TreeNode a0, TreeNode a1, TreeNode a2, i32 a3) { 21 lda.obj a1 22 stobj.obj a0, TreeNode.left 23 lda.obj a2 24 stobj.obj a0, TreeNode.right 25 lda a3 26 stobj a0, TreeNode.item 27 return.void 28} 29 30.function i32 TreeNode.itemCheck(TreeNode a0) <static> { 31 mov.null v0 32 ldobj.obj a0, TreeNode.left 33 jeq.obj v0, if 34 ldobj.obj a0, TreeNode.left 35 sta.obj v1 36 call.short TreeNode.itemCheck, v1 37 sta v2 38 ldobj.obj a0, TreeNode.right 39 sta.obj v1 40 call.short TreeNode.itemCheck, v1 41 neg 42 add2 v2 43 sta v2 44 ldobj a0, TreeNode.item 45 add2 v2 46 return 47if: 48 ldobj a0, TreeNode.item 49 return 50} 51 52.function TreeNode bottomUpTree(i32 a0, i32 a1){ 53 lda a1 54 jgtz if 55 newobj v0, TreeNode 56 lda a0 57 stobj v0, TreeNode.item 58 lda.obj v0 59 return.obj 60if: 61 lda a0 62 muli 2 63 subi 1 64 sta v2 65 lda a1 66 subi 1 67 sta v3 68 call.short bottomUpTree, v2, v3 69 sta.obj v4 70 lda a0 71 muli 2 72 sta v2 73 lda a1 74 subi 1 75 sta v3 76 call.short bottomUpTree, v2, v3 77 sta.obj v5 78 newobj v6, TreeNode 79 call TreeNode.Construct, v6, v4, v5, a0 80 lda.obj v6 81 return.obj 82} 83 84.function i32 max(i32 a0, i32 a1){ 85 lda a0 86 jlt a1, label 87 return 88label: 89 lda a1 90 return 91} 92 93.function u1 test(i32 a0, i32 a1, i32 a2){ 94 movi v25, 0 #ret 95 mov v1, a0 #loop_counter 96 movi v5, 0 97loop: 98 lda v1 99 jgt a1, loop_exit 100 mov v2, a0 #minDepth 101 lda v2 102 addi 2 103 sta v0 104 call.short max, v0, v1 105 sta v3 #maxDepth 106 addi 1 107 sta v4 #stretchDepth 108 call.short bottomUpTree, v5, v4 109 sta.obj v6 110 call.short TreeNode.itemCheck, v6 111 sta v7 #check 112 call.short bottomUpTree, v5, v3 113 sta.obj v11 #LongLivedTree 114 mov v8, v2 #depth 115loop2: 116 lda v8 117 jgt v3, loop2_exit 118 lda v3 119 sub2 v8 120 add2 v2 121 sta v9 122 ldai 1 123 shl2 v9 124 sta v9 #iterations 125 movi v7, 0 126 movi v10, 1 127loop3: 128 lda v10 129 jgt v9, loop3_exit 130 call.short bottomUpTree, v10, v8 131 sta.obj v12 132 call.short TreeNode.itemCheck, v12 133 add2 v7 134 sta v7 135 lda v10 136 neg 137 sta v0 138 call.short bottomUpTree, v0, v8 139 sta.obj v12 140 call.short TreeNode.itemCheck, v12 141 add2 v7 142 sta v7 143 inci v10, 1 144 jmp loop3 145loop3_exit: 146 inci v8, 2 147 jmp loop2 148loop2_exit: 149 lda.obj v11 150 call.short TreeNode.itemCheck, v11 151 add2 v25 152 sta v25 153 inci v1, 1 154 jmp loop 155loop_exit: 156 lda v25 157 jne a2, exit_failure 158 ldai 0 159 return 160exit_failure: 161 ldai 1 162 return 163} 164 165.function u1 main(){ 166 movi v0, 4 167 movi v1, 15 168 movi v2, -12 169 call test, v0, v1, v2 170 return 171} 172