changeset 363:d949fe826e04

Unify il and backend labels
author Michael Pavone <pavone@retrodev.com>
date Sat, 25 Apr 2015 20:56:02 -0700
parents 7101ad443081
children e44f65abaf0e
files modules/il.tp modules/x86.tp
diffstat 2 files changed, 30 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/modules/il.tp	Sat Apr 25 13:05:38 2015 -0700
+++ b/modules/il.tp	Sat Apr 25 20:56:02 2015 -0700
@@ -565,11 +565,14 @@
 		label <- {
 			_labelNum <- _curLabel
 			_curLabel <- _curLabel + 1
+			_offset <- option none
+			_forwardRefs <- #[]
 			#{
 				opcode <- { _label }
 				numops <- { 0 }
 				name <- { _names get: opcode }
 				string <- { name . " " . _labelNum }
+				length <- { 0 }
 				reference <- {
 					#{
 						isInteger? <- { false }
@@ -585,6 +588,14 @@
 						assign:withSource <- :assignments :regSrc {
 							self
 						}
+						withOffset:else <- :fun :elsefun {
+							_offset value: :off {
+								fun: off
+							} none: {
+								_forwardRefs append: fun
+								elsefun:
+							}
+						}
 					}
 				}
 				recordUsage:at <- :tracker :address {
@@ -593,6 +604,16 @@
 				assignRegs:at:withSource:andUsage <- :assignments :at :regSrc :usage {
 					self
 				}
+				flattenTo:at <- :dest :idx {
+					if: (_offset none?) {
+						_offset <- option value: idx
+						foreach: _forwardRefs :_ fun {
+							fun: idx
+						}
+						_forwardRefs <- #[]
+					}
+					idx
+				}
 			}
 		}
 		data <- :_bytes {
@@ -795,15 +816,16 @@
 				backend adjustIL: fun
 			}
 			labels <- prepped map: :_ {
-				backend label
+				il label
 			}
+			labelRefs <- labels map: :label { label reference }
 			outprog <- #[]
 			//translate main first so it's at the start of the output
 			prepped ifget: "main" :instarr {
 				outprog append: (labels get: "main" else: { false })
 				foreach: instarr :_ inst {
 					print: "Translating: " . inst . "\n"
-					backend convertIL: inst to: outprog withLabels: labels
+					backend convertIL: inst to: outprog withLabels: labelRefs
 				}
 			} else: {}
 			
@@ -811,7 +833,7 @@
 				outprog append: (labels get: name else: { false })
 				foreach: instarr :_ inst {
 					print: "Translating: " . inst . "\n"
-					backend convertIL: inst to: outprog withLabels: labels
+					backend convertIL: inst to: outprog withLabels: labelRefs
 				}
 			}
 			outprog
--- a/modules/x86.tp	Sat Apr 25 13:05:38 2015 -0700
+++ b/modules/x86.tp	Sat Apr 25 20:56:02 2015 -0700
@@ -466,37 +466,7 @@
 
 		ret <- { inst: [ 0xC3u8 ] }
 
-		label <- {
-			_offset <- -1
-			_forwardRefs <- #[]
-			#{
-				length <- { 0 }
-				hasOffset? <- { _offset >= 0 }
-				offset <- { _offset }
-				isInteger? <- { false }
-				register? <- { false }
-				label? <- { true }
-				flattenTo:at <- :dest :idx {
-					if: (not: hasOffset?) {
-						_offset <- idx
-						foreach: _forwardRefs :idx fun {
-							fun: _offset
-						}
-						_forwardRefs <- #[]
-					}
-					idx
-				}
-				string <- { "label: " . _offset }
-				withOffset:else <- :fun :elsefun {
-					if: hasOffset? {
-						fun: _offset
-					} else: {
-						_forwardRefs append: fun
-						elsefun:
-					}
-				}
-			}
-		}
+		label <- { il label }
 
 		jmp <- :jmpDest {
 			if: (jmpDest label?) {
@@ -887,7 +857,7 @@
 				{
 					//skipIf
 					endlab <- label:
-					outarr append: (jcc: (mapcond: (inst cond)) endlab)
+					outarr append: (jcc: (mapcond: (inst cond)) (endlab reference))
 					foreach: (inst toskip) :_ inst {
 						convertIL: inst to: outarr withLabels: labels withSaved: saved
 					}
@@ -897,11 +867,11 @@
 				{
 					endlab <- label:
 					elselab <- label:
-					outarr append: (jcc: (mapcond: (inst cond)) elselab)
+					outarr append: (jcc: (mapcond: (inst cond)) (elselab reference))
 					foreach: (inst toskip) :_ inst {
 						convertIL: inst to: outarr withLabels: labels withSaved: saved
 					}
-					outarr append: (jmp: endlab)
+					outarr append: (jmp: (endlab reference))
 					outarr append: elselab
 					foreach: (inst else) :_ inst {
 						convertIL: inst to: outarr withLabels: labels withSaved: saved
@@ -930,6 +900,7 @@
 				}
 				//label
 				{
+					outarr append: inst
 				}
 				//data
 				{