;*;	objfile	includes:optim68brain.mac


doneNextLink		equ	%00000000000000000000000000000001	; *
doneOutput		equ	%00000000000000000000000000000010	; *
doneAutomation		equ	%00000000000000000000000000000100	; *
doneMessage		equ	%00000000000000000000000000001000	; *
doneOptimString		equ	%00000000000000000000000000010000
doneNormalAction	equ	%00000000000000000000000000100000	; *
doneRememberInfo	equ	%00000000000000000000000001000000	; *
doneSubstituteLine	equ	%00000000000000000000000010000000


* OUTPUT types
*
fileExport	equ	0
rexxExport	equ	2
screenOnly	equ	4
none		equ	4
lastOutputCode	equ	4	;highest allowed output code

* ACTION types
*
ignoreLine	equ	0
removeLine	equ	2
substituteLine	equ	4
reportLine	equ	6
rememberInfo	equ	8

* AUTOMATION types
*
showUser	equ	0	;allow user optimization choice
autoFix		equ	1	;auto-optimize
tellUser	equ	2	;hand optimization only
informUser	equ	3	;information message only

* INDEX TYPES
*
Operand1	equ	0
Operand2	equ	4
Operand3	equ	8
Operand4	equ	12


* CONDITIONALS
*
ifImmediate		equ	0
if_eq			equ	1
if_ge			equ	2
if_le			equ	3
ifField1		equ	4
ifField2		equ	5
ifField3		equ	6
ifField4		equ	7
ifByte			equ	8
ifWord			equ	9
ifLong			equ	10
ifSingle		equ	11
ifDouble		equ	12
ifExtended		equ	13
ifPacked		equ	14
ifQuadLong		equ	15
ifRemembered		equ	16
if_is020		equ	17
ifNField1		equ	18
ifNField2		equ	19
ifNField3		equ	20
ifNField4		equ	21
ifSameField1		equ	22
ifSameField2		equ	23
ifSameField3		equ	24
ifSameField4		equ	25
ifLabel			equ	26
ifNoLabel		equ	27
ifRememberedOperand	equ	28

* TEST SYMBOLS used with if_OperandN macros

immediateValue		equ	0
addressReg		equ	2
dataReg			equ	4
controlReg		equ	6
registerList		equ	8
AbsoluteShort		equ	10
AbsoluteLong		equ	12
ARIndirect		equ	14
ARPostInc		equ	16
ARPreDec		equ	18
ARBaseDisp		equ	20
ARBrief			equ	22
ARFullFormat		equ	24
ARFullFormatOD		equ	26
ARFullFormatBD		equ	28
ARFullFormatBDOD	equ	30
PCBaseDisp		equ	32
PCBrief			equ	34
PCFullFormat		equ	36
PCFullFormatOD		equ	38
PCFullFormatBD		equ	40
PCFullFormatBDOD	equ	42
otherAddressMode	equ	44
controlMode		equ	46

lastFieldTestCode	equ	46	;highest legal field test value


* TEST SYMBOLS for use ONLY with ifPrev_Operand macro

trueIfSame	equ	%00000100
falseIfSame	equ	0

dataRegDirect	equ	%00000000
addrRegDirect	equ	%00001000
impliedAddress	equ	%00010000
postIncAddress	equ	%00011000
preDecAddress	equ	%00100000
b16Address	equ	%00101000
bdXnAddress	equ	%00110000
allOtherAddress	equ	%00111000


********************************************************************************
********************************************************************************
*                       SOURCE CODE MANIPULATION MACROS                        *
********************************************************************************
********************************************************************************

Terminate		MACRO
	db	0
	ENDM

StartOfLine		MACRO
	db	1
	ENDM

StartOfLine1Back	MACRO
	db	2
	ENDM

StartOfLine2Back	MACRO
	db	3
	ENDM

StartOfLine3Back	MACRO
	db	4
	ENDM

StartOfLine4Back	MACRO
	db	5
	ENDM

StartOfLine5Back	MACRO
	db	6
	ENDM

StartOfLine6Back	MACRO
	db	7
	ENDM

StartOfLine7Back	MACRO
	db	$83
	ENDM

StartOfLine8Back	MACRO
	db	$84
	ENDM

