Created
April 25, 2018 09:14
-
-
Save kristinnmayo/40e7d8673063f666fb16e00bb2692173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cg_add(leftindex, rightindex): | |
global tempcount | |
### check for and free temporary variables | |
if symbol[leftindex].startswith('.t'): | |
tempcount -= 1 | |
if symbol[rightindex].startswith('.t'): | |
tempcount -= 1 | |
### check for constants | |
constonleft = False | |
constonright = False | |
if symbol[leftindex].startswith('.i'): | |
constonleft = True | |
if symbol[rightindex].startswith('.i'): | |
constonright = True | |
## check for registers, if so get index | |
regonleft = False | |
regonright = False | |
if loc[leftindex] == None: | |
regonleft = False | |
else: | |
regonleft = True | |
leftreg = loc[leftindex] | |
if loc[rightindex] == None: | |
regonright = False | |
else: | |
regonright = True | |
rightreg = loc[rightindex] | |
## case: ( constant + constant ) | |
if constonleft and constonright: | |
# constant folding | |
result = int(value[leftindex]) + int(value[rightindex]) | |
if result >= 0: | |
return enter('.i' + str(result), str(result), False) | |
else: | |
return enter('.i_' + str(-result), str(result), False) | |
## case: ( register + register ) | |
elif regonleft and regonright: | |
## get index of next temp variable | |
tempindex = cg_gettemp() | |
tempreg = loc[tempindex] | |
outfile.write(' add ' + tempreg + ', ' + leftreg + ', ' + rightreg + '\n') | |
return tempindex | |
## case: ( register + constant/variable ) | |
elif regonleft: | |
outfile.write(' ldr r1, ' + symbol[rightindex] + '\n') | |
outfile.write(' add ' + leftreg + ', ' + leftreg + ', r1\n') | |
needword[rightindex] = True | |
return leftindex | |
## case: ( constant/variable + register ) | |
if regonright: | |
outfile.write(' ldr r0, ' + symbol[leftindex] + '\n') | |
outfile.write(' add ' + rightreg + ', ' + rightreg + ', r1\n') | |
needword[leftindex] = True | |
return rightindex | |
## case: ( constant/variable + constant/variable ) | |
else: | |
## get index of next temp variable | |
tempindex = cg_gettemp() | |
tempreg = loc[tempindex] | |
outfile.write(' ldr r0, ' + symbol[leftindex] + '\n') | |
outfile.write(' ldr r1, ' + symbol[rightindex] + '\n') | |
needword[leftindex] = True | |
needword[rightindex] = True | |
# store sum in temp variable | |
outfile.write(' add ' + tempreg + ', r0, r1\n') | |
return tempindex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment