示例#1
0
def mandatory_decls(input_ast, wnum):
    '''
    function for returning the mandatory declarations as a string
    '''
    return_string = ""
    inputast_name = input_ast.get_name()
    tag_string = "private static final String TAG = " + '"' + inputast_name + 'App"'
    return_string += generic.mk_stmt(tag_string)
    for i in range(0, wnum):
        return_string += generic.mk_stmt("private MutualExclusion mutex" + str(i))
    if input_ast.has_flag('allwrite') or input_ast.has_flag('allread'):
        return_string += generic.mk_stmt('private DSM dsm')
    return_string += generic.mk_stmt('private int numBots')
    return_string += generic.mk_stmt('private int pid')
    return return_string
示例#2
0
def mandatory_inits(input_ast, wnum):
    '''
    function for generating mandatory initializations.
    '''
    return_string = generic.mk_stmt("super(gvh)")
    return_string += generic.mk_stmt('String intValue = name.replaceAll("[^0-9]", "")')
    return_string += generic.mk_stmt("pid = Integer.parseInt(intValue)")
    return_string += generic.mk_stmt("numBots = gvh.id.getParticipants().size()")
    if input_ast.has_flag('allwrite') or input_ast.has_flag('allread'):
        return_string += generic.mk_stmt('dsm = new DSMMultipleAttr(gvh)')
    for i in range(0, wnum):
        mutex_str = ("mutex" + str(i) + " = new GroupSetMutex(gvh," + str(i) + ")")
        return_string += generic.mk_stmt(mutex_str)

    if input_ast.has_flag('Motion'):
        m_str = "MotionParameters.Builder settings = new MotionParameters.Builder()"
        return_string += generic.mk_stmt(m_str)
        m_str = "settings.COLAVOID_MODE(COLAVOID_MODE_TYPE.USE_COLAVOID)"
        return_string += generic.mk_stmt(m_str)
        return_string += generic.mk_stmt("MotionParameters param = settings.build()")
        return_string += generic.mk_stmt("gvh.plat.moat.setParameters(param)")
    return return_string
示例#3
0
def mk_dsms(symtab):
    """
    creating DSM variables
    :param symtab: input symbol table
    :return: string with dsm declarations.
    """
    ret_str = ""
    for decl in symtab:
        if decl.get_scope() == ALLWRITE:
            decl_str = 'dsm.createMW("' + str(decl.get_varname())
            decl_str += '",' + str(createval(str(decl.get_dtype()))) + ")"
            ret_str += generic.mk_stmt(decl_str)
    return ret_str
示例#4
0
def get_starl_code(app_code, symtab, delta=100):
    '''
    function for wrapping the event code in the loop. This is where you can change DELTA.
    '''
    return_code = "@Override\n"
    return_code += "public List<Object> callStarL() "
    dsm_code = mk_dsms(symtab)

    app_code = generic.mk_stmt("sleep(" + str(delta) + ")") + app_code
    app_code = dsm_code + "while(true) " + generic.mk_block(app_code, 1)

    return_code += generic.mk_block(app_code, 1)
    return return_code
示例#5
0
def get_package_decl(app_name):
    '''
    Function to generate package declaration from application name.
    '''
    return generic.add_nl(generic.mk_stmt("package " + app_name.lower()), 2)
