flag = m20 > 0
if (flag) m222 = m3 else m222 = m4
-- convert 2-op conditional operator to 1-op <br>
removePhi [] = []<br>
removePhi ((Cmpz cond condr1) : (Phi addr r1 r2) : xs) = <br>
Noop addr : If cond condr1 addr r1 r2 : removePhi xs<br>
removePhi (x : xs) = x : removePhi xs<br>
Here the list is handled as follows: the input of the function is a list of opcodes, the output is a list in which this pair [Cmp, Phi] is replaced by [Noop, If]. Noop is inserted in order to leave the addresses of all commands unchanged (i.e. so that their number and position does not change, because their position in memory is one of their operands).main = do <br>
(len,buf) <- readMyFile<br>
let nframes = fromInteger $ len `div` 12 -- . <br>
-- opcode/data memory in tuples <br>
ids <- mapM ( \ i -> instruction (plusPtr buf $ i * 12 ) i) [ 0 .. nframes - 1 ]<br>
Here, every i (from 0 to nframes-1) was put on the result - a pair (instructon, data), which was obtained by adding the corresponding offset (i * 12) to the pointer (buf) and interpreting the command at this address (call instruction with where it lies with the instruction number, for even-odd sampling).-- code AST <br>
let code = removePhi $ map disasm $ zip (map fst ids) [ O.. ]<br>
Now we (read to the right), took from the list of pairs (ids) all (map) first (fst) elements (only instructions), made pairs (zip) with numbers from zero to as many as necessary ([0 ..] ), then for all these pairs (instruction, address) missed (map) via disasm, got a list of Op-s, and then removed Phi from them, making If instead of it (using removePhi).-- print code<br>
putStrLn $ produceCode code (map snd ids)<br>
Further, having code and data (via map snd ids - take the second halves of the pairs "(instruction, data)", they called produceCode with two parameters and printed the result (putStrLn).free buf
And this line just does not need comments.Source: https://habr.com/ru/post/70185/
All Articles