Exemplo n.º 1
0
def main_page(request):
    global gdata
    last_instr = ""
    error = ""

    site_hdr = get_hdr()

    if request.method == 'GET':
        gdata.re_init()
        form = MainForm()
    else:
        form = MainForm(request.POST)
        if CLEAR in request.POST:
            gdata.re_init()
        else:
            step = (STEP in request.POST)
            gdata.nxt_key = 0
            if step:
                add_debug("Getting next key", gdata)
                try:
                    gdata.nxt_key = int(request.POST.get(NXT_KEY, 0))
                except Exception:
                    gdata.nxt_key = 0

            get_reg_contents(gdata.registers, request)
            get_mem_contents(gdata.memory, request)
            get_stack_contents(gdata.stack, request)
            get_flag_contents(gdata.flags, request)
            (last_instr, error) = assemble(request.POST[CODE], gdata, step)

    return render(
        request, 'main.html', {
            'form': form,
            HEADER: site_hdr,
            'last_instr': last_instr,
            'error': error,
            'unwritable': gdata.unwritable,
            'debug': gdata.debug,
            NXT_KEY: gdata.nxt_key,
            'registers': gdata.registers,
            'memory': gdata.memory,
            'stack': gdata.stack,
            'flags': gdata.flags,
        })
Exemplo n.º 2
0
def main_page(request):
    last_instr = ""
    error = ""

    site_hdr = get_hdr()
    if request.method == 'GET':
        if intel_machine.flavor == None and mips_machine.flavor == None:
            return render(request, 'main_error.html', {HEADER: site_hdr})
        intel_machine.re_init()
        mips_machine.re_init()
        form = MainForm()
    else:
        if 'language' in request.POST:
            intel_machine.re_init()
            mips_machine.re_init()
            form = MainForm()
            lang = request.POST['language']
            print(lang)
            if lang == "mips":
                intel_machine.flavor = None
                mips_machine.flavor = "mips"
                return render(
                    request, 'main.html', {
                        'form': form,
                        HEADER: site_hdr,
                        'last_instr': "",
                        'error': "",
                        'unwritable': mips_machine.unwritable,
                        'debug': mips_machine.debug,
                        NXT_KEY: mips_machine.nxt_key,
                        'registers': mips_machine.registers,
                        'memory': mips_machine.memory,
                        'stack': mips_machine.stack,
                        'flags': mips_machine.flags,
                        'flavor': mips_machine.flavor
                    })
            else:
                mips_machine.flavor = None
                if lang == "att":
                    intel_machine.flavor = "att"
                else:
                    intel_machine.flavor = "intel"
                return render(
                    request, 'main.html', {
                        'form': form,
                        HEADER: site_hdr,
                        'last_instr': "",
                        'error': "",
                        'unwritable': intel_machine.unwritable,
                        'debug': intel_machine.debug,
                        NXT_KEY: intel_machine.nxt_key,
                        'registers': intel_machine.registers,
                        'memory': intel_machine.memory,
                        'stack': intel_machine.stack,
                        'flags': intel_machine.flags,
                        'flavor': intel_machine.flavor
                    })
        form = MainForm(request.POST)
        if 'flavor' in request.POST:
            language = request.POST['flavor']
            if language == "intel" or language == "att":
                intel_machine.flavor = language
                mips_machine.flavor = None
            elif language == "mips":
                intel_machine.flavor = None
                mips_machine.flavor = language
        if CLEAR in request.POST:
            intel_machine.re_init()
            mips_machine.re_init()
        else:
            step = (STEP in request.POST)
            intel_machine.nxt_key = 0
            mips_machine.nxt_key = 0
            if step:
                if intel_machine.flavor != None:
                    add_debug("Getting next key", intel_machine)
                    try:
                        intel_machine.nxt_key = int(
                            request.POST.get(NXT_KEY, 0))
                    except Exception:
                        intel_machine.nxt_key = 0
                else:
                    add_debug("Getting next key", mips_machine)
                    try:
                        mips_machine.nxt_key = int(request.POST.get(
                            NXT_KEY, 0))
                    except Exception:
                        mips_machine.nxt_key = 0

            if intel_machine.flavor != None:
                get_reg_contents(intel_machine.registers, request)
                get_mem_contents(intel_machine.memory, request)
                get_stack_contents(intel_machine.stack, request)
                get_flag_contents(intel_machine.flags, request)
            else:
                get_reg_contents(mips_machine.registers, request)
                get_mem_contents(mips_machine.memory, request)
                get_stack_contents(mips_machine.stack, request)
                get_flag_contents(mips_machine.flags, request)
            if intel_machine.flavor == "intel":
                (last_instr, error) = assemble(request.POST[CODE], INTEL,
                                               intel_machine, step)
            elif intel_machine.flavor == "att":
                (last_instr, error) = assemble(request.POST[CODE], ATT,
                                               intel_machine, step)
            else:
                (last_instr, error) = assemble(request.POST[CODE], "mips",
                                               mips_machine, step)

    if mips_machine.flavor == "mips":
        return render(
            request, 'main.html', {
                'form': form,
                HEADER: site_hdr,
                'last_instr': last_instr,
                'error': error,
                'unwritable': mips_machine.unwritable,
                'debug': mips_machine.debug,
                NXT_KEY: mips_machine.nxt_key,
                'registers': mips_machine.registers,
                'memory': mips_machine.memory,
                'stack': mips_machine.stack,
                'flags': mips_machine.flags,
                'flavor': mips_machine.flavor
            })

    return render(
        request, 'main.html', {
            'form': form,
            HEADER: site_hdr,
            'last_instr': last_instr,
            'error': error,
            'unwritable': intel_machine.unwritable,
            'debug': intel_machine.debug,
            NXT_KEY: intel_machine.nxt_key,
            'registers': intel_machine.registers,
            'memory': intel_machine.memory,
            'stack': intel_machine.stack,
            'flags': intel_machine.flags,
            'flavor': intel_machine.flavor
        })
