Skip to content
Snippets Groups Projects
Commit d466a367 authored by Nicholas Wesley Robinson's avatar Nicholas Wesley Robinson
Browse files

string literal initial

parent e24039f5
No related branches found
No related tags found
3 merge requests!30New new string,!27String concat,!20Master
from AST import ASTNode, getParseTreeNodes from AST import ASTNode, getParseTreeNodes
from Environment import Env from Environment import Env
from TheTypeNode import TypeNode, TypeStruct from TheTypeNode import TypeNode, TypeStruct
from CodeGenUtils import p, getLStringLabel from CodeGenUtils import p, getLStringLabel, importHelper
# LiteralNode # LiteralNode
# ParamNode # ParamNode
...@@ -61,14 +61,66 @@ class LiteralNode(ASTNode): ...@@ -61,14 +61,66 @@ class LiteralNode(ASTNode):
return return
# string TODO # string TODO
# if self.name == 'java.lang.String': if self.name == 'java.lang.String':
# n = getLStringLabel() self.code += ";Start of String Literal Creation for class " + self.typeName + "\n"
# lStringLabel = "_literalstring" + n # generate array of characters
# then call new String(array)
# self.code += p(lStringLabel + ":", "")
# self.code += p("db", str(self.getConstant())) # FIRST: create char array for this string TODO figure out how to populate this array
# self.code += p("mov", "eax", "[" + lStringLabel + "]", " set to literal string") # 1. Allocating space for the char array in heap
# return # Note: uses ecx to temporarily store the array length
self.code += ";Start of char array creation\n" + \
p(instruction="push", arg1="eax", arg2=str(len(self.value)), comment="array length") + \
p(instruction="push", arg1="ecx", comment="saving ecx's value") + \
p(instruction="mov", arg1="ecx", arg2="eax", comment="ecx now stroes the array's length") + \
p(instruction="add", arg1="eax", arg2=2, comment="number of items to allocate on heap") + \
p(instruction="imul", arg1="eax", arg2=4, comment="number of bytes to allocate on heap") + \
p(instruction="call", arg1="__malloc", comment="allocating memory for array on heap")
# 2. Pointing first item to vtable
aLabel = "A_char"
self.code += p(instruction="extern", arg1=aLabel) + \
p(instruction="mov", arg1="[eax]", arg2="dword "+aLabel, comment="first item is vtable pointer")
# 3. Storing length in the second slot
self.code += p(instruction="mov", arg1="[eax+4]", arg2="ecx", comment="storing array length in second slot")
# 4. Restoring ecx
self.code += p(instruction="pop", arg1="ecx", comment="restoring register ecx")
self.code += ";End of char array creation\n"
# SECOND: create new string object with the char array
# 1. alloc enough space for this object
classDef = self.env.getNode('java.lang.String', 'type')
fieldOffset = classDef.fieldOffset
numFields = len(fieldOffset)
numBytes = (numFields + 1) * 4
self.code += "; Creating an object for class " + self.typeName + "\n"
self.code += p(instruction="mov", arg1="eax", arg2=numBytes, comment="allocating memory for object") + \
p(instruction="call", arg1="__malloc")
# 2. Pointing first item to vtable
self.code += importHelper(classDef.name, self.typeName, "C_"+classDef.name)
self.code += p(instruction="mov", arg1="[eax]", arg2="dword C_" + classDef.name, comment="first item is vtable pointer")
# 3. Calling constructor
self.code += "; Calling constructor for String\n"
self.code += p(instruction="push", arg1="eax", comment="pushing object as first argument")
# Evaluate arguments and pushing parameters
self.code += p("mov", "eax", aLabel) # TODO this is probably wrong, where is my array I just created?
# 4. call String::String(char[] chars)
label = "M_String_String_char"
self.code += importHelper(classDef.name, self.typeName, label)
self.code += p(instruction="call", arg1=label, comment="Calling constructor")
# 4. Popping parameters and pointer to object
self.code += p(instruction="add", arg1="esp", arg2=4, comment="Popping parameters")
self.code += p(instruction="pop", arg1="eax", comment="eax now contains pointer to newly created object")
self.code += ";End of object creation\n"
self.code += ";End of String Literal Creation for class " + self.typeName + "\n"
return
# null # null
if self.name == 'null': if self.name == 'null':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment