ack/util/ceg/assemble/obj_assemble/assemble.c

242 lines
4.7 KiB
C
Raw Normal View History

1987-11-20 11:12:07 +00:00
#include <ctype.h>
#include <system.h>
1988-05-31 13:20:28 +00:00
#include <stdio.h>
#include <varargs.h>
1987-11-20 11:12:07 +00:00
#include "as.h"
#include "const.h"
1987-11-26 12:00:32 +00:00
/* This file contains the routine assemble(). Assemble() cuts an
* assembly instruction in a label, a mnemonic and several operands.
* For a mnemonic,label and operands it calls table writer defined
* routines, process_mnemonic() * process_label() and process_operand(),
* to give the table writer the oppurtunity to do something special.
* At the end assemble() calls the routine belonging to the mnemonic
* with the supplied operands.
1987-11-26 12:00:32 +00:00
* If the table writer has other expectations of assemble() he should
* write his own version.
* Assemble parser the following instructions :
* INSTR ::= [ STRING ':']? [ STRING [ OPERAND ( ',' OPERAND)*]? ]?
* OPERAND ::= STRING [ '{' char* '}' |
* '(' char* ')' |
* '[' char* ']' ]?
* note : nested brackets are not recognized.
*/
/* The following global varaibles are defined in the EM_parser.
*/
1987-11-20 11:12:07 +00:00
extern char *mnemonic[];
extern int (*instruction[])(), n_mnems;
1987-11-26 12:00:32 +00:00
/* The struct t_operand must be defined by the table writer in "as.h".
* The constant MAX_OPERANDS is defined in "const.h"
* To change MAX_OPERANDS effectively, the last statement in
* execute_mnem() must be changed.
1987-11-20 11:12:07 +00:00
*/
1987-11-26 12:00:32 +00:00
struct t_operand operand[ MAX_OPERANDS];
1987-11-20 11:12:07 +00:00
char *skip_space(), *parse_label(), *parse_mnemonic(), *parse_operand(),
*skip_string(), *match_ch(), *Salloc(), *skip_operand();
int label();
assemble( instr)
char *instr;
1987-11-26 12:00:32 +00:00
/* Break an assembly instruction down in a LABEL, MNEMONIC and OPERANDS.
1987-11-20 11:12:07 +00:00
*/
{
char *ptr, *copy, *mnem;
int n_ops = 0;
copy = ptr = Salloc( instr, strlen( instr)+1);
ptr = skip_space( ptr);
1987-11-26 12:00:32 +00:00
if ( label( ptr)) { /* Look for a label */
1987-11-20 11:12:07 +00:00
ptr = parse_label( ptr);
if ( *ptr == '\0') return;
}
ptr = parse_mnemonic( ptr, &mnem);
1987-11-26 12:00:32 +00:00
while ( *ptr != '\0') { /* parse operans */
1987-11-20 11:12:07 +00:00
if ( n_ops++ == MAX_OPERANDS)
error( "to many operands\n");
ptr = parse_operand( ptr, n_ops, instr);
}
1987-11-26 12:00:32 +00:00
execute_mnemonic( mnem); /* Execute the assembler instruction */
1987-11-20 11:12:07 +00:00
free( copy);
}
int label( ptr)
char *ptr;
{
ptr = skip_string( ptr);
ptr = skip_space( ptr);
return( *ptr == ':');
}
char *parse_label( ptr)
char *ptr;
{
char *lab = ptr;
ptr = skip_string( ptr);
if ( *ptr == ':')
*ptr++ = '\0';
else {
*ptr++ = '\0';
ptr = skip_space( ptr);
ptr++; /* skip ':' */
}
handle_label( lab);
ptr = skip_space( ptr);
return( ptr);
}
char *parse_mnemonic( ptr, mnem)
char *ptr, **mnem;
{
*mnem = ptr;
ptr = skip_string( ptr);
if ( *ptr != '\0') {
*ptr++ = '\0';
ptr = skip_space( ptr);
}
return( ptr);
}
char *parse_operand( ptr, n_ops, instr)
char *ptr, *instr;
int n_ops;
{
1988-05-31 13:16:48 +00:00
char *op = ptr,
*last;
1987-11-20 11:12:07 +00:00
ptr = skip_operand( ptr, instr);
1988-05-31 13:41:03 +00:00
for( last=ptr-1; isspace( *last); last--)
;
if ( *ptr == ',') {
ptr = skip_space( ptr + 1);
1987-11-20 11:12:07 +00:00
}
1988-05-31 13:41:03 +00:00
*(last+1) = '\0';
1987-11-20 11:12:07 +00:00
process_operand( op, &operand[ n_ops-1]);
return( ptr);
}
char *skip_operand( ptr, instr)
char *ptr, *instr;
{
while ( *ptr != ',' && *ptr != '\0') {
switch ( *ptr) {
case '{' : ptr = match_ch( '}', ptr, instr);
break;
case '(' : ptr = match_ch( ')', ptr, instr);
break;
case '[' : ptr = match_ch( ']', ptr, instr);
break;
}
ptr++;
}
return( ptr);
}
char *match_ch( c, str, instr)
char c, *str, *instr;
{
char *ptr, *strindex();
ptr = strindex( str, c);
if ( ptr == 0) {
error( "syntax error in %s : %c expected\n", instr, c);
return( str);
}
else
return( ptr);
}
char *skip_string( ptr)
char *ptr;
{
while ( *ptr != '\0' && !isspace( *ptr) && *ptr != ':')
1987-11-20 11:12:07 +00:00
ptr++;
return( ptr);
}
char *skip_space( ptr)
char *ptr;
{
while ( isspace( *ptr) )
ptr++;
return( ptr);
}
1987-11-26 12:00:32 +00:00
/*** Execution **************************************************************/
1987-11-20 11:12:07 +00:00
execute_mnemonic( mnem)
char *mnem;
/* Find the function by "mnem" and execute it.
*/
{
int low, mid, high, rel;
process_mnemonic( mnem);
1987-11-20 11:12:07 +00:00
low = 0;
high = n_mnems-1;
while ( TRUE) {
mid = ( low + high) / 2;
rel = strcmp(mnem, mnemonic[ mid]);
if ( rel == 0 )
break;
else if ( high == low) {
1987-11-20 11:12:07 +00:00
error( "can't find %s", mnem);
return;
}
1987-11-20 11:12:07 +00:00
else if ( rel < 0)
high = mid;
else
1989-01-26 14:27:02 +00:00
/* watch it, mid is truncated */
1987-11-20 11:12:07 +00:00
low = ( mid == low ? low + 1: mid);
}
( *( instruction[ mid]))( &operand[0], &operand[1], &operand[2],
&operand[3]);
}
1987-11-26 12:00:32 +00:00
/*** Error ****************************************************************/
/*VARARGS*/
error(va_alist)
va_dcl
1987-11-20 11:12:07 +00:00
{
char *fmt;
va_list args;
1987-11-20 11:12:07 +00:00
extern int yylineno;
va_start(args);
fmt = va_arg(args, char *);
fprint( STDERR, "ERROR in line %d : ", yylineno);
doprnt( STDERR, fmt, args);
fprint( STDERR, "\n");
va_end(args);
1987-11-20 11:12:07 +00:00
}