示例#6
0
def codegen(input_ast, symtab, tabs=0, wnum=0, stages=False):
    '''
    The main code generation function. It takes as input an AST,
    and returns its corresponding java code. It is called recursively
    on the branches of the syntax tree.

    Args:
        input_ast (ast): if its not an AST, then return nothing, else generate code.
        tabs (int): indentation for generated java program.

    Returns:
        generated_code (str): java code as a string.

    '''
    generated_code = ""

    if not is_ast(input_ast):
        return str(input_ast)

    inputast_type = input_ast.get_type()

    if inputast_type == PGMTYPE:

        app_name = input_ast.get_name()
        include_code = ""

        for flag in input_ast.get_flags():
            include_code += generic.mk_indent(gen_ic(flag), tabs)

        generated_code += get_package_decl(app_name)
        generated_code += include_code
        generated_code += get_class_decl(app_name)
        block_code = generic.add_nl(mdecls.mandatory_decls(input_ast, wnum), 2)

        decl_code = ""
        for decl in symtab:
            decl_code += codegen(decl, [], 0, wnum)

        block_code += generic.mk_indent(decl_code, tabs)
        block_code += get_class_method(app_name)

        init_code = mdecls.mandatory_inits(input_ast, wnum)
        init = input_ast.get_init()
        if init is not None:
            init_code += codegen(init, symtab, tabs, wnum)
        block_code += generic.mk_block(init_code, tabs + 1)

        event_code = ""
        for event in input_ast.get_events():
            event_code += codegen(event, symtab, tabs, wnum)
        block_code += generic.mk_indent(get_starl_code(event_code, symtab),
                                        tabs)

        generated_code += generic.mk_block(block_code, tabs + 1)

    elif inputast_type == DECLTYPE:
        dtype = input_ast.get_dtype()
        varname = input_ast.get_varname()
        value = input_ast.get_value()
        decl_str = codegen(dtype, symtab) + " " + codegen(varname, symtab)
        if value is not None:
            decl_str += " = " + codegen(value, symtab)
        generated_code += generic.mk_stmt(decl_str)

    elif inputast_type == BVAL or inputast_type == NUMTYPE:
        generated_code = str(input_ast)

    elif inputast_type == INITTYPE:
        for stmt in input_ast.get_stmts():
            generated_code += codegen(stmt, symtab, tabs, wnum)

    elif inputast_type == EVENTTYPE:
        ename = input_ast.get_name()
        vars = get_vars(input_ast.get_pre())
        add_str = ""

        for var in vars:
            entry = get_entry(var[0], symtab)
            if entry is None:
                continue
            var_scope = entry.get_scope()
            if var_scope == ALLWRITE:
                add_str += generic.mk_stmt(
                    checknull(aw_get_codegen(var[0], var[1]), stages, ename))
                cast_str = ""
                l_br = ""
                r_br = ""
                if str(entry.get_dtype()) == 'int':
                    cast_str = "Integer.parseInt"
                    l_br = "("
                    r_br = ")"
                c_str = str(var[0]) + " = " + cast_str + l_br + (
                    aw_get_codegen(var[0], var[1])) + r_br
                add_str += generic.mk_stmt(c_str)
        #adding pre condition
        pre_code = "//" + str(ename) + "\n" + add_str
        pre_code += "if (" + codegen(input_ast.get_pre(), symtab, 0,
                                     wnum) + ")"
        generated_code += generic.mk_indent(pre_code, tabs).rstrip()

        eff_code = ""
        for stmt in input_ast.get_eff():
            eff_code += codegen(stmt, symtab, tabs, wnum)
        eff_code += generic.mk_indent("continue;", tabs)
        generated_code += generic.mk_block(eff_code, tabs + 1)

    elif inputast_type == ASGNTYPE:
        lvar = codegen(input_ast.get_lvar(), symtab, 0, wnum)
        rexp = codegen(input_ast.get_rexp(), symtab, 0, wnum)
        lv = get_vars(input_ast.get_lvar())
        rv = get_vars(input_ast.get_rexp())
        l_scope = get_entry(lv[0][0], symtab).get_scope()
        for var in rv:
            v_scope = get_entry(var[0], symtab).get_scope()
            if v_scope == ALLWRITE:
                cast_str = ""
                l_br = ""
                r_br = ""
                if str(get_entry(var[0], symtab).get_dtype()) == 'int':
                    cast_str = "Integer.parseInt"
                    l_br = "("
                    r_br = ")"
                    add_str = checknull(aw_get_codegen(var[0], symtab), stages,
                                        None) + "\n"
                    st = generic.mk_stmt(
                        str(var[0]) + " = " + cast_str + l_br +
                        (aw_get_codegen(var[0], symtab)) + r_br)
                    add_str += st
                    generated_code += add_str
        generated_code += generic.mk_stmt(lvar + " = " + rexp)
        if l_scope == ALLWRITE:
            generated_code += aw_put_codegen(input_ast.get_lvar())

    elif inputast_type == NUMTYPE:
        generated_code += str(input_ast)

    elif inputast_type == BVAL:
        generated_code += str(input_ast)

    elif inputast_type == VARTYPE:
        generated_code += str(input_ast)

    elif inputast_type == ARITHTYPE:
        generated_code += "(" + codegen(input_ast.get_lexp(), symtab, 0, wnum)
        generated_code += " " + str(input_ast.get_op())
        generated_code += " " + codegen(input_ast.get_rexp(), symtab, 0,
                                        wnum) + ")"

    elif inputast_type == RESTYPE:
        generated_code += str(input_ast)

    elif inputast_type == CONDTYPE:
        if input_ast.rexp is not None:
            generated_code += "(" + codegen(input_ast.get_lexp(), symtab, 0,
                                            wnum)
            generated_code += " " + str(input_ast.get_op()) + " "
            generated_code += codegen(input_ast.get_rexp(), symtab, 0,
                                      wnum) + ")"
        elif input_ast.op is not None:
            generated_code += str(input_ast.get_op())
            generated_code += "(" + codegen(input_ast.lexp, symtab, 0,
                                            wnum) + ")"
        else:
            generated_code += codegen(input_ast.get_lexp(), symtab, 0, wnum)

    elif inputast_type == ATOMICTYPE:
        swnum = str(input_ast.get_wnum())
        atomic_pre = "if(!wait" + swnum + ")"
        atomic_code = generic.mk_stmt("mutex" + swnum + ".requestEntry(0)")
        atomic_code += generic.mk_stmt("wait" + swnum + " = true")
        atomic_code = atomic_pre + generic.mk_block(atomic_code, 1)

        entry_pre = "if (mutex" + swnum + ".clearToEnter(0)) "
        entry_code = ""
        for stmt in input_ast.stmts:
            entry_code += codegen(stmt, symtab, 0, wnum)
        entry_code += generic.mk_stmt("mutex" + swnum + ".exit(0)")
        entry_code = entry_pre + generic.mk_block(entry_code, 1)
        generated_code += atomic_code + entry_code

    return generated_code