www.pudn.com > ASM86_64.rar > label.c


#include "a64-2.h"
#include "label.h"


extern unsigned long long current_pc;
extern unsigned long line;


/**********************************************/

int is_label(char *s)
{
	if (!s || (s[0] != '@')) 
		return 0;
				
	struct label_link *label_link = label_table;

	while (label_link && !str_cmp(label_link->label->name, s))
		label_link = label_link->next;

	return (int)label_link;
}


void mount_label_table(char *s)
{
	struct label_link *head_label_link = label_table;


	struct label_struct *label = 
		(struct label_struct *)a64_malloc(sizeof(struct label_struct));


	label->name = (char *)a64_malloc(str_len(s)+1);

	strcpy(label->name, s);
	label->addr = current_pc;
	label->line = line;

	struct label_link *label_link_node = 
		(struct label_link *)a64_malloc(sizeof(struct label_link));

	label_link_node->label = label;		

	if (!head_label_link) {
		label_table = label_link_node;
	} else {
		while (head_label_link && head_label_link->next)	
			head_label_link = head_label_link->next;
		head_label_link->next = label_link_node;
	}
}


#if 0
struct token_link *found_label(char *buf)
{
	char str[80];
	int i = 0;
	int line = 1;
	char *p = buf;
	char *q;

	struct token_link *tlk = 0;

	while (*buf) {
		if (*buf == '\n')
			line++;
		else if (*buf == ':') {
			q = buf - 1;
			while ((*q != ' ') && (*q != '\n') && (q >= p)) 
				 q--;
			q++;
			while (q < buf) 
				str[i++] = *q++;
			
			str[i] = 0;
			if (i == 0) {
				buf++; continue;
			}
			if (is_valid_sym(str)) {
				struct token *t = get_token();
				t->name = (char *)malloc(i);
				strcpy(t->name, str);
				t->line = line;
				
				struct token_link *tk = (struct token_link *)
					malloc(sizeof(struct token_link));
				memset(tk, 0, sizeof(tk));
				tk->token = t;
				if (tlk) {
					for (; tlk->next; tlk = tlk->next);
					tlk->next = tk;
				} else
					tlk = tk;
			}
		}
		i = 0;	
		buf++;
	}		

	return tlk;
}

#endif


void mount_hole_link(char *label, i_key_t *i_key)
{
	struct transfer_hole_struct *hpos = (struct transfer_hole_struct *)
			a64_malloc(sizeof(struct transfer_hole_struct));

	struct transfer_hole_struct *head_hpos = hole_link;

	hpos->hole_label = a64_malloc(str_len(label) + 1);
	strcpy(hpos->hole_label, label);
	hpos->hole_i_key = i_key;
	hpos->hole_addr = current_pc;
	hpos->hole_line = line;
	hpos->bits = current_bits;
	
	if (!hole_link) {
		hole_link = hpos;
	} else {
		while (head_hpos->next) 
			head_hpos = head_hpos->next;

		head_hpos->next = hpos;
	}
}


struct label_link *get_label_node(char *label)
{
	struct label_link *head_ll = label_table;
	
	while (head_ll && !str_cmp(head_ll->label->name, label))
		head_ll = head_ll->next;
	
	return head_ll;
}


unsigned long long get_label_addr(char *label)
{
	struct label_link *head_ll = label_table;
	
	while (head_ll && !str_cmp(head_ll->label->name, label)) 
		head_ll = head_ll->next;
	
	return head_ll ? head_ll->label->addr : 0;
}




void print_label_table()
{
	struct label_link *lt = label_table;
	while (lt) {
		printf("label: %s ", lt->label->name);
		printf("addr: %x ", lt->label->addr);
		printf("line: %d\n", lt->label->line);
		lt= lt->next;
	}
}

void print_hole_link()
{
	struct transfer_hole_struct *hl = hole_link;
	while (hl) {
		printf("hole label: %s ", hl->hole_label);
		printf("hole line: %d ", hl->hole_line);
		printf("hole addr: %x ", hl->hole_addr);
		printf("hole size: %d\n", hl->hole_size);
		hl = hl->next;
	}
}

/**************************************************************************/
static void release_hole_struct(struct transfer_hole_struct *hole_struct)
{
	if (hole_struct) {
		if (hole_struct->hole_label)
			a64_free(hole_struct->hole_label);
			
		a64_free(hole_struct);
	}
}

static void release_label_struct(struct label_struct *label_struct)
{
	if (label_struct) {
		if (label_struct->name)
			a64_free(label_struct->name);
		
		a64_free(label_struct);
	}
}

void release_hole_link()
{
	struct transfer_hole_struct *hole_next  = 0;

	while (hole_link) {
		hole_next = hole_link->next;
		release_hole_struct(hole_link);

		hole_link = hole_next;
	
	}
}

void release_label_table()
{
	struct label_link *label_next;
	
	while (label_table) {
		label_next = label_table->next;
		release_label_struct(label_table->label);
		a64_free(label_table);
		label_table = label_next;
	}
}




/*
main()
{
	char buf[80];
	int fd = open("string.s",O_RDONLY);
	struct iob iob;
	init_iob(fd, &iob);
	while (read_into_iob(&iob,1024))
	while (read_from_iob(&iob, buf, 80)) {
	struct token_link *tk = found_label(buf);
	while (tk) {
		printf("%s:%d\n", tk->token->name, tk->token->line);
		tk = tk->next;
	}
}
}
*/