~manxi-david/pyeffect/pyeffect

« back to all changes in this revision

Viewing changes to s4/match.py

  • Committer: dvspeed
  • Date: 2011-12-26 13:18:36 UTC
  • Revision ID: manxi.david@gmail.com-20111226131836-mvoy9y32z0ujxvm1
-Change of folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2011 David Manzanares <manxi.david@gmail.com>
2
 
#This file is part of PyEffect.
3
 
#
4
 
# PyEffect is free software: you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation, either version 3 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# PyEffect is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with PyEffect. If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
#This is the matcher for the Stage 3
18
 
 
19
 
from s3 import act
20
 
from s3 import tImm
21
 
from s3 import tNat
22
 
from s3 import tChr
23
 
from s3 import loc
24
 
from s3 import lns
25
 
import arch
26
 
from arch import ins
27
 
 
28
 
nms = []
29
 
locs = []
30
 
 
31
 
class nm:
32
 
        def __init__(self, oldAct, newAct):
33
 
                if oldAct.loc.Type == "reg" and newAct.loc.Type == "reg" and oldAct.loc.index == newAct.loc.index:
34
 
                        pass
35
 
                else:
36
 
                        self.n = nms.__len__()
37
 
                        self.newAct = newAct
38
 
                        self.oldAct = oldAct
39
 
                        self.interferences = None
40
 
                        locs.append(oldAct.loc)
41
 
                        nms.append(self)
42
 
        def match(self):
43
 
                lns.now = lns.new()
44
 
                lns.now.exe = True
45
 
                var = self.newAct.up
46
 
                if var.Type == "char" and var.up.Type == "string":
47
 
                        lns.now.a1 = var.newAct("read", self.oldAct.loc)
48
 
                        lns.now.a1.pointer = True
49
 
                        lns.now.a2 = var.newAct("write", self.newAct.loc)
50
 
 
51
 
                else:
52
 
                        lns.now.a1 = var.newAct("read", self.oldAct.loc)
53
 
                        lns.now.a2 = var.newAct("write", self.newAct.loc)
54
 
                        if self.newAct.pointer == True:
55
 
                                lns.now.a1.pointer = True
56
 
                size = max(self.oldAct.size, self.newAct.up)
57
 
                if size == -1:
58
 
                        size = arch.wordSize
59
 
                lns.now.ins = "mov" + ins.suffixDic[size]
60
 
                #This makes the match just after the oldAct:
61
 
                #if self.oldAct.isNone==False:
62
 
                #       lns.now.insert(lns.lines.index(self.oldAct.line)+1)
63
 
                #else:
64
 
                #       lns.now.insert(self.newAct.line)
65
 
                lns.now.flow = self.newAct.line.flow
66
 
                lns.now.insert(self.newAct.line)
67
 
                locs.remove(self.oldAct.loc)
68
 
                self.interferences = -1
69
 
        def comp(self):
70
 
                if self.interferences != -1:
71
 
                        c = 0
72
 
                        for l in locs:
73
 
                                if l == self.newAct.loc and self.newAct.loc.asm != None:
74
 
                                        c = c + 1
75
 
                        self.interferences = c
76
 
        def __str__(self):
77
 
                return "NM " + str(self.n) + " Interferences: " + str(self.interferences) + "\nOld: " + str(self.oldAct) + "\nNew: " + str(self.newAct) + "\n"
78
 
 
79
 
def comp():
80
 
        lns.by.append("Match")
81
 
        #IMMS
82
 
        for v in tImm.imms:
83
 
                v.acts.sort()
84
 
                default = act.newNone(loc.new(None, None))
85
 
                for a in v.acts:
86
 
                        if a.loc != default.loc:
87
 
                                nm(default, a)
88
 
        #NATS
89
 
        for v in tNat.nats:
90
 
                v.acts.sort()
91
 
                la = act.newNone(loc.new(None))
92
 
                for a in v.acts:
93
 
                        if a.loc != la.loc and a.Type == "read" or a.Type == "modify":
94
 
                                nm(la, a)
95
 
                        if a.Type == "write" or a.Type == "modify":
96
 
                                la = a
97
 
        #exit(0)
98
 
        #CHARS
99
 
        for v in tChr.chrs:
100
 
                v.acts.sort()
101
 
                la = act.newNone(loc.new(None,))
102
 
                for a in v.acts:
103
 
                        if a.loc != la.loc and a.Type == "read":
104
 
                                nm(la, a)
105
 
        #Match
106
 
        m = nms.__len__()
107
 
        while m > 0:
108
 
                for x in nms:
109
 
                        x.comp()
110
 
                for x in nms:
111
 
                        if x.interferences == 0:
112
 
                                x.match()
113
 
                                m = m - 1
114
 
        lns.by.remove("Match")