StartOfLine9Back	MACRO
	db	$a
	ENDM

SetOrigin1LineBack	MACRO
	db	$b
	ENDM

SetOrigin2LineBack	MACRO
	db	$c
	ENDM

SetOrigin3LineBack	MACRO
	db	$d
	ENDM

SetOrigin4LineBack	MACRO
	db	$e
	ENDM

SetOrigin5LineBack	MACRO
	db	$f
	ENDM

SetOrigin6LineBack	MACRO
	db	$10
	ENDM

SetOrigin7LineBack	MACRO
	db	$11
	ENDM

SetOrigin8LineBack	MACRO
	db	$12
	ENDM

SetOrigin9LineBack	MACRO
	db	$13
	ENDM

SaveLocPtr1		MACRO
	db	$14
	ENDM

LoadLocPtr1		MACRO
	db	$15
	ENDM

SaveLocPtr2		MACRO
	db	$16
	ENDM

LoadLocPtr2		MACRO
	db	$17
	ENDM

Skip1OperandField	MACRO
	db	$18
	ENDM

Copy1OperandField	MACRO
	db	$19
	ENDM

CopyWhitespace		MACRO
	db	$1a
	ENDM

CopyToEOL		MACRO
	db	$1b
	ENDM

SkipToEOL		MACRO
	db	$86
	ENDM

SkipNChars		MACRO	1,1
	db	$1c,'\1',$7f
	ENDM

CopyNChars		MACRO	1,1
	db	$1d,'\1',$7f
	ENDM

CopySizeExt		MACRO
	db	$1e
	ENDM

CopyToOpcodeField	MACRO
	db	$1f
	ENDM

SkipToOpcodeField	MACRO
	db	$85
	ENDM

CopyToCurrentLine	MACRO
	db	$80
	ENDM

CopyToOperandField	MACRO
	db	$81
	ENDM

BackupNChars		MACRO	1,1
	db	$82,'\1',$7f
	ENDM



NewSource		MACRO	1,1
	db	\1
	ENDM


********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
*                     OPTIMIZATION EVALUATION UNIT MACROS                      *
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************



nextLink	MACRO	1,1
	ifnc	'\1','0'
		sw	\1-*
	else
		dw	0
	endc
oeuCheckVal	set	doneNextLink
_ifCount	set	*
conditionCount	set	0
		db	0		;reserve space for condition count
	ENDM


*******************


Output	MACRO	1,1
		db	\1
	ifne	oeuCheckVal&doneOutput
		PRINTX	Line #\*VALOF(_linenum) *** "Output" macro already supplied for this OEU.
	endc
oeuCheckVal	set	oeuCheckVal!doneOutput
	ifgt	\1-lastOutputCode
	FAIL	Invalid output code
	else
	ifne	\1&$00000001
	FAIL	Invalid output code
	endc
	endc
	ENDM


*******************


Automation	MACRO	1,1
		db	\1
oeuCheckVal	set	oeuCheckVal!doneAutomation
	ifgt	\1-255
	FAIL	The automation code must be in the range of 0 to 255
	endc
	ENDM


*******************


AlwaysShowUser	MACRO	0,0
		db	showUser
oeuCheckVal	set	oeuCheckVal!doneAutomation
	ENDM


*******************


Message		MACRO	1,1
	sw	\1-*
oeuCheckVal	set	oeuCheckVal!doneMessage
	ENDM


*******************


OptimString	MACRO	1,1
	sw	\1-*
oeuCheckVal	set	oeuCheckVal!doneOptimString
	ENDM


*******************


Action		MACRO	1,3
		poke.b	#conditionCount,(_ifCount+_hunkbase)
		db	\1			;action code

	ifeq	NARG-1
oeuCheckVal	set	oeuCheckVal!doneNormalAction
	ifc	'\1','ignoreLine'
	MEXIT
	endc
	ifc	'\1','removeLine'
	MEXIT
	endc
	ifc	'\1','substituteLine'
oeuCheckVal	set	oeuCheckVal!doneSubstituteLine
	MEXIT
	endc
	ifc	'\1','reportLine'
	MEXIT
	endc
	FAIL	Unknown first argument or Need more arguments.
	MEXIT
	endc

	ifc	'\1','rememberInfo'