Exemplo n.º 3
0
def dump_dict(d, intel_machine):
    for key, val in d.items():
        add_debug(str(key) + ": " + str(val), intel_machine)
Exemplo n.º 4
0
def dump_dict(d, gdata):
    for key, val in d.items():
        add_debug(str(key) + ": " + str(val), gdata)
Exemplo n.º 5
0
def main_page(request):
    last_instr = ""
    error = ""
    sample = "none"
    bit_code = ""
    button = ""

    site_hdr = get_hdr()
    if request.method == 'GET':
        if (intel_machine.flavor is None and
                mips_machine.flavor is None and
                riscv_machine.flavor is None):
            return render(request, 'main_error.html', {HEADER: site_hdr})
        machine_reinit()
        form = MainForm()
    else:
        vm = None
        base = request.POST['base']
        if 'language' in request.POST:
            machine_reinit()
            machine_flavor_reset()
            form = MainForm()
            lang = request.POST['language']
            if lang in MIPS:
                site_hdr += f": {MIPS[lang]} {base.upper()}"
                vm = mips_machine
            if lang in INTEL:
                site_hdr += f": {INTEL[lang]} {base.upper()}"
                vm = intel_machine
            if lang in RISCV:
                site_hdr += f": {RISCV[lang]} {base.upper()}"
                vm = riscv_machine
            if lang in WASM:
                wasm_machine.flavor = lang
                wasm_machine.base = base
                site_hdr += ": " + WASM[lang] + " " + wasm_machine.base.upper()
                # wasm does not have registers so it should not be calling
                # hex_conversion(wasm_machine)
                # r_reg, f_reg = processRegisters(wasm_machine.registers)
                return render(request, 'wasm.html',
                              {'form': form,
                               HEADER: site_hdr,
                               'last_instr': "",
                               'error': "",
                               'debug': wasm_machine.debug,
                               NXT_KEY: wasm_machine.nxt_key,
                               'memory': wasm_machine.memory,
                               'stack': wasm_machine.stack,
                               'symbols': wasm_machine.symbols,
                               'cstack': wasm_machine.c_stack,
                               'globals': wasm_machine.globals,
                               'locals': wasm_machine.locals,
                               'flavor': wasm_machine.flavor,
                               'data_init': wasm_machine.data_init,
                               'base': wasm_machine.base,
                               'sample': 'none',
                               'start_ip': wasm_machine.start_ip,
                               'bit_code': "",
                               'button_type': "",
                               'changes': [],
                               'stack_change': "",
                               })

            vm.base = base
            vm.flavor = lang
            hex_conversion(vm)
            render_data = create_render_data(request, vm, form, site_hdr,
                                             last_instr, error, sample,
                                             bit_code, button)
            return render(request, 'main.html', render_data)

        form = MainForm(request.POST)
        vm = None
        if 'flavor' in request.POST:
            machine_flavor_reset()
            language = request.POST['flavor']
            if language in INTEL:
                vm = intel_machine
                site_hdr += f": {INTEL[language]} {base.upper()}"
            elif language in MIPS:
                vm = mips_machine
                site_hdr += f": {MIPS[language]} {base.upper()}"
            elif language in RISCV:
                vm = riscv_machine
                site_hdr += f": {RISCV[language]} {base.upper()}"
            else:
                vm = wasm_machine
                site_hdr += f": {WASM[language]} {base.upper()}"
            vm.flavor = language
            vm.base = base
        sample = request.POST['sample']
        button = request.POST['button_type']
        if button == CLEAR:
            machine_reinit()
        else:
            intel_machine.changes_init()
            mips_machine.changes_init()
            riscv_machine.changes_init()
            wasm_machine.changes_init()
            step = (button == STEP) or (button == DEMO)
            intel_machine.nxt_key = 0
            mips_machine.nxt_key = 0
            riscv_machine.nxt_key = 0
            wasm_machine.nxt_key = 0
            if step:
                key = 0
                try:
                    key = int(request.POST.get(NXT_KEY, 0))
                except Exception:
                    key = 0
                add_debug("Getting next key", vm)
                vm.nxt_key = key

            if vm.flavor != 'wasm':
                get_reg_contents(vm.registers, request)
                get_flag_contents(vm.flags, request)
            else:
                get_symbol_contents(vm, request)
            get_mem_contents(vm.memory, request)
            get_stack_contents(vm.stack, request)
            vm.data_init = request.POST[DATA_INIT]
            vm.start_ip = int(request.POST['start_ip'])

            (last_instr, error, bit_code) = assemble(request.POST[CODE],
                                                     vm, step)
    if button == DEMO:
        if (last_instr == "Reached end of executable code." or
                last_instr.find("Exiting program") != -1):
            button = ""
        elif error != "":
            button = ""
    else:
        button = ""

    vm.order_mem()
    hex_conversion(vm)
    if vm.flavor == 'wasm':
        return render(request, 'wasm.html',
                      {'form': form,
                       HEADER: site_hdr,
                       'last_instr': "",
                       'error': "",
                       'debug': wasm_machine.debug,
                       NXT_KEY: wasm_machine.nxt_key,
                       'memory': wasm_machine.memory,
                       'stack': wasm_machine.stack,
                       'symbols': wasm_machine.symbols,
                       'cstack': wasm_machine.c_stack,
                       'globals': wasm_machine.globals,
                       'locals': wasm_machine.locals,
                       'flavor': wasm_machine.flavor,
                       'data_init': wasm_machine.data_init,
                       'base': wasm_machine.base,
                       'sample': 'none',
                       'start_ip': wasm_machine.start_ip,
                       'bit_code': "",
                       'button_type': "",
                       'changes': wasm_machine.changes,
                       'stack_change': "",
                       })
    render_data = create_render_data(request, vm, form, site_hdr, last_instr,
                                     error, sample, bit_code, button)
    return render(request, 'main.html', render_data)
