def gen_fun(fun_id, fun_label, fun_kind):
    if fun_kind == 'function': far_size = 2
    else: far_size = 4
    prolog = assembler.block(fun_id, 'flash', fun_label)
    prolog.append_inst('push', 'r29')
    prolog.append_inst('push', 'r28')
    for id, kind, param_num \
     in crud.read_as_tuples('symbol_table', 'id', 'kind', 'int1',
                            context=fun_id,
                            order_by=('kind', 'desc', 'int1')):
        sym = symbol_table.get_by_id(id)
        far_size += 2
        sym.address = -far_size
        if fun_kind == 'task':
            sym.ram_size = 2
        if kind == 'parameter':
            prolog.append_inst('push', 'r%d' % (2 * param_num + 1))
            prolog.append_inst('push', 'r%d' % (2 * param_num))
    for id, name, next, next_conditional \
     in crud.read_as_tuples('blocks', 'id', 'name', 'next',
                            'next_conditional',
                            word_symbol_id=fun_id,
                            order_by=('id',)):
        asm_block = assembler.block(fun_id, 'flash', name)
        if next: asm_block.next_label(next)
        for what, should, this, be \
         in crud.read_as_tuples('triples',
                                block_id=id,
                                # FIX: finish...
                               ): pass
def get_var_uses(fn_id):
    r'''Get global variables usage for function fn_id.

    Returns vars_used, vars_set as two frozensets of symbol_ids.
    '''
    uses_vars = []
    sets_vars = []
    for var_id, sets in crud.read_as_tuples('fn_global_var_uses',
                                            'sets', 'var_id',
                                            fn_id=fn_id):
        if sets: sets_vars.append(var_id)
        else: uses_vars.append(var_id)
    return frozenset(uses_vars), frozenset(sets_vars)
    def get_sub_elements(cls, row_id):
        r'''Internal method called by `row_to_key` in derived classes.

        Reads the sub_elements from the database and returns a tuple of (name,
        type) tuples.

        This method is shared by all base classes.
        '''
        return tuple((name, Types_by_id[element_type])
                     for name, element_type
                      in crud.read_as_tuples('sub_element',
                                             'name', 'element_type',
                                             parent_id=row_id,
                                             order_by=('element_order',)))
def gen_assembler():
    for block_id, name, word_symbol_id, next, next_conditional \
     in crud.read_as_tuples('blocks', 'id', 'name', 'word_symbol_id', 'next',
                                      'next_conditional'):
        triples = triple2.read_triples(block_id)
        if Debug: print >> sys.stderr, "triples", triples
        tops = filter(lambda t: len(t.parents) == 0, triples)
        if Debug: print >> sys.stderr, "tops", tops
        crud.Db_cur.execute('''
                select predecessor, successor
                  from triple_order_constraints
                 where predecessor in (%(qmarks)s) or successor in (%(qmarks)s)
            ''' % {'qmarks': ', '.join('?' * len(triples))},
            [t.id for t in triples] * 2)
        pred_succ = crud.Db_cur.fetchall()
        if Debug: print >> sys.stderr, "pred_succ", pred_succ
        shareds = filter(lambda s: len(s) > 1,
                         (frozenset(tops(t.parents))
                          for t in triples if len(t.parents) > 1))
        if Debug: print >> sys.stderr, "shareds", shareds
        for top in order_tops(tops, pred_succ, shareds):
            print >> sys.stderr, \
                  'gen_assembler for block', block_id, 'triple', top.id
def gen_assembler():
    for fun_id, fun_label, fun_kind \
     in crud.read_as_tuples('symbol_table', 'id', 'label', 'kind',
                            kind=('function', 'task')):
        with crud.db_transaction():
            gen_fun(fun_id, fun_label, fun_kind)