oeuCheckVal	set	oeuCheckVal!doneRememberInfo
	ifne	NARG-3
	FAIL	Wrong number of arguments supplied for "rememberInfo"
	endc
	ifle	\2
	FAIL	The remember index value must be in the range of 1 to 10
	endc
	ifgt	\2-10
	FAIL	The remember index value must be in the range of 1 to 10
	endc
	iflt	\3-1
	FAIL	User defined value must be in the range of 1 to 65535
	endc
	ifgt	\3-65535
	FAIL	User defined value must be in the range of 1 to 65535
	endc
		Output	none
		db	\2			;additional action info (action code specific)
		dw	\3			;user defined value
	else
	FAIL	First argument unknown
	endc

	ENDM


*******************


ControlAction	MACRO	1,1
		db	\1			;action code

oeuCheckVal	set	oeuCheckVal!doneNormalAction
	ifc	'\1','ignoreLine'
	MEXIT
	endc
	ifc	'\1','removeLine'
	MEXIT
	endc
	ifc	'\1','substituteLine'
oeuCheckVal	set	oeuCheckVal!doneSubstituteLine
	MEXIT
	endc
	ifc	'\1','reportLine'
	MEXIT
	endc
	FAIL	Unknown first argument or Need more arguments.
	ENDM


*******************


OEU_end	MACRO
	ifeq	oeuCheckVal&doneNextLink
		FAIL	You MUST begin an OEU with a nextLink macro.
	endc
	ifeq	oeuCheckVal&doneOutput
		FAIL	Output macro MUST be used in this format OEU.
	endc


	ifne	oeuCheckVal&doneRememberInfo
	ifne	oeuCheckVal&doneAutomation
		PRINTX	Line #\*VALOF(_linenum) *** No "Automation" macro is needed in this OEU.
	endc
	ifne	oeuCheckVal&doneMessage
		PRINTX	Line #\*VALOF(_linenum) *** No "Message" macro is needed in this OEU.
	endc
	ifne	oeuCheckVal&doneOptimString
		PRINTX	Line #\*VALOF(_linenum) *** No "OptimString" macro is needed in this OEU.
	endc
		MEXIT
	endc


	ifeq	oeuCheckVal&doneNormalAction
		FAIL	The "Action" macro MUST be used in this format OEU.
	endc
	ifeq	oeuCheckVal&doneMessage
		FAIL	The "Message" macro MUST be used in this format OEU.
	endc
	ifeq	oeuCheckVal&doneAutomation
		FAIL	The "Automation" macro MUST be used in this format OEU.
	endc
	ifne	oeuCheckVal&doneSubstituteLine
	ifeq	oeuCheckVal&doneOptimString
		FAIL	The "OptimString" macro MUST be used in this format OEU.
	endc
	endc
oeuCheckVal	set	0
	ENDM


*******************


specialOEU_end	MACRO
	ifeq	oeuCheckVal&doneOutput
		FAIL	Output macro MUST be used in this format OEU.
	endc


	ifne	oeuCheckVal&doneRememberInfo
	ifne	oeuCheckVal&doneAutomation
		PRINTX	Line #\*VALOF(_linenum) *** No "Automation" macro is needed in this OEU.
	endc
	ifne	oeuCheckVal&doneMessage
		PRINTX	Line #\*VALOF(_linenum) *** No "Message" macro is needed in this OEU.
	endc
	ifne	oeuCheckVal&doneOptimString
		PRINTX	Line #\*VALOF(_linenum) *** No "OptimString" macro is needed in this OEU.
	endc
		MEXIT
	endc


	ifeq	oeuCheckVal&doneMessage
		FAIL	The "Message" macro MUST be used in this format OEU.
	endc
	ifeq	oeuCheckVal&doneAutomation
		FAIL	The "Automation" macro MUST be used in this format OEU.
	endc
oeuCheckVal	set	0
	ENDM


********************************************************************************
*                              CONDITIONAL MACROS                              *
********************************************************************************


if_ImmediateSrc	MACRO	0,0
		db	ifImmediate
conditionCount	set	conditionCount+1
	ENDM


*********


if_QuadlongSize	MACRO	0,0
		db	ifQuadLong
conditionCount	set	conditionCount+1
	ENDM


*********