Exemplo n.º 6
0
def main_page(request):
    last_instr = ""
    error = ""
    sample = ""
    bit_code = ""
    button = ""

    site_hdr = get_hdr()
    if request.method == 'GET':
        if (intel_machine.flavor is None and
                mips_machine.flavor is None and
                riscv_machine.flavor is None):
            return render(request, 'main_error.html', {HEADER: site_hdr})
        intel_machine.re_init()
        mips_machine.re_init()
        riscv_machine.re_init()
        form = MainForm()
    else:
        base = request.POST['base']
        if 'language' in request.POST:
            intel_machine.re_init()
            mips_machine.re_init()
            riscv_machine.re_init()
            form = MainForm()
            lang = request.POST['language']
            curr_reg = getCurrRegister(request.POST)
            if lang in MIPS:
                intel_machine.flavor = None
                riscv_machine.flavor = None
                mips_machine.flavor = lang
                mips_machine.base = base
                site_hdr += ": " + MIPS[lang] + " " + mips_machine.base.upper()
                hex_conversion(mips_machine)
                r_reg, f_reg = processRegisters(mips_machine.registers)
                return render(request, 'main.html',
                              {'form': form,
                               HEADER: site_hdr,
                               'last_instr': "",
                               'error': "",
                               'unwritable': mips_machine.unwritable,
                               'debug': mips_machine.debug,
                               NXT_KEY: mips_machine.nxt_key,
                               'registers': mips_machine.registers,
                               'r_registers': r_reg,
                               'f_registers': f_reg,
                               'memory': mips_machine.memory,
                               'stack': mips_machine.stack,
                               'symbols': mips_machine.symbols,
                               'cstack': mips_machine.c_stack,
                               'flags': mips_machine.flags,
                               'flavor': mips_machine.flavor,
                               'data_init': mips_machine.data_init,
                               'base': mips_machine.base,
                               'sample': 'none',
                               'start_ip': mips_machine.start_ip,
                               'bit_code': "",
                               'button_type': "",
                               'changes': [],
                               'stack_change': "",
                               'curr_reg': curr_reg
                               })
            if lang in INTEL:
                mips_machine.flavor = None
                riscv_machine.flavor = None
                intel_machine.base = base
                intel_machine.flavor = lang
                site_hdr += ": " + INTEL[lang] + " "
                site_hdr += intel_machine.base.upper()
                hex_conversion(intel_machine)
                return render(request, 'main.html',
                              {'form': form,
                               HEADER: site_hdr,
                               'last_instr': "",
                               'error': "",
                               'unwritable': intel_machine.unwritable,
                               'debug': intel_machine.debug,
                               NXT_KEY: intel_machine.nxt_key,
                               'registers': intel_machine.registers,
                               'memory': intel_machine.memory,
                               'stack': intel_machine.stack,
                               'symbols': intel_machine.symbols,
                               'cstack': intel_machine.c_stack,
                               'flags': intel_machine.flags,
                               'flavor': intel_machine.flavor,
                               DATA_INIT: intel_machine.data_init,
                               'base': intel_machine.base,
                               'sample': 'none',
                               'start_ip': intel_machine.start_ip,
                               'bit_code': "",
                               'button_type': "",
                               'changes': [],
                               'stack_change': ""
                               })
            if lang in RISCV:
                mips_machine.flavor = None
                intel_machine.flavor = None
                riscv_machine.flavor = lang
                riscv_machine.base = base
                site_hdr += ": " + RISCV[lang] + " "
                site_hdr += riscv_machine.base.upper()
                hex_conversion(riscv_machine)
                return render(request, 'main.html',
                              {'form': form,
                               HEADER: site_hdr,
                               'last_instr': "",
                               'error': "",
                               'unwritable': riscv_machine.unwritable,
                               'debug': riscv_machine.debug,
                               NXT_KEY: riscv_machine.nxt_key,
                               'registers': riscv_machine.registers,
                               'memory': riscv_machine.memory,
                               'stack': riscv_machine.stack,
                               'symbols': riscv_machine.symbols,
                               'cstack': riscv_machine.c_stack,
                               'flags': riscv_machine.flags,
                               'flavor': riscv_machine.flavor,
                               'data_init': riscv_machine.data_init,
                               'base': riscv_machine.base,
                               'sample': 'none',
                               'start_ip': riscv_machine.start_ip,
                               'bit_code': "",
                               'button_type': "",
                               'changes': [],
                               'stack_change': ""
                               })

        form = MainForm(request.POST)
        if 'flavor' in request.POST:
            language = request.POST['flavor']
            if language in INTEL:
                intel_machine.flavor = language
                intel_machine.base = base
                mips_machine.flavor = None
                riscv_machine.flavor = None
            if language in MIPS:
                intel_machine.flavor = None
                mips_machine.flavor = language
                mips_machine.base = base
                riscv_machine.flavor = None
            if language in RISCV:
                intel_machine.flavor = None
                mips_machine.flavor = None
                riscv_machine.flavor = language
                riscv_machine.base = base
        sample = request.POST['sample']
        button = request.POST['button_type']
        if button == CLEAR:
            intel_machine.re_init()
            mips_machine.re_init()
            riscv_machine.re_init()
        else:
            intel_machine.changes_init()
            mips_machine.changes_init()
            riscv_machine.changes_init()
            step = (button == STEP) or (button == DEMO)
            intel_machine.nxt_key = 0
            mips_machine.nxt_key = 0
            riscv_machine.nxt_key = 0
            if step:
                key = 0
                try:
                    key = int(request.POST.get(NXT_KEY, 0))
                except Exception:
                    key = 0
                if intel_machine.flavor is not None:
                    add_debug("Getting next key", intel_machine)
                    intel_machine.nxt_key = key
                if mips_machine.flavor is not None:
                    add_debug("Getting next key", mips_machine)
                    mips_machine.nxt_key = key
                if riscv_machine.flavor is not None:
                    add_debug("Getting next key", riscv_machine)
                    riscv_machine.nxt_key = key

            if intel_machine.flavor is not None:
                get_reg_contents(intel_machine.registers, request)
                get_mem_contents(intel_machine.memory, request)
                get_stack_contents(intel_machine.stack, request)
                get_flag_contents(intel_machine.flags, request)
                intel_machine.data_init = request.POST[DATA_INIT]
                intel_machine.start_ip = int(request.POST['start_ip'])
            if mips_machine.flavor is not None:
                get_reg_contents(mips_machine.registers, request)
                get_mem_contents(mips_machine.memory, request)
                get_stack_contents(mips_machine.stack, request)
                get_flag_contents(mips_machine.flags, request)
                mips_machine.data_init = request.POST[DATA_INIT]
                mips_machine.start_ip = int(request.POST['start_ip'])
            if riscv_machine.flavor is not None:
                get_reg_contents(riscv_machine.registers, request)
                get_mem_contents(riscv_machine.memory, request)
                get_stack_contents(riscv_machine.stack, request)
                get_flag_contents(riscv_machine.flags, request)
                riscv_machine.data_init = request.POST[DATA_INIT]
                riscv_machine.start_ip = int(request.POST['start_ip'])

            if intel_machine.flavor in INTEL:
                (last_instr, error, bit_code) = assemble(request.POST[CODE],
                                                         intel_machine.flavor,
                                                         intel_machine, step)
            if mips_machine.flavor in MIPS:
                (last_instr, error, bit_code) = assemble(request.POST[CODE],
                                                         mips_machine.flavor,
                                                         mips_machine, step)
            if riscv_machine.flavor in RISCV:
                (last_instr, error, bit_code) = assemble(request.POST[CODE],
                                                         riscv_machine.flavor,
                                                         riscv_machine, step)
    if button == DEMO:
        if (last_instr == "Reached end of executable code." or
                last_instr.find("Exiting program") != -1):
            button = ""
        elif error != "":
            button = ""
    else:
        button = ""

    if mips_machine.flavor in MIPS:
        mips_machine.order_mem()
        site_hdr += ": " + MIPS[mips_machine.flavor] + " "
        site_hdr += mips_machine.base.upper()
        hex_conversion(mips_machine)
        r_reg, f_reg = processRegisters(mips_machine.registers)
        curr_reg = getCurrRegister(request.POST)
        return render(request, 'main.html',
                      {'form': form,
                       HEADER: site_hdr,
                       'last_instr': last_instr,
                       'error': error,
                       'unwritable': mips_machine.unwritable,
                       'debug': mips_machine.debug,
                       NXT_KEY: mips_machine.nxt_key,
                       'registers': mips_machine.registers,
                       'r_registers': r_reg,
                       'f_registers': f_reg,
                       'memory': mips_machine.memory,
                       'stack': mips_machine.stack,
                       'symbols': mips_machine.symbols,
                       'cstack': mips_machine.c_stack,
                       'flags': mips_machine.flags,
                       'flavor': mips_machine.flavor,
                       DATA_INIT: mips_machine.data_init,
                       'base': mips_machine.base,
                       'sample': sample,
                       'start_ip': mips_machine.start_ip,
                       'bit_code': bit_code,
                       'button_type': button,
                       'changes': mips_machine.changes,
                       'stack_change': mips_machine.stack_change,
                       'curr_reg': curr_reg
                       })
    if intel_machine.flavor in INTEL:
        intel_machine.order_mem()
        site_hdr += ": " + INTEL[intel_machine.flavor] + " "
        site_hdr += intel_machine.base.upper()
        hex_conversion(intel_machine)
        return render(request, 'main.html',
                      {'form': form,
                       HEADER: site_hdr,
                       'last_instr': last_instr,
                       'error': error,
                       'unwritable': intel_machine.unwritable,
                       'debug': intel_machine.debug,
                       NXT_KEY: intel_machine.nxt_key,
                       'registers': intel_machine.registers,
                       'memory': intel_machine.memory,
                       'stack': intel_machine.stack,
                       'symbols': intel_machine.symbols,
                       'cstack': intel_machine.c_stack,
                       'flags': intel_machine.flags,
                       'flavor': intel_machine.flavor,
                       DATA_INIT: intel_machine.data_init,
                       'base': intel_machine.base,
                       'sample': sample,
                       'start_ip': intel_machine.start_ip,
                       'bit_code': bit_code,
                       'button_type': button,
                       'changes': intel_machine.changes,
                       'stack_change': intel_machine.stack_change
                       })
    if riscv_machine.flavor in RISCV:
        riscv_machine.order_mem()
        site_hdr += ": " + RISCV[riscv_machine.flavor] + " "
        site_hdr += riscv_machine.base.upper()
        hex_conversion(riscv_machine)
        return render(request, 'main.html',
                      {'form': form,
                       HEADER: site_hdr,
                       'last_instr': last_instr,
                       'error': error,
                       'unwritable': riscv_machine.unwritable,
                       'debug': riscv_machine.debug,
                       NXT_KEY: riscv_machine.nxt_key,
                       'registers': riscv_machine.registers,
                       'memory': riscv_machine.memory,
                       'stack': riscv_machine.stack,
                       'symbols': riscv_machine.symbols,
                       'cstack': riscv_machine.c_stack,
                       'flags': riscv_machine.flags,
                       'flavor': riscv_machine.flavor,
                       DATA_INIT: riscv_machine.data_init,
                       'base': riscv_machine.base,
                       'sample': sample,
                       'start_ip': riscv_machine.start_ip,
                       'bit_code': bit_code,
                       'button_type': button,
                       'changes': riscv_machine.changes,
                       'stack_change': riscv_machine.stack_change
                       })