def findLables(arr, dindx): for i in range(len(arr)): index = arr[i].find(':') if index > -1: label = (arr[i][:index].strip()) rval = symTable.symPresentN(label) if rval == -1: dindx += 1 symTable.symT(dindx, label, 0, 0, 'L', 'D', '-', '-') return dindx return dindx
def findAttr(fname): fp = open(fname, "r") fp2 = open("intermediate.txt", "w+") name = size = ts = flag = mflag = 0 dindx = lindx = -1 ddcnt = dbcnt = cnt = dqcnt = 0 for i in fp: arr = i.split(" ", 1) if len(arr) > 1: m = bool(re.search('.data', arr[1], re.IGNORECASE)) mm = bool(re.search('.bss', arr[1], re.IGNORECASE)) mmm = bool(re.search('.text', arr[1], re.IGNORECASE)) if m == True: flag = 1 fp2.writelines(i) continue #skip section .data line elif mm == True: flag = -1 fp2.writelines(i) continue elif mmm == True: flag = -2 fp2.writelines(i) continue if flag == 1: if (arr[1][:2]).lower() == 'dd': #check 1st two char name = arr[0].strip() size = 4 cnta = arr[1][3:].split(",") ddcnt += int(len(cnta)) lindx += 1 cnta = litTable.litT(lindx, cnta, 1) fp2.writelines(name + ' ' + 'dd' + ' ' + cnta + '\n') dindx += 1 symTable.symT(dindx, name, size, ddcnt, 'S', 'D', ts, cnta) ts += ddcnt * int(size) ddcnt = 0 elif (arr[1][:2]).lower() == 'db': name = arr[0].strip() size = 1 arr1 = arr[1][3:] for k in range(len(arr1)): if arr1[k] == '"' or arr1[k] == "'": cnt += 1 elif cnt == 1 and arr1[k] != '\n\t': dbcnt += 1 elif cnt == 2 or cnt == 0: if arr1[k] != ',' and arr1[k] != '1' and arr1[ k] != '\n': dbcnt += 1 #if k>0: # dbcnt+=1 lindx += 1 arr1 = litTable.litT(lindx, arr1, -1) fp2.writelines(name + ' ' + 'db' + ' ' + arr1 + '\n') dindx += 1 #symTable.symT(dindx,name,size,dbcnt,'S','D',ts,arr1[:len(arr1)-1]) #remove last \n symTable.symT(dindx, name, size, dbcnt, 'S', 'D', ts, arr1) ts += dbcnt * int(size) dbcnt = cnt = 0 elif (arr[1][:2]).lower() == 'dq': name = arr[0].strip() size = 8 cnta = arr[1][3:].split(",") dqcnt += int(len(cnta)) lindx += 1 cnta = litTable.litT(lindx, cnta, 1) fp2.writelines(name + ' ' + 'dq' + ' ' + cnta + '\n') dindx += 1 symTable.symT(dindx, name, size, dqcnt, 'S', 'D', ts, cnta) ts += dqcnt * int(size) elif flag == -1: if (arr[1][:4]).lower() == 'resd': name = arr[0].strip() size = 4 tsize = arr[1][5:].strip() fp2.writelines(i) dindx += 1 symTable.symT(dindx, name, size, tsize, 'S', 'D', ts, '-') ts += int(tsize) * int(size) elif (arr[1][:4]).lower() == 'resq': name = arr[0].strip() size = 8 tsize = arr[1][5:].strip() fp2.writelines(i) dindx += 1 symTable.symT(dindx, name, size, tsize, 'S', 'D', ts, '-') ts += int(tsize) * int(size) if len(arr) == 1: mflag = -1 if mflag == 0 and flag == -2: fp2.writelines(i) if mflag == -1: dindx = findLables(arr, dindx) dindx, lindx = regTable.regT(arr, dindx, lindx) #symTable.printSymT() #litTable.printLitT() fp.close() fp2.close() lstFile.lstF("intermediate.txt")
def regT(instrn, dindx, lindx): fp = open("intermediate.txt", "a+") flag = valflag = 0 i = len(instrn) if i > 1: m = bool(re.search('printf', instrn[1].strip(), re.IGNORECASE)) mm = bool(re.search('puts', instrn[1].strip(), re.IGNORECASE)) if m == True or mm == True: str1 = instrn[0] + ' ' + instrn[1] fp.writelines(str1) for key in reg: val = instrn[1].__contains__(key) if val == True: valflag = 1 str1 = "" str1 += instrn[0].strip() + ' ' chk = instrn[1].strip().split(",") for j in range(len(chk)): fst = chk[j].find('[') dword = chk[j][:fst + 1] if j == 0: if key == chk[j].strip(): str1 += reg.get(key) elif chk[j].__contains__(key) and fst > -1: str1 += dword + reg.get(key) + ']' else: indx = "" indx = symTable.checkSym(chk[j].strip()) if indx != chk[j].strip() and fst == -1: str1 += indx + '\n' elif indx != chk[j].strip() and fst > -1: str1 += dword + indx + ']' else: for r, v in reg.items(): if r == chk[j].strip(): str1 += v break if (len(chk) == 1): fp.write(str1 + '\n') elif j > 0: for r in reg: if r == chk[j].strip(): if fst == -1: str1 += ',' + reg.get(r) + '\n' elif fst > -1: str1 += ',' + reg.get(r) + ']\n' flag = 1 break elif chk[j].__contains__(key) and fst > -1: str1 += dword + reg.get(key) + ']\n' flag = 1 break else: flag = 0 if flag == 0: indx = "" indx = symTable.checkSym(chk[j].strip()) if indx != chk[j].strip(): if fst == -1: str1 += ',' + dword + indx + '\n' elif fst > -1: str1 += ',' + dword + indx + ']\n' else: indx = litTable.checkLit([chk[j].strip()]) if indx == -1: lindx += 1 indx = litTable.litT( lindx, [chk[j].strip()], 1) str1 += ',' + indx + '\n' fp.writelines(str1) break if valflag == 0: indx = str1 = "" indx = symTable.checkSym(instrn[1].strip()) if indx != (instrn[1].strip()): str1 = instrn[0].strip() + ' ' + indx + '\n' fp.writelines(str1) else: if instrn[0].strip() == 'jz' or instrn[0].strip( ) == 'jmp' or instrn[0].strip() == 'je' or instrn[0].strip( ) == 'jne': dindx += 1 symTable.symT(dindx, instrn[1].strip(), 0, 0, 'L', 'U', '-', '-') indx = symTable.checkSym(instrn[1].strip()) if indx != (instrn[1].strip()): str1 = instrn[0].strip() + ' ' + indx + '\n' fp.writelines(str1) else: str1 = "" if instrn[0] != '\n': str1 += instrn[0] fp.write(str1) fp.close() return dindx, lindx