if_PackedSize	MACRO	0,0
		db	ifPacked
conditionCount	set	conditionCount+1
	ENDM


*********


if_ExtendedSize	MACRO	0,0
		db	ifExtended
conditionCount	set	conditionCount+1
	ENDM


*********


if_DoubleSize	MACRO	0,0
		db	ifDouble
conditionCount	set	conditionCount+1
	ENDM


*********


if_SingleSize	MACRO	0,0
		db	ifSingle
conditionCount	set	conditionCount+1
	ENDM


*********


if_LongSize	MACRO	0,0
		db	ifLong
conditionCount	set	conditionCount+1
	ENDM


*********


if_WordSize	MACRO	0,0
		db	ifWord
conditionCount	set	conditionCount+1
	ENDM


*********


if_ByteSize	MACRO	0,0
		db	ifByte
conditionCount	set	conditionCount+1
	ENDM


*********


if_EQ	MACRO	1,1
		db	if_eq
		dl	\1
conditionCount	set	conditionCount+1
	ENDM


*********


if_GE	MACRO	1,1
		db	if_ge
		dl	\1
conditionCount	set	conditionCount+1
	ENDM


*********


if_LE	MACRO	1,1
		db	if_le
		dl	\1
conditionCount	set	conditionCount+1
	ENDM


*********


if_Operand1	MACRO	1,1
		db	ifField1
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


if_Operand2	MACRO	1,1
		db	ifField2
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


if_Operand3	MACRO	1,1
		db	ifField3
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


if_Operand4	MACRO	1,1
		db	ifField4
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


ifNot_Operand1	MACRO	1,1
		db	ifNField1
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


ifNot_Operand2	MACRO	1,1
		db	ifNField2
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


ifNot_Operand3	MACRO	1,1
		db	ifNField3
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


ifNot_Operand4	MACRO	1,1
		db	ifNField4
		db	\1
conditionCount	set	conditionCount+1
	ifgt	\1-lastFieldTestCode
	FAIL	Invalid code supplied
	else
	ifne	\1&$00000001
	FAIL	Invalid code supplied
	endc
	endc
	ENDM


*********


ifSame_Operand1	MACRO	4,4
		db	ifSameField1
		db	\1		;remember index
		db	\2		;# lines ago
		dw	\3		;user defined value
		db	\4		;operand to check against
conditionCount	set	conditionCount+1
	ENDM


*********


ifSame_Operand2	MACRO	4,4
		db	ifSameField2
		db	\1
		db	\2
		dw	\3
		db	\4
conditionCount	set	conditionCount+1
	ENDM


*********


ifSame_Operand3	MACRO	4,4
		db	ifSameField3
		db	\1
		db	\2
		dw	\3
		db	\4
conditionCount	set	conditionCount+1
	ENDM


*********


ifSame_Operand4	MACRO	4,4
		db	ifSameField4
		db	\1
		db	\2
		dw	\3
		db	\4
conditionCount	set	conditionCount+1
	ENDM


*********


if_Label	MACRO	0,0
		db	ifLabel
conditionCount	set	conditionCount+1
	ENDM


*********


if_NoLabel	MACRO	0,0
		db	ifNoLabel
conditionCount	set	conditionCount+1
	ENDM


*********


ifPrev_Operand	MACRO	6,6
		db	ifRememberedOperand
		db	\1		;remember index
		db	\2		;# lines ago
		dw	\3		;user defined value
		db	\4		;operand to check against
		db	\5		;address mode to test against
		db	\6		;true/false modifier
conditionCount	set	conditionCount+1
	ENDM


*********


if_Remembered	MACRO	3,3
	ifle	\1
	FAIL	The remember index value must be in the range of 1 to 10
	endc
	ifgt	\1-10
	FAIL	The remember index value must be in the range of 1 to 10
	endc
	iflt	\3-1
	FAIL	User defined value must be in the range of 1 to 65535
	endc
	ifgt	\3-65535
	FAIL	User defined value must be in the range of 1 to 65535
	endc
	db	ifRemembered
	db	\1
	db	\2
	dw	\3
conditionCount	set	conditionCount+1
	ENDM

*********

if_020	MACRO	0,0
		db	if_is020
conditionCount	set	conditionCount+1
	ENDM

*********
