# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.record panda.String <external>
.record IO <external>
.function void IO.printString(panda.String a0) <external>
.function void IO.printI64(i64 a0) <external>

# a0 - index in parameter list
.function i64 get(i64 a0, i64 a1, i64 a2, i64 a3) {
    lda.str "\n"
    sta.obj v0
    call.short IO.printString, v0, v0
    call.short IO.printI64, a0, a0
    call.short IO.printString, v0, v0
    call.short IO.printI64, a1, a1
    call.short IO.printString, v0, v0
    call.short IO.printI64, a2, a2
    call.short IO.printString, v0, v0
    call.short IO.printI64, a3, a3
    call.short IO.printString, v0, v0

    lda.64 a0
    jeqz zero

    subi 1
    jeqz first

    lda.64 a3
    return


first:
    lda.64 a2
    return

zero:
    lda.64 a1
    return
}


.function i32 main() {
    movi.64 v0, 0

    # 0x 3aaa aaaa aaaa aaa3
    movi.64 v1, 4227378850225105571
    # 0x 7333 3333 3333 3337
    movi.64 v2, 8301034833169298231
    # 0x 1fff ffff ffff fff1
    movi.64 v3, 2305843009213693937

    call get, v0, v1, v2, v3
    sub2.64 v1
    jnez print_fail_1

    movi.64 v0, 1
    call get, v0, v1, v2, v3
    sub2.64 v2
    jnez print_fail_2

    movi.64 v0, 2
    call get, v0, v1, v2, v3

    sub2.64 v3
    jnez print_fail_3
    return

print_fail_1:
    sta.64 v1
    lda.str "1-st paramer fail!"
    sta.obj v0
    call.short IO.printString, v0, v0
    lda.64 v1
    return.64

print_fail_2:
    sta.64 v1
    lda.str "2-d paramer fail!"
    sta.obj v0
    call.short IO.printString, v0, v0
    lda.64 v1
    return.64

print_fail_3:
    sta.64 v1
    lda.str "3-d paramer fail!"
    sta.obj v0
    call.short IO.printString, v0, v0
    lda.64 v1
    return.64
}