~manxi-david/pyeffect/pyeffect

« back to all changes in this revision

Viewing changes to s3/tNat.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 naturals compiler module for the Stage 2
18
 
 
19
 
#Purpose:
20
 
#Compile all variables of type natural
21
 
 
22
 
#Own libraries:
23
 
import var
24
 
import act
25
 
import ref
26
 
import lns
27
 
import loc
28
 
import tStr
29
 
import tImm
30
 
import arch
31
 
import out
32
 
import  syscall
33
 
import types
34
 
from arch import ins
35
 
 
36
 
#Global variables:
37
 
nats = []
38
 
loops = 0
39
 
 
40
 
class new(types.staticVar):
41
 
        def __init__(self, name, size):
42
 
                types.staticVar.__init__(self, name, "natural")
43
 
                self.size = size
44
 
                self.acts = []
45
 
                self.n = nats.__len__()
46
 
                self.copies = 0
47
 
                var.dicc[name] = self
48
 
                nats.append(self)
49
 
        def set(self, right):
50
 
                r = ref.get(right)
51
 
                if r.Type == "imm" or r.Type == "natural":
52
 
                        ins.mov(r, self)
53
 
                else:
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)
57
 
                self.acts.append(a)
58
 
                return a
59
 
        def toStr(self):
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)
65
 
                ins.mov(self, divQuo)
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
69
 
                lns.naa()
70
 
                lns.now.ins = "mov" + arch.wordSuffix
71
 
                lns.now.exe = True
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))
75
 
                #Begin of loop
76
 
                label = ins.loop(self.name)
77
 
                #Set vars
78
 
                #Divide
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
83
 
                #Save the value
84
 
                lns.naa()
85
 
                lns.now.ins = "movb"
86
 
                lns.now.exe = True
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))
92
 
                #Increment the offset
93
 
                ins.dec(pointer)
94
 
                lns.now.a1.loc = loc.new("reg", 1)
95
 
                #Is the end?
96
 
                ins.cmp(tImm.new(0), divQuo)
97
 
                lns.now.a2.loc = loc.new("reg", 0)
98
 
                ins.jne(label)
99
 
                lns.flow.pop()
100
 
                return string
101
 
        def fPrint(self):
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
112
 
                lns.naa()
113
 
                lns.now.ins = "mov" + arch.wordSuffix
114
 
                lns.now.exe = True
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))
118
 
                #Pointer 2
119
 
                lns.naa()
120
 
                lns.now.ins = "mov" + arch.wordSuffix
121
 
                lns.now.exe = True
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))
125
 
                #Begin of loop
126
 
                label = ins.loop(self.name)
127
 
                #Set vars
128
 
                #Divide
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
133
 
                #Save the value
134
 
                lns.naa()
135
 
                lns.now.ins = "movb"
136
 
                lns.now.exe = True
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
143
 
                ins.dec(pointer1)
144
 
                lns.now.a1.loc = loc.new("reg", 1)
145
 
                #Is the end?
146
 
                ins.cmp(tImm.new(0), divQuo)
147
 
                lns.now.a2.loc = loc.new("reg", 0)
148
 
                ins.jne(label)
149
 
                lns.flow.pop()
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)
156
 
                ins.inc(pointer1)
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(),
161
 
                return ""
162
 
        def printAll(self):
163
 
                self.printBasic()