Beispiel #1
0
 def replace(words):
     """ Recursively replace labels in the code """
     result = []
     for word in words:
         if word[0] in label_ops:
             result.append((word[0], labels[word[1]]))
         elif word[0] == im.label()[0]:
             result.append(im.label(labels[word[1]]))
         elif symbol_match(word, im.parallel()):
             left, right = word[1:]
             result.append(im.parallel(replace(left), replace(right)))
         elif symbol_match(word, im.pipeline()):
             words = word[1]
             replaced_words = (replace(w) for w in words)
             result.append(im.pipeline(replaced_words))
         elif symbol_match(word, im.slot()):
             dec, enc, id_, trans = word[1:]
             if symbol_match(dec, im.choose()) or symbol_match(
                     dec, im.sample()):
                 result.append(
                     im.slot((dec[0], replace(dec[1])), enc, id_, trans))
             else:
                 result.append(word)
         else:
             result.append(word)
     return tuple(result)
Beispiel #2
0
 def replace(words):
     """ Recursively replace constant values to IDs in the code"""
     result = []
     for word in words:
         if word[0] == im.constant()[0]:
             if word[1] not in constants:
                 constants[word[1]] = len(constants)
             result.append(im.constant(constants[word[1]]))
         elif symbol_match(word, im.parallel()):
             left, right = word[1:]
             result.append(im.parallel(replace(left), replace(right)))
         elif symbol_match(word, im.pipeline()):
             words = word[1]
             replaced_words = (replace(w) for w in words)
             result.append(im.pipeline(replaced_words))
         elif symbol_match(word, im.slot()):
             dec, enc, id_, trans = word[1:]
             if symbol_match(dec, im.choose()) or symbol_match(
                     dec, im.sample()):
                 result.append(
                     im.slot((dec[0], replace(dec[1])), enc, id_, trans))
             else:
                 result.append(word)
         else:
             result.append(word)
     return tuple(result)
Beispiel #3
0
 def create_word(input_word):
     """ Turns a word into an assembled DSM word"""
     if symbol_match(input_word, im.slot()):
         with tf.variable_scope("slots"):
             dec, enc, label, transformations = input_word[
                 1:]
             # produce decoder & encoder
             with tf.name_scope("decoder"):
                 produced_dec = self.produce_decoder(dec)
             with tf.name_scope("encoder"):
                 produced_enc = self.produce_encoder(
                     enc, produced_dec.input_dim(),
                     transformations)
             with tf.name_scope("ENCODER_DECODER_WORD"):
                 created_word = edsm.EncoderDecoderWord(
                     produced_enc, produced_dec)
             self.slots[label] = created_word
     elif symbol_match(input_word, im.parallel()):
         true, false = input_word[1:]
         created_word = edsm.ParallelWord(
             create_word(to_single_word(true)),
             create_word(to_single_word(false)))
     elif symbol_match(input_word, im.pipeline()):
         words = input_word[1]
         steps = [create_word(s) for s in words]
         created_word = edsm.PipelineWord(steps)
     else:
         sym_dsm = sym.SymbolicDSM(self.stack_size * 2,
                                   self.stack_size * 2,
                                   self.heap_size,
                                   self.value_size)
         created_word = edsm.SymbolicDSMWord(
             sym_dsm, input_word)
     return created_word
Beispiel #4
0
def inject_steps(im_code):
    """
    Injects DSM step transitions where needed to make sure the PC always progresses.

    For example, the code:
        ('MACRO', (('BRANCH', 1),))
        ('MACRO', (('1+',), ('1+',), ('EXIT',)))
        ('MACRO', (('>',), ('CONSTANT', 0)))
        ('PARALLEL', (('MACRO', (('DROP',), ('DROP',))),), (('MACRO', (('DROP',),)),))
    results in:
        ('MACRO', (('BRANCH', 1),))
        ('MACRO', (('1+',), ('1+',), ('EXIT',)))
        ('MACRO', (('>',), ('CONSTANT', 0), ('STEP',)))
        ('PIPELINE', (('PARALLEL',
                       (('MACRO', (('DROP',), ('DROP',))),),
                       (('MACRO', (('DROP',),)),)), ('STEP',)))

    Args:
        im_code: the code to inject to

    Returns:
        a list of words such that every word is guaranteed to progress.

    """
    result = []
    macro_terminators = {
        im.step()[0],
        im.branch()[0],
        im.branch0()[0],
        im.call()[0],
        im.exit()[0],
        im.inc_do_loop()[0],
        im.slot()[0]
    }
    for word in im_code:
        if word[0] == im.macro()[0]:
            final_arg_typ = word[1][-1][0]
            if final_arg_typ not in macro_terminators:
                result.append(im.macro(word[1] + (im.step(), )))
            else:
                result.append(word)
        elif symbol_match(word, im.parallel()):
            result.append(im.pipeline((word, im.step())))
        elif (word[0] == im.slot()[0] and
              (word[1][0] == im.choose()[0] or word[1][0] == im.sample()[0])):
            dec, enc, label, transformations = word[1:]
            choices = dec[1]
            injected = [
                choice if choice[0] in macro_terminators else im.macro(
                    (choice, im.step())) for choice in choices
            ]
            result.append(
                im.slot((dec[0], tuple(injected)), enc, label,
                        transformations))
        elif word[0] not in macro_terminators:
            result.append(im.macro((word, im.step())))
        else:
            result.append(word)

    return result
Beispiel #5
0
def merge_pipelines(code):
    """
    Merges commands into a pipeline

    TODO example

    Args:
        code: intermediate code

    Returns:
        code with non-branched commands merged in pipelines
    """
    result = []
    current = None
    seq_types = {im.pipeline()[0], im.macro()[0]}
    branch_types = {
        im.branch()[0],
        im.branch0()[0],
        im.call()[0],
        im.inc_do_loop()[0]
    }

    def get_last_step(im_code):
        if im_code[0] in seq_types:
            return get_last_step(im_code[1][-1])
        else:
            return im_code

    for word in code:
        if current is not None and current[0] in seq_types:
            last_step = get_last_step(current)
            if last_step[0] not in branch_types:
                current = im.pipeline((current, word))
            else:
                result.append(current)
                current = word
        else:
            if current is not None:
                result.append(current)
            current = word
    if current is not None:
        result.append(current)
    return result
Beispiel #6
0
 def to_single_word(commands):
     """ Encapsulates a list of commands in a pipeline """
     if len(commands) == 1:
         return commands[0]
     else:
         return im.pipeline(commands)