?? labels.c
字號:
/* labels.c label handling for the Netwide Assembler
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "nasm.h"
#include "nasmlib.h"
/*
* A local label is one that begins with exactly one period. Things
* that begin with _two_ periods are NASM-specific things.
*
* If TASM compatibility is enabled, a local label can also begin with
* @@, so @@local is a TASM compatible local label. Note that we only
* check for the first @ symbol, although TASM requires both.
*/
#define islocal(l) \
(tasm_compatible_mode ? \
(((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
((l)[0] == '.' && (l)[1] != '.'))
#define islocalchar(c) \
(tasm_compatible_mode ? \
((c) == '.' || (c) == '@') : \
((c) == '.'))
#define LABEL_BLOCK 32 /* no. of labels/block */
#define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
#define LABEL_HASHES 37 /* no. of hash table entries */
#define END_LIST -3 /* don't clash with NO_SEG! */
#define END_BLOCK -2
#define BOGUS_VALUE -4
#define PERMTS_SIZE 4096 /* size of text blocks */
#if (PERMTS_SIZE > IDLEN_MAX)
#error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
#endif
/* values for label.defn.is_global */
#define DEFINED_BIT 1
#define GLOBAL_BIT 2
#define EXTERN_BIT 4
#define NOT_DEFINED_YET 0
#define TYPE_MASK 3
#define LOCAL_SYMBOL (DEFINED_BIT)
#define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
#define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
union label { /* actual label structures */
struct {
long segment, offset;
char *label, *special;
int is_global, is_norm;
} defn;
struct {
long movingon, dummy;
union label *next;
} admin;
};
struct permts { /* permanent text storage */
struct permts *next; /* for the linked list */
int size, usage; /* size and used space in ... */
char data[PERMTS_SIZE]; /* ... the data block itself */
};
extern int global_offset_changed; /* defined in nasm.c */
static union label *ltab[LABEL_HASHES]; /* using a hash table */
static union label *lfree[LABEL_HASHES]; /* pointer into the above */
static struct permts *perm_head; /* start of perm. text storage */
static struct permts *perm_tail; /* end of perm. text storage */
static void init_block(union label *blk);
static char *perm_copy(char *string1, char *string2);
static char *prevlabel;
static int initialised = FALSE;
char lprefix[PREFIX_MAX] = { 0 };
char lpostfix[PREFIX_MAX] = { 0 };
/*
* Internal routine: finds the `union label' corresponding to the
* given label name. Creates a new one, if it isn't found, and if
* `create' is TRUE.
*/
static union label *find_label(char *label, int create)
{
int hash = 0;
char *p, *prev;
int prevlen;
union label *lptr;
if (islocal(label))
prev = prevlabel;
else
prev = "";
prevlen = strlen(prev);
p = prev;
while (*p)
hash += *p++;
p = label;
while (*p)
hash += *p++;
hash %= LABEL_HASHES;
lptr = ltab[hash];
while (lptr->admin.movingon != END_LIST) {
if (lptr->admin.movingon == END_BLOCK) {
lptr = lptr->admin.next;
if (!lptr)
break;
}
if (!strncmp(lptr->defn.label, prev, prevlen) &&
!strcmp(lptr->defn.label + prevlen, label))
return lptr;
lptr++;
}
if (create) {
if (lfree[hash]->admin.movingon == END_BLOCK) {
/*
* must allocate a new block
*/
lfree[hash]->admin.next =
(union label *)nasm_malloc(LBLK_SIZE);
lfree[hash] = lfree[hash]->admin.next;
init_block(lfree[hash]);
}
lfree[hash]->admin.movingon = BOGUS_VALUE;
lfree[hash]->defn.label = perm_copy(prev, label);
lfree[hash]->defn.special = NULL;
lfree[hash]->defn.is_global = NOT_DEFINED_YET;
return lfree[hash]++;
} else
return NULL;
}
int lookup_label(char *label, long *segment, long *offset)
{
union label *lptr;
if (!initialised)
return 0;
lptr = find_label(label, 0);
if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
*segment = lptr->defn.segment;
*offset = lptr->defn.offset;
return 1;
} else
return 0;
}
int is_extern(char *label)
{
union label *lptr;
if (!initialised)
return 0;
lptr = find_label(label, 0);
if (lptr && (lptr->defn.is_global & EXTERN_BIT))
return 1;
else
return 0;
}
void redefine_label(char *label, long segment, long offset, char *special,
int is_norm, int isextrn, struct ofmt *ofmt,
efunc error)
{
union label *lptr;
int exi;
/* This routine possibly ought to check for phase errors. Most assemblers
* check for phase errors at this point. I don't know whether phase errors
* are even possible, nor whether they are checked somewhere else
*/
(void)segment; /* Don't warn that this parameter is unused */
(void)special; /* Don't warn that this parameter is unused */
(void)is_norm; /* Don't warn that this parameter is unused */
(void)isextrn; /* Don't warn that this parameter is unused */
(void)ofmt; /* Don't warn that this parameter is unused */
#ifdef DEBUG
#if DEBUG<3
if (!strncmp(label, "debugdump", 9))
#endif
error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
label, segment, offset, special, is_norm, isextrn);
#endif
lptr = find_label(label, 1);
if (!lptr)
error(ERR_PANIC, "can't find label `%s' on pass two", label);
if (!islocal(label)) {
if (!islocalchar(*label) && lptr->defn.is_norm)
prevlabel = lptr->defn.label;
}
global_offset_changed |= (lptr->defn.offset != offset);
lptr->defn.offset = offset;
if (pass0 == 1) {
exi = !!(lptr->defn.is_global & GLOBAL_BIT);
if (exi) {
char *xsymbol;
int slen;
slen = strlen(lprefix);
slen += strlen(lptr->defn.label);
slen += strlen(lpostfix);
slen++; /* room for that null char */
xsymbol = nasm_malloc(slen);
snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
lpostfix);
ofmt->symdef(xsymbol, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
exi,
special ? special : lptr->
defn.special);
/** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
} else {
if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
EXTERN_BIT) {
ofmt->symdef(lptr->defn.label, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(label, segment, offset,
exi,
special ? special :
lptr->defn.special);
}
}
}
/* if (pass0 == 1) */
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -