/* * Copyright (c) 1987 Fujitsu * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /* actions.c -- Actions associated with various machine instruction classes (68000 version). Chip Morningstar -- Lucasfilm Ltd. 26-April-1985 */ #include "macrossTypes.h" #include "macrossGlobals.h" #define binary opcode->opcode #define altBinary opcode->subClass #define sub opcode->subClass #define mode(n) ((evaluatedOperands[n])->addressMode) #define trueMode(n) (operandKindField(mode(n))) #define address(n) (evaluatedOperands[n]->value) #define forward(n) (!isDefined(evaluatedOperands[n])) #define register(n) (getRegister(mode(n))) #define controlRegister(n) (trueMode(n)==USP_REGISTER_OPND ? \ 0x800 : (register(n) == SFC_REGISTER ? \ 0x000 : (register(n) == DFC_REGISTER ? \ 0x001 : 0x801))) #define eaPC(n) (effectiveAddressBits(mode(n), address(n), \ forward(n), TRUE)) #define eaNoPC(n) (effectiveAddressBits(mode(n), address(n), \ forward(n), FALSE)) #define eaFlop(n) (eaNoPC(n)>>3 | (eaNoPC(n)&7)<<3) #define extendPC(n) emitPossibleExtension(address(n), mode(n), binary, n,\ forward(n), TRUE) #define extendNoPC(n) emitPossibleExtension(address(n), mode(n), binary, n,\ forward(n), FALSE) #define check(g, n) ((operandClassTable[(int)trueMode(n)] &\ g) != 0) #define defcheck(n) isDefined(evaluatedOperands[n]) #define check1(g) (numberOfOperands==1 && check(g, 0)) #define check2(g1, g2) (numberOfOperands==2 && check(g1, 0) && check(g2, 1)) /*#define check1(g) (numberOfOperands==1 && check(g, 0) && defcheck(0)) #define check2(g1, g2) (numberOfOperands==2 && check(g1, 0) && check(g2, 1)\ && defcheck(0) && defcheck(1))*/ #define iifCheck0() if (numberOfOperands==0) { #define iifCheck1(g) if (check1(g)) { #define eifCheck1(g) } else if (check1(g)) { #define iifCheck2(g1, g2) if (check2(g1, g2)) { #define eifCheck2(g1, g2) } else if (check2(g1, g2)) { #define done } else {\ error(INAPPROPRIATE_ADDRESS_MODES_ERROR);\ } #define D_TO_EA_BIT 0x0100 #define EA_TO_D_BIT 0x0000 #define R_SHIFT 9 #define Q_DATA_SHIFT 9 #define OP_SIZE_BITS 0x00C0 #define BYTE_OP_SIZE_BITS 0x0000 #define WORD_OP_SIZE_BITS 0x0040 #define LONG_OP_SIZE_BITS 0x0080 #define byteOp(op) (((op) & OP_SIZE_BITS) == BYTE_OP_SIZE_BITS) #define wordOp(op) (((op) & OP_SIZE_BITS) == WORD_OP_SIZE_BITS) #define longOp(op) (((op) & OP_SIZE_BITS) == LONG_OP_SIZE_BITS) #define MOVE_OP_SIZE_BITS 0x3000 #define MOVE_BYTE_OP_SIZE_BITS 0x1000 #define MOVE_WORD_OP_SIZE_BITS 0x3000 #define MOVE_LONG_OP_SIZE_BITS 0x2000 #define moveByteOp(op) (((op) & MOVE_OP_SIZE_BITS) == \ MOVE_BYTE_OP_SIZE_BITS) #define moveWordOp(op) (((op) & MOVE_OP_SIZE_BITS) == \ MOVE_WORD_OP_SIZE_BITS) #define moveLongOp(op) (((op) & MOVE_OP_SIZE_BITS) == \ MOVE_LONG_OP_SIZE_BITS) #define isMoveOp(op) ((op)==0x1000 || (op)==0x2000 || (op)==0x3000) #define byteOpGeneral(op) (isMoveOp(op) ? moveByteOp(op) : byteOp(op)) #define wordOpGeneral(op) (isMoveOp(op) ? moveWordOp(op) : wordOp(op)) void emitPossibleExtension(); #define emitImmediate(n) \ if (byteOp(binary)) {\ if (byteCheck(address(n))) {\ emitByte(0);\ putFixupsHere(BYTE_FIXUP, n);\ emitByte(address(n));\ }\ } else if (wordOp(binary)) {\ if (wordCheck(address(n))) {\ putFixupsHere(WORD_FIXUP, n);\ emitWord(address(n));\ }\ } else {\ putFixupsHere(LONG_FIXUP, n);\ emitLong(address(n));\ } /* These routines are vectored off of the opcode lookup table. Each instruction is of a particular category that defines which address modes it accepts its operands in and what size the operands are. There is one "actionsXXXX" routine for each of these categories that grabs the operands, checks their address modes, and emits the binary instruction and operands. */ /* Class I: abcd, addxb, addxw, addxl, sbcd, subxb, subxw, subxl */ void actionsClassI(opcode, numberOfOperands, evaluatedOperands) opcodeTableEntryType *opcode; int numberOfOperands; valueType *evaluatedOperands[]; { #define D_REGISTER_BIT_I 0x0000 #define PREDECREMENT_BIT_I 0x0008 iifCheck2(GROUP_A, GROUP_A) emitWord(binary | register(1)<data */ emitWord(binary | register(0)<addr */ emitWord(binary | register(0)<data */ emitWord(binary | register(1)<addr */ emitWord(binary | register(0)<kindOfValue==ABSOLUTE_VALUE && isByte(value->value)); } bool isWord(value) int value; { return (-32769kindOfValue!=UNDEFINED_VALUE); } #define D_REGISTER_EA 0x0000 #define A_REGISTER_EA 0x0008 #define A_REGISTER_INDIRECT_EA 0x0010 #define POSTINCREMENT_EA 0x0018 #define PREDECREMENT_EA 0x0020 #define DISPLACEMENT_EA 0x0028 #define SELECTED_EA 0x0028 #define INDEXED_EA 0x0030 #define INDEX_SELECTED_EA 0x0030 #define PC_DISPLACEMENT_EA 0x003A #define PC_INDEXED_EA 0x003B #define IMMEDIATE_EA 0x003C #define ABSOLUTE_SHORT_EA 0x0038 #define ABSOLUTE_LONG_EA 0x0039 int effectiveAddressBits(operandKind, value, isForwardRef, pcRelativeOK) operandKindType operandKind; int value; bool isForwardRef; bool pcRelativeOK; { switch(operandKindField(operandKind)) { case D_REGISTER_OPND: return(D_REGISTER_EA | getRegister(operandKind)); break; case A_REGISTER_OPND: return(A_REGISTER_EA | getRegister(operandKind)); break; case A_REGISTER_INDIRECT_OPND: return(A_REGISTER_INDIRECT_EA | getRegister(operandKind)); break; case POSTINCREMENT_OPND: return(POSTINCREMENT_EA | getRegister(operandKind)); break; case PREDECREMENT_OPND: return(PREDECREMENT_EA | getRegister(operandKind)); break; case DISPLACEMENT_OPND: return(DISPLACEMENT_EA | getRegister(operandKind)); break; case SELECTED_OPND: return(SELECTED_EA | getRegister(operandKind)); break; case INDEXED_OPND: return(INDEXED_EA | getRegister(operandKind)); break; case INDEX_SELECTED_OPND: return(INDEX_SELECTED_EA | getRegister(operandKind)); break; case PC_DISPLACEMENT_OPND: return(PC_DISPLACEMENT_EA); break; case PC_INDEXED_OPND: return(PC_INDEXED_EA); break; case IMMEDIATE_OPND: return(IMMEDIATE_EA); break; case ABSOLUTE_SHORT_OPND: return(ABSOLUTE_SHORT_EA); break; case ABSOLUTE_LONG_OPND: return(ABSOLUTE_LONG_EA); break; case EXPRESSION_OPND: if (pcRelativeOK && isWordOffset(value - (currentLocationCounter.value - targetOffset + 2)) && !isForwardRef) return(PC_DISPLACEMENT_EA); else if (isWord(value) && !isForwardRef) return(ABSOLUTE_SHORT_EA); else return(ABSOLUTE_LONG_EA); break; case STRING_OPND: botch("string opnd to get ea\n"); break; case BLOCK_OPND: botch("block opnd to get ea\n"); break; case CC_REGISTER_OPND: case STATUS_REGISTER_OPND: case USP_REGISTER_OPND: case CONTROL_REGISTER_OPND: botch("funny opnd to get ea\n"); break; default: botch("non-existant operand kind to get ea: %x\n", operandKind); break; } } void emitPossibleExtension(value, addressMode, opcode, operandNumber, isForwardRef, pcRelativeOK) int value; operandKindType addressMode; int opcode; int operandNumber; bool isForwardRef; bool pcRelativeOK; { switch (operandKindField(addressMode)) { case DISPLACEMENT_OPND: case SELECTED_OPND: case ABSOLUTE_SHORT_OPND: case PC_DISPLACEMENT_OPND: putFixupsHere(WORD_FIXUP, operandNumber); wordCheck(value); emitWord(value); break; case INDEXED_OPND: case INDEX_SELECTED_OPND: case PC_INDEXED_OPND: emitByte(indexByte(addressMode)); putFixupsHere(BYTE_FIXUP,operandNumber); byteCheck(value); emitByte(value); break; case ABSOLUTE_LONG_OPND: putFixupsHere(LONG_FIXUP, operandNumber); emitLong(value); break; case IMMEDIATE_OPND: if (byteOpGeneral(opcode)) { emitByte(0); putFixupsHere(BYTE_FIXUP, operandNumber); byteCheck(value); emitByte(value); } else if (wordOpGeneral(opcode)) { putFixupsHere(WORD_FIXUP, operandNumber); wordCheck(value); emitWord(value); } else { putFixupsHere(LONG_FIXUP, operandNumber); emitLong(value); } break; case EXPRESSION_OPND: if (pcRelativeOK && isWordOffset(value - (currentLocationCounter.value - targetOffset)) && !isForwardRef) { putFixupsHere(WORD_RELATIVE_FIXUP, operandNumber); emitWord(value - (currentLocationCounter.value - targetOffset)); } else if (isWord(value) && !isForwardRef) { putFixupsHere(WORD_FIXUP, operandNumber); emitWord(value); } else { putFixupsHere(LONG_FIXUP, operandNumber); emitLong(value); } break; default: break; } } int indexByte(operandKind) operandKindType operandKind; { return((getIndexRegister(operandKind)<<4) | (getWL(operandKind)<<3)); }