File: macro.c

package info (click to toggle)
alsa-tools 1.0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,464 kB
  • ctags: 5,787
  • sloc: ansic: 23,609; cpp: 15,057; sh: 12,305; pascal: 1,140; asm: 1,053; xml: 814; makefile: 733; python: 250
file content (210 lines) | stat: -rw-r--r-- 5,791 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/***************************************************************************
                          macro.c  - various functions to handle macros
                             -------------------
    Date                 : May 23 2000
    Copyright            : (C) 2000 by Daniel Bertrand
    Email                : d.bertrand@ieee.ca
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include<string.h>
#include<stdio.h>
#include"types.h"
#include"proto.h"
#include <ctype.h>

int macro_ctn;
struct macrdef macro[MAX_DEF_MACRO];
extern char *listing,listtemp[60];
extern FILE *listfile;

//determines if an opcode neumonic is a macro

int ismacro(char *mac)
{
       
        int i;
        
        for(i=0;i<macro_ctn; i++){
                if(strcasecmp(macro[i].name,mac)==0){
                        return(i);
                }
        }
        return(-1);
}

//defines a new macro, adds it to the macro list
void new_macro(char *symbol, char *line, char *operand)
{
        extern int source_line_num;
        struct sym *sym;

	if (macro_ctn >= MAX_DEF_MACRO)
                as_exit("Parse Error: Too many macros");

        if(isalpha(*symbol)==0)
                as_exit("Parse Error: Symbol must start with an alpha character");
        
        if(ismacro(symbol)!=-1)
                as_exit("Parsed Error: macro is already defined");

        if(issymbol(symbol,&sym)!=-1)
                as_exit("Parse Error: Symbol is already defined");

        macro[macro_ctn].line_num=source_line_num;
        macro[macro_ctn].ptr=line;
        strcpy(macro[macro_ctn].name,symbol); 
        macro[macro_ctn].operands=operand;
        macro_ctn++;
        
}

//called from parsed() when a macro is used, stores the arguments and recursively calls the parse().

void macro_expand(int macnum,char *operand )
{
        char *line,*next;
        int done=0,i,old;
        extern unsigned int macro_depth;
        extern int macro_line_num;
        char string[MAX_LINE_LENGTH];
       
        //initialize macro use:
        i=0;
       
        if(macro_depth+1> MAX_MAC_DEPTH)
                as_exit("Error exceeded maximum number of recursive macro calls");

        old=macro_line_num;
        macro_line_num=macro[macnum].line_num;
        macro_operand(macro[macnum].operands,operand);
        macro_depth++;
        
        line=macro[macnum].ptr;
        next=line;

        
        while((*next!= '\n') )         //skip to the line after the macro definition
                next++;
        line=next;
        
        
        //Expand the macro calling parse()
        
        while(done!=-1)
        {
                
                while((*next!= '\n') )
                        next++;
                
                *next='\0';
   
                strcpy(&string[0],line);
                listtemp[0]='\0';
                done=parse(string, line);
                macro_line_num++;
                *next='\n';
		if(listing){
			if(done==1)
				sprintf(listtemp,"macro expansion done");
			if(done!=-3)
				fprintf(listfile,"%-50s ||   %s\n",listtemp,line);
		}
                if(done==-1)
                        break;
                next++;
        line=next;
        }
        macro_line_num=old;
        macro_depth--;

        return;

}
//assigns calling arguments with defined symbols.
void macro_operand(char *symbols,char *val)
{
        char tmp[MAX_LINE_LENGTH],*ptr=symbols,*sym=tmp,*next_sym=sym,*next_val=val;
        extern unsigned int macro_depth;
       
       
        if(symbols==NULL&&val==NULL)
                return;
        if(symbols==NULL||val==NULL)
                as_exit("error in macro_operand, Null operand list");
                
       
        while(*ptr!='\n' && *ptr!=';')
                ptr++;
        *ptr='\0';
         
        strcpy(tmp,symbols);
        
        //#ifdef DEBUG        
        //        printf("syms:\"%s\",vals:\"%s\"\n",sym,val);
        //#endif
          *ptr='\n';
        
        while(1){

                //skip over blanks:
                advance(next_sym);
                advance(next_val);
                
                
                sym = next_sym;
                val = next_val;
               
                if(*next_val=='\0' && *next_sym=='\0')
                        return;
                if(*next_sym=='\0')
                        as_exit("Error, To many arguments for defined Macro");
                
                
                if(*next_val=='\0')
                        as_exit("Error, Not enough arguments for defined macro");

                
                
                while(*next_sym != '\0' && *next_sym!= ',' )
                        next_sym++;

                while(*next_val != '\0' && *next_val!= ',' )
                        next_val++;
                //                                 printf("sym=\"%s\";val=\"%s\"(=0x%x)\n",sym, val,arg_decode(val,0) );
                if( sym!=next_sym || val!=next_val ){
                        update_symbol(sym,TYPE_MACRO_ARG,arg_decode(val,0),macro_depth+1);
                }
               
        }
        
}