2001-05-05 16:12:47 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* codeinfo.c */
|
|
|
|
/* */
|
|
|
|
/* Additional information about 6502 code */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* (C) 2001 Ullrich von Bassewitz */
|
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@cc65.org */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-05-05 21:42:58 +00:00
|
|
|
/* common */
|
|
|
|
#include "coll.h"
|
|
|
|
|
2001-05-05 16:12:47 +00:00
|
|
|
/* cc65 */
|
2001-05-15 19:42:17 +00:00
|
|
|
#include "codeent.h"
|
|
|
|
#include "codeseg.h"
|
2001-05-20 20:42:00 +00:00
|
|
|
#include "datatype.h"
|
2001-05-15 19:42:17 +00:00
|
|
|
#include "error.h"
|
2001-05-20 20:42:00 +00:00
|
|
|
#include "symtab.h"
|
2001-05-05 16:12:47 +00:00
|
|
|
#include "codeinfo.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Data */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Table listing the function names and code info values for known internally
|
|
|
|
* used functions. This table should get auto-generated in the future.
|
|
|
|
*/
|
|
|
|
typedef struct FuncInfo FuncInfo;
|
|
|
|
struct FuncInfo {
|
|
|
|
const char* Name; /* Function name */
|
2001-09-21 19:34:57 +00:00
|
|
|
unsigned short Use; /* Register usage */
|
2001-09-21 21:52:50 +00:00
|
|
|
unsigned short Chg; /* Changed/destroyed registers */
|
2001-05-05 16:12:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const FuncInfo FuncInfoTable[] = {
|
2001-09-23 09:20:55 +00:00
|
|
|
{ "addysp", REG_Y, REG_NONE },
|
|
|
|
{ "aslax1", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "aslax2", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "aslax3", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "aslax4", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "bnega", REG_A, REG_AX },
|
|
|
|
{ "bnegax", REG_AX, REG_AX },
|
|
|
|
{ "bnegeax", REG_EAX, REG_EAX },
|
|
|
|
{ "booleq", REG_NONE, REG_AX },
|
|
|
|
{ "boolge", REG_NONE, REG_AX },
|
|
|
|
{ "boolgt", REG_NONE, REG_AX },
|
|
|
|
{ "boolle", REG_NONE, REG_AX },
|
|
|
|
{ "boollt", REG_NONE, REG_AX },
|
|
|
|
{ "boolne", REG_NONE, REG_AX },
|
|
|
|
{ "booluge", REG_NONE, REG_AX },
|
|
|
|
{ "boolugt", REG_NONE, REG_AX },
|
|
|
|
{ "boolule", REG_NONE, REG_AX },
|
|
|
|
{ "boolult", REG_NONE, REG_AX },
|
|
|
|
{ "complax", REG_AX, REG_AX },
|
|
|
|
{ "decax1", REG_AX, REG_AX },
|
|
|
|
{ "decax2", REG_AX, REG_AX },
|
|
|
|
{ "decax3", REG_AX, REG_AX },
|
|
|
|
{ "decax4", REG_AX, REG_AX },
|
|
|
|
{ "decax5", REG_AX, REG_AX },
|
|
|
|
{ "decax6", REG_AX, REG_AX },
|
|
|
|
{ "decax7", REG_AX, REG_AX },
|
|
|
|
{ "decax8", REG_AX, REG_AX },
|
|
|
|
{ "decaxy", REG_AXY, REG_AX | REG_TMP1 },
|
|
|
|
{ "decsp1", REG_NONE, REG_Y },
|
|
|
|
{ "decsp2", REG_NONE, REG_A },
|
|
|
|
{ "decsp3", REG_NONE, REG_A },
|
|
|
|
{ "decsp4", REG_NONE, REG_A },
|
|
|
|
{ "decsp5", REG_NONE, REG_A },
|
|
|
|
{ "decsp6", REG_NONE, REG_A },
|
|
|
|
{ "decsp7", REG_NONE, REG_A },
|
|
|
|
{ "decsp8", REG_NONE, REG_A },
|
|
|
|
{ "incax1", REG_AX, REG_AX },
|
|
|
|
{ "incax2", REG_AX, REG_AX },
|
|
|
|
{ "incsp1", REG_NONE, REG_NONE },
|
|
|
|
{ "incsp2", REG_NONE, REG_Y },
|
|
|
|
{ "incsp3", REG_NONE, REG_Y },
|
|
|
|
{ "incsp4", REG_NONE, REG_Y },
|
|
|
|
{ "incsp5", REG_NONE, REG_Y },
|
|
|
|
{ "incsp6", REG_NONE, REG_Y },
|
|
|
|
{ "incsp7", REG_NONE, REG_Y },
|
|
|
|
{ "incsp8", REG_NONE, REG_Y },
|
2001-09-25 10:30:48 +00:00
|
|
|
{ "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
|
|
|
|
{ "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
|
|
|
|
{ "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
|
2001-09-23 09:20:55 +00:00
|
|
|
{ "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
|
|
|
|
{ "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
|
|
|
|
{ "ldax0sp", REG_Y, REG_AX },
|
|
|
|
{ "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
|
|
|
|
{ "ldaxidx", REG_AXY, REG_AX | REG_PTR1 },
|
|
|
|
{ "ldaxysp", REG_Y, REG_AX },
|
|
|
|
{ "leaasp", REG_A, REG_AX },
|
|
|
|
{ "negax", REG_AX, REG_AX },
|
|
|
|
{ "pusha", REG_A, REG_Y },
|
|
|
|
{ "pusha0", REG_A, REG_XY },
|
|
|
|
{ "pushax", REG_AX, REG_Y },
|
|
|
|
{ "pusheax", REG_EAX, REG_Y },
|
|
|
|
{ "pushw0sp", REG_NONE, REG_AXY },
|
|
|
|
{ "pushwysp", REG_Y, REG_AXY },
|
|
|
|
{ "shlax1", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shlax2", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shlax3", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shlax4", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shrax1", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shrax2", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shrax3", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shrax4", REG_AX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shreax1", REG_EAX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shreax2", REG_EAX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shreax3", REG_EAX, REG_AX | REG_TMP1 },
|
|
|
|
{ "shreax4", REG_EAX, REG_AX | REG_TMP1 },
|
|
|
|
{ "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
|
|
|
|
{ "stax0sp", REG_AX, REG_Y },
|
2001-09-25 10:30:48 +00:00
|
|
|
{ "staxysp", REG_AXY, REG_Y },
|
2001-09-23 09:20:55 +00:00
|
|
|
{ "tosicmp", REG_AX, REG_AXY | REG_SREG },
|
|
|
|
{ "tosdiva0", REG_AX, REG_ALL },
|
|
|
|
{ "tosdivax", REG_AX, REG_ALL },
|
|
|
|
{ "tosdiveax", REG_EAX, REG_ALL },
|
|
|
|
{ "tosmula0", REG_AX, REG_ALL },
|
|
|
|
{ "tosmulax", REG_AX, REG_ALL },
|
|
|
|
{ "tosmuleax", REG_EAX, REG_ALL },
|
|
|
|
{ "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
|
|
|
|
{ "tosumula0", REG_AX, REG_ALL },
|
|
|
|
{ "tosumulax", REG_AX, REG_ALL },
|
|
|
|
{ "tosumuleax", REG_EAX, REG_ALL },
|
2001-07-25 21:51:40 +00:00
|
|
|
};
|
2001-05-05 16:12:47 +00:00
|
|
|
#define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
|
|
|
|
|
2001-05-21 20:05:52 +00:00
|
|
|
/* Table with names of zero page locations used by the compiler */
|
2001-05-22 07:11:22 +00:00
|
|
|
static const ZPInfo ZPInfoTable[] = {
|
2001-09-25 10:30:48 +00:00
|
|
|
{ 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
|
|
|
|
{ 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
|
|
|
|
{ 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
|
|
|
|
{ 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
|
|
|
|
{ 4, "ptr3", REG_NONE, REG_NONE },
|
|
|
|
{ 4, "ptr4", REG_NONE, REG_NONE },
|
|
|
|
{ 7, "regbank", REG_NONE, REG_NONE },
|
|
|
|
{ 0, "regsave", REG_SAVE_LO, REG_SAVE },
|
|
|
|
{ 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
|
|
|
|
{ 2, "sp", REG_NONE, REG_NONE },
|
|
|
|
{ 0, "sreg", REG_SREG_LO, REG_SREG },
|
|
|
|
{ 0, "sreg+1", REG_SREG_HI, REG_SREG },
|
|
|
|
{ 0, "tmp1", REG_TMP1, REG_TMP1 },
|
|
|
|
{ 0, "tmp2", REG_NONE, REG_NONE },
|
|
|
|
{ 0, "tmp3", REG_NONE, REG_NONE },
|
|
|
|
{ 0, "tmp4", REG_NONE, REG_NONE },
|
2001-05-22 07:11:22 +00:00
|
|
|
};
|
|
|
|
#define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
|
2001-05-05 16:12:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-21 21:52:50 +00:00
|
|
|
/* Code */
|
2001-05-05 16:12:47 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int CompareFuncInfo (const void* Key, const void* Info)
|
|
|
|
/* Compare function for bsearch */
|
|
|
|
{
|
|
|
|
return strcmp (Key, ((const FuncInfo*) Info)->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-21 19:34:57 +00:00
|
|
|
void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
|
2001-05-20 18:51:12 +00:00
|
|
|
/* For the given function, lookup register information and store it into
|
|
|
|
* the given variables. If the function is unknown, assume it will use and
|
|
|
|
* load all registers.
|
2001-05-05 16:12:47 +00:00
|
|
|
*/
|
|
|
|
{
|
2001-05-20 20:42:00 +00:00
|
|
|
/* If the function name starts with an underline, it is an external
|
|
|
|
* function. Search for it in the symbol table. If the function does
|
|
|
|
* not start with an underline, it may be a runtime support function.
|
|
|
|
* Search for it in the list of builtin functions.
|
|
|
|
*/
|
|
|
|
if (Name[0] == '_') {
|
|
|
|
|
|
|
|
/* Search in the symbol table, skip the leading underscore */
|
2001-05-22 13:58:56 +00:00
|
|
|
SymEntry* E = FindGlobalSym (Name+1);
|
2001-05-20 20:42:00 +00:00
|
|
|
|
|
|
|
/* Did we find it in the top level table? */
|
2001-05-22 13:58:56 +00:00
|
|
|
if (E && IsTypeFunc (E->Type)) {
|
2001-05-28 06:30:03 +00:00
|
|
|
|
2001-05-20 20:42:00 +00:00
|
|
|
/* A function may use the A or A/X registers if it is a fastcall
|
2001-05-21 06:43:46 +00:00
|
|
|
* function. If it is not a fastcall function but a variadic one,
|
|
|
|
* it will use the Y register (the parameter size is passed here).
|
2001-05-21 20:05:52 +00:00
|
|
|
* In all other cases, no registers are used. However, we assume
|
2001-05-21 06:43:46 +00:00
|
|
|
* that any function will destroy all registers.
|
2001-05-20 20:42:00 +00:00
|
|
|
*/
|
|
|
|
FuncDesc* D = E->V.F.Func;
|
|
|
|
if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
|
|
|
|
/* Will use registers depending on the last param */
|
|
|
|
SymEntry* LastParam = D->SymTab->SymTail;
|
|
|
|
if (SizeOf (LastParam->Type) == 1) {
|
|
|
|
*Use = REG_A;
|
|
|
|
} else {
|
|
|
|
*Use = REG_AX;
|
|
|
|
}
|
2001-05-21 06:43:46 +00:00
|
|
|
} else if ((D->Flags & FD_VARIADIC) != 0) {
|
|
|
|
*Use = REG_Y;
|
2001-05-21 20:05:52 +00:00
|
|
|
} else {
|
2001-05-20 20:42:00 +00:00
|
|
|
/* Will not use any registers */
|
|
|
|
*Use = REG_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Will destroy all registers */
|
2001-09-21 21:52:50 +00:00
|
|
|
*Chg = REG_ALL;
|
2001-05-20 20:42:00 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-05-05 16:12:47 +00:00
|
|
|
} else {
|
2001-05-20 20:42:00 +00:00
|
|
|
|
2001-09-24 22:36:35 +00:00
|
|
|
/* Search for the function in the list of builtin functions */
|
2001-05-20 20:42:00 +00:00
|
|
|
const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
|
|
|
|
sizeof(FuncInfo), CompareFuncInfo);
|
|
|
|
|
|
|
|
/* Do we know the function? */
|
|
|
|
if (Info) {
|
|
|
|
/* Use the information we have */
|
|
|
|
*Use = Info->Use;
|
|
|
|
*Chg = Info->Chg;
|
|
|
|
return;
|
|
|
|
}
|
2001-05-05 16:12:47 +00:00
|
|
|
}
|
2001-05-20 20:42:00 +00:00
|
|
|
|
2001-09-25 10:30:48 +00:00
|
|
|
/* Function not found - assume that the primary register is input, and all
|
2001-09-21 21:52:50 +00:00
|
|
|
* registers are changed
|
|
|
|
*/
|
2001-09-25 10:30:48 +00:00
|
|
|
*Use = REG_EAXY;
|
2001-09-21 21:52:50 +00:00
|
|
|
*Chg = REG_ALL;
|
2001-05-05 16:12:47 +00:00
|
|
|
}
|
|
|
|
|
2001-05-05 21:42:58 +00:00
|
|
|
|
2001-05-21 20:05:52 +00:00
|
|
|
|
2001-09-23 09:20:55 +00:00
|
|
|
static int CompareZPInfo (const void* Name, const void* Info)
|
|
|
|
/* Compare function for bsearch */
|
|
|
|
{
|
|
|
|
/* Cast the pointers to the correct data type */
|
|
|
|
const char* N = (const char*) Name;
|
|
|
|
const ZPInfo* E = (const ZPInfo*) Info;
|
|
|
|
|
|
|
|
/* Do the compare. Be careful because of the length (Info may contain
|
|
|
|
* more than just the zeropage name).
|
|
|
|
*/
|
2001-09-24 22:36:35 +00:00
|
|
|
if (E->Len == 0) {
|
|
|
|
/* Do a full compare */
|
|
|
|
return strcmp (N, E->Name);
|
|
|
|
} else {
|
|
|
|
/* Only compare the first part */
|
|
|
|
int Res = strncmp (N, E->Name, E->Len);
|
|
|
|
if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
|
|
|
|
/* Name is actually longer than Info->Name */
|
|
|
|
Res = -1;
|
|
|
|
}
|
|
|
|
return Res;
|
2001-09-23 09:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-25 10:30:48 +00:00
|
|
|
const ZPInfo* GetZPInfo (const char* Name)
|
|
|
|
/* If the given name is a zero page symbol, return a pointer to the info
|
|
|
|
* struct for this symbol, otherwise return NULL.
|
2001-09-21 21:52:50 +00:00
|
|
|
*/
|
2001-05-21 20:05:52 +00:00
|
|
|
{
|
2001-09-23 09:20:55 +00:00
|
|
|
/* Search for the zp location in the list */
|
2001-09-25 10:30:48 +00:00
|
|
|
return bsearch (Name, ZPInfoTable, ZPInfoCount,
|
|
|
|
sizeof(ZPInfo), CompareZPInfo);
|
2001-05-21 20:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-05 21:42:58 +00:00
|
|
|
|
2001-09-21 19:34:57 +00:00
|
|
|
static unsigned GetRegInfo2 (CodeSeg* S,
|
|
|
|
CodeEntry* E,
|
|
|
|
int Index,
|
|
|
|
Collection* Visited,
|
|
|
|
unsigned Used,
|
2001-09-24 22:36:35 +00:00
|
|
|
unsigned Unused,
|
|
|
|
unsigned Wanted)
|
2001-05-15 19:42:17 +00:00
|
|
|
/* Recursively called subfunction for GetRegInfo. */
|
|
|
|
{
|
|
|
|
/* Follow the instruction flow recording register usage. */
|
2001-05-05 21:42:58 +00:00
|
|
|
while (1) {
|
|
|
|
|
2001-09-21 19:34:57 +00:00
|
|
|
unsigned R;
|
2001-05-15 19:42:17 +00:00
|
|
|
|
|
|
|
/* Check if we have already visited the current code entry. If so,
|
|
|
|
* bail out.
|
|
|
|
*/
|
2001-05-31 21:08:49 +00:00
|
|
|
if (CE_HasMark (E)) {
|
2001-05-15 19:42:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark this entry as already visited */
|
2001-05-31 21:08:49 +00:00
|
|
|
CE_SetMark (E);
|
2001-05-15 19:42:17 +00:00
|
|
|
CollAppend (Visited, E);
|
|
|
|
|
|
|
|
/* Evaluate the used registers */
|
2001-05-15 22:35:38 +00:00
|
|
|
R = E->Use;
|
2001-05-28 06:30:03 +00:00
|
|
|
if (E->OPC == OP65_RTS ||
|
2001-05-15 22:35:38 +00:00
|
|
|
((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
|
|
|
|
/* This instruction will leave the function */
|
|
|
|
R |= S->ExitRegs;
|
|
|
|
}
|
|
|
|
if (R != REG_NONE) {
|
2001-05-15 19:42:17 +00:00
|
|
|
/* We are not interested in the use of any register that has been
|
|
|
|
* used before.
|
|
|
|
*/
|
|
|
|
R &= ~Unused;
|
|
|
|
/* Remember the remaining registers */
|
|
|
|
Used |= R;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Evaluate the changed registers */
|
|
|
|
if ((R = E->Chg) != REG_NONE) {
|
|
|
|
/* We are not interested in the use of any register that has been
|
|
|
|
* used before.
|
|
|
|
*/
|
|
|
|
R &= ~Used;
|
|
|
|
/* Remember the remaining registers */
|
|
|
|
Unused |= R;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we know about all registers now, bail out */
|
2001-09-24 22:36:35 +00:00
|
|
|
if (((Used | Unused) & Wanted) == Wanted) {
|
2001-05-05 21:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-05-15 19:42:17 +00:00
|
|
|
|
|
|
|
/* If the instruction is an RTS or RTI, we're done */
|
2001-06-04 12:27:16 +00:00
|
|
|
if ((E->Info & OF_RET) != 0) {
|
2001-05-15 19:42:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have an unconditional branch, follow this branch if possible,
|
|
|
|
* otherwise we're done.
|
|
|
|
*/
|
|
|
|
if ((E->Info & OF_UBRA) != 0) {
|
|
|
|
|
|
|
|
/* Does this jump have a valid target? */
|
|
|
|
if (E->JumpTo) {
|
|
|
|
|
|
|
|
/* Unconditional jump */
|
|
|
|
E = E->JumpTo->Owner;
|
2001-09-24 22:36:35 +00:00
|
|
|
Index = -1; /* Invalidate */
|
2001-05-15 19:42:17 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Jump outside means we're done */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of conditional branches, follow the branch if possible and
|
|
|
|
* follow the normal flow (branch not taken) afterwards. If we cannot
|
|
|
|
* follow the branch, we're done.
|
|
|
|
*/
|
|
|
|
} else if ((E->Info & OF_CBRA) != 0) {
|
|
|
|
|
|
|
|
if (E->JumpTo) {
|
|
|
|
|
|
|
|
/* Recursively determine register usage at the branch target */
|
2001-09-21 19:34:57 +00:00
|
|
|
unsigned U1;
|
|
|
|
unsigned U2;
|
2001-05-15 19:42:17 +00:00
|
|
|
|
2001-09-24 22:36:35 +00:00
|
|
|
U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
|
2001-05-15 19:42:17 +00:00
|
|
|
if (U1 == REG_AXY) {
|
|
|
|
/* All registers used, no need for second call */
|
|
|
|
return REG_AXY;
|
|
|
|
}
|
|
|
|
if (Index < 0) {
|
2001-05-31 21:08:49 +00:00
|
|
|
Index = CS_GetEntryIndex (S, E);
|
2001-06-04 12:27:16 +00:00
|
|
|
}
|
2001-05-31 21:08:49 +00:00
|
|
|
if ((E = CS_GetEntry (S, ++Index)) == 0) {
|
2001-05-15 19:42:17 +00:00
|
|
|
Internal ("GetRegInfo2: No next entry!");
|
|
|
|
}
|
2001-09-24 22:36:35 +00:00
|
|
|
U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
|
2001-05-15 19:42:17 +00:00
|
|
|
return U1 | U2; /* Used in any of the branches */
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Jump to global symbol */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Just go to the next instruction */
|
|
|
|
if (Index < 0) {
|
2001-05-31 21:08:49 +00:00
|
|
|
Index = CS_GetEntryIndex (S, E);
|
2001-05-15 19:42:17 +00:00
|
|
|
}
|
2001-05-31 21:08:49 +00:00
|
|
|
E = CS_GetEntry (S, ++Index);
|
2001-05-15 19:42:17 +00:00
|
|
|
if (E == 0) {
|
|
|
|
/* No next entry */
|
|
|
|
Internal ("GetRegInfo2: No next entry!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-05-05 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return to the caller the complement of all unused registers */
|
2001-05-15 19:42:17 +00:00
|
|
|
return Used;
|
2001-05-05 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-21 19:34:57 +00:00
|
|
|
static unsigned GetRegInfo1 (CodeSeg* S,
|
|
|
|
CodeEntry* E,
|
|
|
|
int Index,
|
|
|
|
Collection* Visited,
|
|
|
|
unsigned Used,
|
2001-09-24 22:36:35 +00:00
|
|
|
unsigned Unused,
|
|
|
|
unsigned Wanted)
|
2001-05-15 19:42:17 +00:00
|
|
|
/* Recursively called subfunction for GetRegInfo. */
|
2001-05-05 21:42:58 +00:00
|
|
|
{
|
|
|
|
/* Remember the current count of the line collection */
|
2001-05-15 19:42:17 +00:00
|
|
|
unsigned Count = CollCount (Visited);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
|
|
|
/* Call the worker routine */
|
2001-09-24 22:36:35 +00:00
|
|
|
unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
|
2001-05-15 19:42:17 +00:00
|
|
|
|
|
|
|
/* Restore the old count, unmarking all new entries */
|
|
|
|
unsigned NewCount = CollCount (Visited);
|
|
|
|
while (NewCount-- > Count) {
|
|
|
|
CodeEntry* E = CollAt (Visited, NewCount);
|
2001-05-31 21:08:49 +00:00
|
|
|
CE_ResetMark (E);
|
2001-05-15 19:42:17 +00:00
|
|
|
CollDelete (Visited, NewCount);
|
|
|
|
}
|
2001-05-05 21:42:58 +00:00
|
|
|
|
2001-05-15 19:42:17 +00:00
|
|
|
/* Return the registers used */
|
2001-05-05 21:42:58 +00:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-24 22:36:35 +00:00
|
|
|
unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
|
2001-05-15 19:42:17 +00:00
|
|
|
/* Determine register usage information for the instructions starting at the
|
|
|
|
* given index.
|
2001-05-05 21:42:58 +00:00
|
|
|
*/
|
|
|
|
{
|
2001-09-21 19:34:57 +00:00
|
|
|
CodeEntry* E;
|
|
|
|
Collection Visited; /* Visited entries */
|
|
|
|
unsigned R;
|
2001-05-15 19:42:17 +00:00
|
|
|
|
|
|
|
/* Get the code entry for the given index */
|
2001-05-31 21:08:49 +00:00
|
|
|
if (Index >= CS_GetEntryCount (S)) {
|
2001-05-15 19:42:17 +00:00
|
|
|
/* There is no such code entry */
|
|
|
|
return REG_NONE;
|
|
|
|
}
|
2001-05-31 21:08:49 +00:00
|
|
|
E = CS_GetEntry (S, Index);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
2001-05-15 19:42:17 +00:00
|
|
|
/* Initialize the data structure used to collection information */
|
|
|
|
InitCollection (&Visited);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
|
|
|
/* Call the recursive subfunction */
|
2001-09-24 22:36:35 +00:00
|
|
|
R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
|
|
|
/* Delete the line collection */
|
2001-05-15 19:42:17 +00:00
|
|
|
DoneCollection (&Visited);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
|
|
|
/* Return the registers used */
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-15 19:42:17 +00:00
|
|
|
int RegAUsed (struct CodeSeg* S, unsigned Index)
|
2001-05-05 21:42:58 +00:00
|
|
|
/* Check if the value in A is used. */
|
|
|
|
{
|
2001-09-24 22:36:35 +00:00
|
|
|
return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
|
2001-05-05 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-15 19:42:17 +00:00
|
|
|
int RegXUsed (struct CodeSeg* S, unsigned Index)
|
2001-05-05 21:42:58 +00:00
|
|
|
/* Check if the value in X is used. */
|
|
|
|
{
|
2001-09-24 22:36:35 +00:00
|
|
|
return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
|
2001-05-05 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-15 19:42:17 +00:00
|
|
|
int RegYUsed (struct CodeSeg* S, unsigned Index)
|
2001-05-05 21:42:58 +00:00
|
|
|
/* Check if the value in Y is used. */
|
|
|
|
{
|
2001-09-24 22:36:35 +00:00
|
|
|
return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
|
2001-05-05 21:42:58 +00:00
|
|
|
}
|
2001-05-05 16:12:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-31 21:08:49 +00:00
|
|
|
|
2001-09-21 19:34:57 +00:00
|
|
|
|