changeset 348:a840e9a068a2

Get sample builtin to il module working again
author Michael Pavone <pavone@retrodev.com>
date Fri, 10 Apr 2015 00:48:42 -0700
parents ff7ea11b4b60
children 60292f131de9
files modules/il.tp modules/x86.tp
diffstat 2 files changed, 45 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/modules/il.tp	Fri Apr 10 00:48:12 2015 -0700
+++ b/modules/il.tp	Fri Apr 10 00:48:42 2015 -0700
@@ -374,8 +374,10 @@
 						_target recordUsage: tracker at: address withSize: q
 					}
 					foreach: _args :_ arg {
-						//TODO: have some mechanism for properly expressing sizes of arguments
-						arg recordUsage: tracker at: address withSize: q
+						if: (not: (arg isInteger?)) {
+							//TODO: have some mechanism for properly expressing sizes of arguments
+							arg recordUsage: tracker at: address withSize: q
+						}
 					}
 				}
 				assignRegs:at:withSource:andUsage <- :assignments :address :regSrc :usage {
@@ -515,7 +517,7 @@
 			}
 		}
 
-		allocRegs:withSource <- :instarr:regSrc {
+		allocRegs:withSource <- :instarr :regSrc {
 			_regMap <- dict linear
 			_argMap <- dict linear
 
@@ -542,7 +544,7 @@
 			liveFrom:to <- :regs :from :to {
 				live <- #[]
 				foreach: regs :reg usage {
-					if: ((usage lastUsage) addrGreatEq: from) && ((usage firstUsage) addrLessEq: to) {
+					if: (addrGreatEq: (usage lastUsage) from) && (addrLessEq: (usage firstUsage) to) {
 						live append: reg
 					}
 				}
@@ -568,10 +570,10 @@
 				}
 
 				liveRegsAt <- :address {
-					_regMap liveFrom: address to: address
+					liveFrom: _regMap address to: address
 				}
 				liveArgsAt <- :address {
-					_argMap liveFrom: address to: address
+					liveFrom: _argMap  address to: address
 				}
 
 				print <- {
@@ -627,12 +629,12 @@
 			while: { curuses > 0 && (_assignments length) < (_regMap length) } do: {
 				foreach: _regMap  :reg usage {
 					if: (usage useCount) = curuses {
-						liveArgs <- _argMap liveFrom: (usage firstUsage) to: (usage lastUsage)
+						liveArgs <- liveFrom: _argMap  (usage firstUsage) to: (usage lastUsage)
 						foreach: liveArgs :_ arg {
 							regSrc allocArg: (arg num)
 						}
 
-						liveRegs <- _regMap liveFrom: (usage firstUsage) to: (usage lastUsage)
+						liveRegs <- liveFrom: _regMap (usage firstUsage) to: (usage lastUsage)
 						print: (string: reg) . " | Live: " . (liveRegs join: ", ") . ", Live Args: " . (liveArgs join: ", ") . "\n"
 						foreach: liveRegs :_ reg {
 							if: (_assignments contains?: reg) {
@@ -705,6 +707,7 @@
 			foreach: prepped :name instarr {
 				outprog append: (labels get: name)
 				foreach: instarr :_ inst {
+					print: "Translating: " . inst . "\n"
 					backend convertIL: inst to: outprog withLabels: labels
 				}
 			}
@@ -732,9 +735,17 @@
 			}
 			prog set: "fib" fib
 
-			mprog <- prog toBackend: x86
+			mprog <- toBackend: prog x86
+			foreach: mprog :_ inst {
+				print: (string: inst) . "\n"
+			}
 			ba <- bytearray executableFromBytes: mprog
-			res <- ba runWithArg: 30u64
+			res <- if: (ba length) = 0 {
+				print: "Failed to translate code\n"
+				-1
+			} else: {
+				ba runWithArg: 30u64
+			}
 			print: (string: res) . "\n"
 			0
 		}
--- a/modules/x86.tp	Fri Apr 10 00:48:12 2015 -0700
+++ b/modules/x86.tp	Fri Apr 10 00:48:42 2015 -0700
@@ -226,6 +226,7 @@
 					idx + 1
 				}
 			}
+			string <- { (ilist map: :el { hex: el}) join: " "}
 		}
 	}
 	multiInst <- :instarr {
@@ -240,6 +241,11 @@
 					inst flattenTo: dest at: idx
 				}
 			}
+			string <- {
+				(instarr map: :inst {
+					(inst map: :el { hex: el}) join: " "
+				}) join: "\n" 
+			}
 		}
 	}
 
@@ -449,6 +455,7 @@
 					}
 					idx
 				}
+				string <- { "label: " . _offset }
 				withOffset:else <- :fun :elsefun {
 					if: hasOffset? {
 						fun: _offset
@@ -503,6 +510,9 @@
 						}
 						idx + _size
 					}
+					string <- {
+						"jmp " . jmpDest
+					}
 				}
 			} else: {
 				inst: 0xFFu8 | (mod_rm: (opex: 5u8) jmpDest)
@@ -540,6 +550,9 @@
 					}
 					idx + _size
 				}
+				string <- {
+					"jcc " . jmpDest
+				}
 			}
 		}
 
@@ -559,6 +572,9 @@
 						}
 						idx + 5
 					}
+					string <- {
+						"call " . callDest
+					}
 				}
 			} else: {
 				inst: 0xFFu8 | (mod_rm: (opex: 2u8) callDest)
@@ -732,8 +748,10 @@
 				{ outarr append: (and: (inst in) (inst out) (mapSize: (inst size))) }
 				{ outarr append: (or: (inst in) (inst out) (mapSize: (inst size))) }
 				{ outarr append: (xor: (inst in) (inst out) (mapSize: (inst size))) }
-				//mul
-				//div
+				{ } //muls
+				{ } //mulu
+				{ } //divs
+				{ } //divu
 				{ outarr append: (sub: (inst in) (inst out) (mapSize: (inst size))) }
 				{ outarr append: (cmp: (inst in) (inst out) (mapSize: (inst size))) }
 				{ outarr append: (bnot: (inst arg) (mapSize: (inst size))) }
@@ -789,6 +807,7 @@
 					outarr append: endlab
 				}
 				//skipIf:else
+				{ }
 				{
 					//save
 					newsave <- []
@@ -799,14 +818,16 @@
 					foreach: (inst scope) :_ inst {
 						convertIL: inst to: outarr withLabels: labels withSaved: newsave
 					}
-					if: ((inst scope) length) = 0 || (((inst scope) get: ((inst scope) length) - 1) opcode) != 14 {
+					if: ((inst scope) length) = 0 || (((inst scope) get: ((inst scope) length) - 1) opcode) != 18 {
 						foreach: newsave :_ reg {
 							outarr append: (pop: reg)
 						}
 					}
 				}
 				//bool
+				{ }
 			]
+			print: "Opcode: " . (inst opcode) . "\n"
 			fun <- opmap get: (inst opcode)
 			fun:
 			outarr