1
# Copyright (C) 2011 David Manzanares <manxi.david@gmail.com>
2
#This file is part of PyEffect.
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.
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.
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/>.
17
#This is the naturals compiler module for the Stage 2
20
#Compile all variables of type natural
40
class new(types.staticVar):
41
def __init__(self, name, size):
42
types.staticVar.__init__(self, name, "natural")
45
self.n = nats.__len__()
51
if r.Type == "imm" or r.Type == "natural":
54
out.error("s2.tNat.new.set() Natural variables cannot been set to: " + ref.get(right).Type)
55
def newAct(self, Type, loc):
56
a = act.new(Type, self, loc)
60
"""Return a new str var containing the value of the natural number at the time of invoking this function"""
61
divQuo = new("divQuo", arch.wordSize)
62
remainder = new("remainder", arch.wordSize)
63
pointer = new("pointer", arch.wordSize)
64
string = tStr.new("string", 11)
66
lns.now.a2.loc = loc.new("reg", 0)
67
ins.mov(tImm.new(10), string.char[10])#Must be changed!!!
68
#Mov the memory adress of the top char of the string to the pointer
70
lns.now.ins = "mov" + arch.wordSuffix
72
lns.now.a1 = string.char[9].newAct("read", loc.new(None))
73
lns.now.a1.pointer = True
74
lns.now.a2 = pointer.newAct("write", loc.new("reg", 1))
76
label = ins.loop(self.name)
79
ins.div(divQuo, tImm.new(10), divQuo, remainder)
80
#Transform it to ASCII
81
ins.add(tImm.new(48), remainder)
82
lns.now.a2.loc = loc.new("reg", 3)#Don't hold it on memory
87
lns.now.a1 = remainder.newAct("read", loc.new("reg", 3))#Use the register %rdx
88
lns.now.a2 = pointer.newAct("write", loc.new("reg", 1))#Use a register
89
lns.now.a1.loc.asm = "%dl"
90
lns.now.a2.loc.asm = "(%rdi)"
91
string.newAct("write", loc.new(None))
94
lns.now.a1.loc = loc.new("reg", 1)
96
ins.cmp(tImm.new(0), divQuo)
97
lns.now.a2.loc = loc.new("reg", 0)
102
"""Print the value of the natural var at the time of invoking this function"""
103
divQuo = new("divQuo", arch.wordSize)
104
remainder = new("remainder", arch.wordSize)
105
pointer1 = new("pointer1", arch.wordSize)
106
pointer2 = new("pointer2", arch.wordSize)
107
string = tStr.new("string", 11)
108
ins.mov(self, divQuo)
109
lns.now.a2.loc = loc.new("reg", 0)
110
ins.mov(tImm.new(10), string.char[10])#Must be changed!!!
111
#Mov the memory adress of the top char of the string to the pointers
113
lns.now.ins = "mov" + arch.wordSuffix
115
lns.now.a1 = string.char[9].newAct("read", loc.new(None))
116
lns.now.a1.pointer = True
117
lns.now.a2 = pointer1.newAct("write", loc.new("reg", 1))
120
lns.now.ins = "mov" + arch.wordSuffix
122
lns.now.a1 = string.char[10].newAct("read", loc.new(None))
123
lns.now.a1.pointer = True
124
lns.now.a2 = pointer2.newAct("write", loc.new("reg", 2))
126
label = ins.loop(self.name)
129
ins.div(divQuo, tImm.new(10), divQuo, remainder)
130
#Transform it to ASCII
131
ins.add(tImm.new(48), remainder)
132
lns.now.a2.loc = loc.new("reg", 3)#Don't hold it on memory
137
lns.now.a1 = remainder.newAct("read", loc.new("reg", 3))#Use the register %rdx
138
lns.now.a2 = pointer1.newAct("write", loc.new("reg", 1))#Use a register
139
lns.now.a1.loc.asm = "%dl"
140
lns.now.a2.loc.asm = "(%rdi)"
141
string.newAct("write", loc.new(None))
142
#Increment the offset
144
lns.now.a1.loc = loc.new("reg", 1)
146
ins.cmp(tImm.new(0), divQuo)
147
lns.now.a2.loc = loc.new("reg", 0)
150
ins.sub(pointer1, pointer2)
151
lns.now.a1.loc = loc.new("reg", 1)
152
lns.now.a2.loc = loc.new("reg", 2)
153
ins.mov(pointer2, pointer2)
154
lns.now.a1.loc = loc.new("reg", 2)
155
lns.now.a2.loc = loc.new("reg", 3)
157
lns.now.a1.loc = loc.new("reg", 1)
158
syscall.make(None, tImm.new(1), tImm.new(1), pointer1, pointer2)
159
def printBasic(self):
160
print "Var name:", self.name, "Type:", self.Type, "Lifetime:", self.begin().getIndex(), "-", self.finish().getIndex(),