Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
2
Indexable
Never
| Scan (binop, acc_exp, arr_exp, tp, pos) ->
    let arr_reg = newReg "arr"
    let size_reg = newReg "size"
    let i_reg = newReg "ind_var"
    let addr_reg = newReg "addr"
    let acc_reg = newReg "acc"
    let tmp_reg = newReg "tmp"
    let loop_beg = newLab "loop_beg"
    let loop_end = newLab "loop_end"

    let arr_code = compileExp arr_exp vtable arr_reg
    let header1 = [ LW(size_reg, arr_reg, 0)]

    let acc_code = compileExp acc_exp vtable acc_reg

    let init_regs = [ADDI (addr_reg, place, 4)]


    let loop_code = 
            [ADDI (arr_reg, arr_reg, 4)
            ; MV (i_reg, Rzero)
            ; Label (loop_beg)
            ; BGE (i_reg, size_reg, loop_end)
            ]

    let elem_size = getElemSize tp
    let load_code =
        [ Load elem_size (tmp_reg, arr_reg, 0)
        ; ADDI (arr_reg, arr_reg, elemSizeToInt elem_size)
        ]

    let apply_code =
        applyFunArg(binop, [acc_reg; tmp_reg], vtable, acc_reg, pos)
    
    let store_code =
        [store elem_size (acc_reg, addr_reg, 0)
        ; ADDI (addr_reg, addr_reg, elemSizeToInt elem_size)
        ]

    arr_code
    @ header1
    @ dynalloc (size_reg, place, tp)
    @ init_regs
    @ acc_code
    @ loop_code
    @ load_code
    @ apply_code
    @ store_code
    @ [ADDI(i_reg, i_reg, 1)
        ; J loop_beg
        ; LABEL loop_end
        ]