File: preprocessorMain.c

package info (click to toggle)
adms 2.3.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,592 kB
  • sloc: xml: 7,167; perl: 4,866; ansic: 3,147; lex: 1,128; yacc: 691; sh: 323; makefile: 110
file content (495 lines) | stat: -rw-r--r-- 15,770 bytes parent folder | download | duplicates (4)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
This file is part of adms - http://sourceforge.net/projects/mot-adms.

adms is a code generator for the Verilog-AMS language.

Copyright (C) 2002-2012 Laurent Lemaitre <r29173@users.sourceforge.net>

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 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * RCS Info
 * $Id: preprocessorMain.c 1154 2008-11-04 10:25:43Z r29173 $
 * 
 * Log
 * $Log$
 * Revision 1.12  2005/06/17 16:44:25  r29173
 * debug memory allocation with valgrind
 *
 * Revision 1.11  2005/06/16 07:12:45  r29173
 * debug memory allocation with valgrind
 *
 * Revision 1.10  2005/05/09 14:38:30  r29173
 * cleaned-up source directory structure
 *
 * Revision 1.9  2005/05/03 09:35:15  r29173
 * cleaned-up header file dependencies
 *
 * Revision 1.8  2005/05/03 07:58:21  r29173
 * moved admsNS into admsFile
 *
 * Revision 1.7  2004/11/19 12:29:56  r29173
 * renamed _C into CAT
 * saved argc, argv into admsmain element
 * added adms:setenv transform
 *
 * Revision 1.6  2004/11/01 09:37:37  r29173
 * changed messaging macros (avoid useless computation of arguments)
 * improved the coding of transforms in xmlProgram.c
 *
 * Revision 1.5  2004/10/20 15:33:52  r29173
 * tons of changes
 * 1- introduced element whileloop (marat)
 * 2- reshaped the handling of the messaging system
 * 3- fixed bug in admsCheck/Makefile.am
 * 4- started implementation of implicit adms transforms
 *
 * Revision 1.4  2004/08/19 16:45:47  r29173
 * cleaned-up i/o file handling
 *
 * Revision 1.3  2004/08/03 12:33:55  r29173
 * import adms-1.21.0 from local CVS
 *
 * Revision 1.3  2004/07/09 14:39:11  r29173
 * removed data structure ns
 *
 * Revision 1.2  2004/05/26 13:02:48  r29173
 * added default values to all enumerations
 *
 * Revision 1.1.1.1  2004/05/21 12:20:01  r29173
 * recreated cvs data structure (crashed after revision 1.13.0!)
 *
 * Revision 1.14  2004/03/08 13:58:06  r29173
 * all code lower-cased
 *
 * Revision 1.13  2004/02/13 14:28:39  r29173
 * started implementing code builder in yacc parser
 *
 * Revision 1.12  2004/02/06 09:06:03  r29173
 * started implementing code builder in yacc parser
 *
 * Revision 1.11  2004/01/21 14:35:47  r29173
 * started implementing code builder in yacc parser
 *
 * Revision 1.10  2004/01/09 16:23:47  r29173
 * cleaned-up use of #include
 *
 * Revision 1.9  2004/01/08 10:28:50  r29173
 * moved code to always.[ch]
 *
 * Revision 1.8  2004/01/06 12:35:29  r29173
 * fixed the use of globals: input file and output file
 *
 * Revision 1.7  2003/12/12 15:08:35  r29173
 * changed construct (a==NULL) to (!a)
 *
 * Revision 1.6  2003/12/12 14:43:38  r29173
 * changed construct (a==NULL) to (!a)
 *
 * Revision 1.5  2003/12/12 14:33:26  r29173
 * changed construct (a!=NULL) into (a) or (a)?1:0
 *
 * Revision 1.4  2003/12/11 20:14:07  r29173
 * cleaned-up after compilation with CFLAG+=-Wall
 *
 * Revision 1.3  2003/12/11 16:01:53  r29173
 * changed prefix [epst]_adms_ to [epst]_
 *
 * Revision 1.2  2003/05/21 14:18:01  r29173
 * add rcs info
 *
 */

#include "admsPreprocessor.h"

#define strdup_before_ptr(s,sptr,offset) adms_knclone(s+offset,sptr-s-offset)
#define strdup_after_ptr(s,sptr,offset)  adms_knclone(sptr+offset,strlen(s)-(sptr-s)-offset)

extern int adms_preprocessor_leng;
static p_preprocessor_main adms_preprocessor_main;
void adms_preprocessor_valueto_main (p_preprocessor_main myadms_preprocessor_main)
{
  adms_preprocessor_main=myadms_preprocessor_main;
}
p_preprocessor_main pproot ()
{
  return adms_preprocessor_main;
}
void adms_preprocessor_get_define_from_argv (const int argc,const char** argv)
{
  int i;
  for (i=1; i<argc; i++)
  {
    char *value=strdup(argv[i]);
    char *defineName = NULL;
    char *defineValue = NULL;
    if ( (strlen(value)>2) && ((value[0])=='-') && ((value[1])=='D') )
    {
      char *equal = NULL;
      char *arg = strdup_after_ptr (value,value+2,0);
      char *argPtr = arg;
      while (*argPtr && (!equal))
      {
        if (*argPtr=='=')
          equal= argPtr;
        argPtr++;
      }
      if (*arg == '=')
        adms_message_verbose(("at command line argument, bad syntax ... '%s'\n",value))
      else if (equal == last_char (arg))
        defineName = strdup_before_ptr (arg,equal,0);
      else if (!equal)
        defineName = strdup(arg);
      else
      {
        defineName = strdup_before_ptr (arg,equal,0);
        defineValue = strdup_after_ptr (arg,equal,1);
      }
      if (defineName)
      {
        if (defineValue)
          adms_preprocessor_define_add_default_with_text (defineName, defineValue);
        else
          adms_preprocessor_define_add_default (defineName);
      }
      free(arg);
    }
    if (defineName)
      adms_message_verbose(("set pragma ... '%s'\n",defineName))
    free(value);
  }
}
int adms_preprocessor_add_message ( char *message)
{
  adms_message_info((message))
  return 0;
}
p_preprocessor_text adms_preprocessor_new_text_as_string(char *name)
{
  p_preprocessor_text text=(p_preprocessor_text)malloc(sizeof(t_preprocessor_text));
  text->_str = name;
  text->_isarg = 0;
  return text;
}
static int adms_preprocessor_pragma_substitute_text (p_preprocessor_substitutor substitutor)
{
  if (adms_preprocessor_pragma_define_is_def (substitutor->_pragma))
  {
    p_preprocessor_text space = adms_preprocessor_new_text_as_string(" ");
    p_slist l; for(l=substitutor->_pragma->text;l;l=l->next)
    {
      p_preprocessor_text text=(p_preprocessor_text)(l->data);
      p_slist arg=adms_slist_copy(substitutor->_pragma->arg);
      adms_slist_inreverse(&arg);
      substitutor->_cr_text=text;
      substitutor->_cr_newarg=adms_slist_copy(substitutor->_newarg);
      adms_slist_inreverse(&substitutor->_cr_newarg);
      if (text->_isarg==1)
      {
        p_slist ll; for(ll=arg;ll;ll=ll->next)
        {
          char* arg=(char*)ll->data;
          if(!strcmp(substitutor->_cr_text->_str,arg))
          {
            if(substitutor->_cr_newarg)
            {
              p_slist newarg=adms_slist_copy((p_slist)(substitutor->_cr_newarg->data));
              adms_slist_inreverse(&newarg);
              adms_slist_concat(&newarg,substitutor->_newtext);
              substitutor->_newtext=newarg;
            }
            else
              adms_slist_push(&(substitutor->_newtext),(p_adms)substitutor->_cr_text);
          }
          substitutor->_cr_newarg = substitutor->_cr_newarg->next;
        }
      }
      else
      {
        p_preprocessor_text newtext = adms_preprocessor_new_text_as_string(text->_str);
        newtext->_isarg = -1;
        adms_slist_push(&(substitutor->_newtext),(p_adms)newtext);
      }
      adms_slist_free(arg);
      adms_slist_free(substitutor->_cr_newarg);
    }
    adms_slist_push(&(substitutor->_newtext),(p_adms)space);
  }
  else
  {
    p_preprocessor_text name = adms_preprocessor_new_text_as_string(substitutor->_pragma->name);
    adms_slist_push(&(substitutor->_newtext),(p_adms)name);
  }
  return 0;
}
p_slist adms_preprocessor_new_text_as_substitutor (p_preprocessor_pragma_define define, p_slist newarg)
{
  p_preprocessor_substitutor substitutor=(p_preprocessor_substitutor)malloc(sizeof(t_preprocessor_substitutor));
  substitutor->_newtext=NULL;
  substitutor->_cr_text=NULL;
  substitutor->_cr_newarg=NULL;
  substitutor->_pragma = define;
  substitutor->_newarg = newarg;
  adms_preprocessor_pragma_substitute_text(substitutor);
  adms_slist_inreverse(&substitutor->_newtext);
  return substitutor->_newtext;
}
int adms_preprocessor_pragma_define_has_arg (p_preprocessor_pragma_define pragma)
{
  return (pragma->hasArg==1);
}
int adms_preprocessor_pragma_define_is_def (p_preprocessor_pragma_define pragma)
{
  return (pragma->isDefined==1);
}
int adms_preprocessor_identifier_is_def (char * name)
{
  int test;
  p_preprocessor_pragma_define pragma;
  if(
      (pragma = adms_preprocessor_pragma_define_exists (name))
      &&
      adms_preprocessor_pragma_define_is_def(pragma)
    )
    test = ( pragma->isDefined == 1 );
  else
    test = 0;
  return test;
}
p_preprocessor_pragma_define adms_preprocessor_identifer_set_undef (char * name)
{
  p_preprocessor_pragma_define pragma = adms_preprocessor_pragma_define_exists (name);
  if(!pragma)
    pragma = adms_preprocessor_define_add (name);
  pragma->isDefined = 0;
  return pragma;
}
int adms_preprocessor_identifier_is_ndef (char * name)
{
  return (!adms_preprocessor_identifier_is_def (name) );
}
int adms_preprocessor_pragma_define_is_ndef (p_preprocessor_pragma_define pragma)
{
  return (!adms_preprocessor_pragma_define_is_def(pragma));
}
int adms_preprocessor_pragma_define_has_noarg (p_preprocessor_pragma_define pragma)
{
  return (!adms_preprocessor_pragma_define_has_arg(pragma));
}
int adms_preprocessor_pragma_define_has_nullarg (p_preprocessor_pragma_define pragma)
{
  return adms_preprocessor_pragma_define_has_arg(pragma) && (adms_slist_length( pragma->arg )==0);
}
int adms_preprocessor_pragma_define_has_text (p_preprocessor_pragma_define pragma)
{
  return ((pragma->text)?1:0);
}
int adms_preprocessor_pragma_define_has_notext (p_preprocessor_pragma_define pragma)
{
  return (!adms_preprocessor_pragma_define_has_text(pragma));
}
int adms_preprocessor_pragma_define_has_noarg_and_notext (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_noarg(pragma)
         &&
         adms_preprocessor_pragma_define_has_notext(pragma)
         ;
  return test;
}
int adms_preprocessor_pragma_define_has_noarg_and_text (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_noarg(pragma)
         &&
         adms_preprocessor_pragma_define_has_text(pragma)
         ;
  return test;
}
int adms_preprocessor_pragma_define_has_nullarg_and_notext (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_nullarg(pragma)
         &&
         adms_preprocessor_pragma_define_has_notext(pragma)
         ;
  return test;
}
int adms_preprocessor_pragma_define_has_nullarg_and_text (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_nullarg(pragma)
         &&
         adms_preprocessor_pragma_define_has_text(pragma)
         ;
  return test;
}
int adms_preprocessor_pragma_define_has_arg_and_notext (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_arg(pragma)
         &&
         adms_preprocessor_pragma_define_has_notext(pragma)
         ;
  return test;
}
int adms_preprocessor_pragma_define_has_arg_and_text (p_preprocessor_pragma_define pragma)
{
  int test;
  test = adms_preprocessor_pragma_define_has_arg(pragma)
         &&
         adms_preprocessor_pragma_define_has_text(pragma)
         ;
  return test;
}
static int adms_preprocessor_identifier_is_pragma_cb (p_preprocessor_pragma_define pragma, char * identifier)
{
  return (!!strcmp(pragma->name,identifier));
}
typedef int (*Binary) (const void* d1,const void* d2);
static p_slist adms_slist_find_custom (p_slist l,const void* data,Binary func)
{
  while(l)
  {
    if(!func(l->data,data))
       return l;
      l=l->next;
  }
  return ((void *)0);
}
p_preprocessor_pragma_define adms_preprocessor_pragma_define_exists (char * name)
{
  p_slist Data;
  p_preprocessor_pragma_define pragma;
  if((Data=adms_slist_find_custom ( pproot()->Defined,name,(Binary)adms_preprocessor_identifier_is_pragma_cb)))
    pragma=(p_preprocessor_pragma_define) Data->data;
  else
    pragma=NULL;
  return pragma;
}
p_preprocessor_pragma_define adms_preprocessor_define_add (char * name)
{
  p_preprocessor_pragma_define pragma;
  if ((pragma = adms_preprocessor_pragma_define_exists(name)))
  {
    if( pragma->isDefined == 1 )
      adms_message_warning(("pragma redefined ... '%s'\n",name))
    pragma->hasArg = 0;
    pragma->arg = NULL;
    pragma->text = NULL;
  }
  else
  {
    pragma=(p_preprocessor_pragma_define)malloc(sizeof(t_preprocessor_pragma_define));
    pragma->hasArg = 0;
    pragma->arg=NULL;
    pragma->text=NULL;
    pragma->name = name;
    adms_slist_push(&(pproot()->Defined),(p_adms)pragma);
  }
  pragma->isDefined = 1;
  return pragma;
}
int adms_preprocessor_define_add_default (char * name)
{
  p_preprocessor_pragma_define pragma;
  pragma = adms_preprocessor_define_add (name);
  adms_message_verbose(("define macro ... '%s'\n",name))
  assert(adms_preprocessor_pragma_define_has_noarg_and_notext(pragma));
  return 0;
}
int adms_preprocessor_define_add_default_with_text (char * name, char * value)
{
  p_slist Text;
  p_preprocessor_text text;
  p_preprocessor_pragma_define pragma;
  text = adms_preprocessor_new_text_as_string(value);
  Text = adms_slist_new((p_adms)text);
  pragma = adms_preprocessor_define_add_with_text (name, Text);
  adms_message_verbose(("define macro with value ... '%s=%s'\n",name, value))
  assert(adms_preprocessor_pragma_define_has_noarg_and_text(pragma));
  return 0;
}
p_preprocessor_pragma_define adms_preprocessor_undefine_add (char * name)
{
  p_preprocessor_pragma_define pragma;
  pragma = adms_preprocessor_pragma_define_exists(name);
  if(!pragma)
    {
      pragma=(p_preprocessor_pragma_define)malloc(sizeof(t_preprocessor_pragma_define));
      pragma->arg=NULL;
      pragma->text=NULL;
      pragma->name = name;
      adms_slist_push(&(pproot()->Defined),(p_adms)pragma);
    }
  pragma->isDefined = 0;
  return pragma;
}
p_preprocessor_pragma_define adms_preprocessor_define_add_with_arg (char * name, p_slist arg)
{
  p_preprocessor_pragma_define pragma;
  pragma = adms_preprocessor_define_add (name );
  pragma->hasArg = 1;
  pragma->arg = arg;
  pragma->text = NULL;
  return pragma;
}
p_preprocessor_pragma_define adms_preprocessor_define_add_with_text (char * name, p_slist text)
{
  p_preprocessor_pragma_define pragma;
  pragma = adms_preprocessor_define_add (name );
  pragma->hasArg = 0;
  pragma->arg = NULL;
  pragma->text = text;
  return pragma;
}
p_preprocessor_pragma_define adms_preprocessor_define_add_with_arg_and_text (char * name, p_slist arg, p_slist text)
{
  p_preprocessor_pragma_define pragma;
  p_slist l;
  pragma = adms_preprocessor_define_add_with_arg (name, arg );
  pragma->text = text;
  for(l=pragma->text;l;l=l->next)
  {
    p_preprocessor_text text=(p_preprocessor_text)(l->data);
    p_slist ll; for(ll=pragma->arg;ll;ll=ll->next)
    {
      if((text->_isarg != -1) && !strcmp(text->_str,(char*)(ll->data)))
        text->_isarg = 1;
    }
  }
  return pragma;
}
static p_continuator adms_preprocessor_get_line_position_cb (p_slist l,int mycharpos)
{
  while(l)
  {
    p_continuator mycontinuator=(p_continuator)(l->data);
    if(mycharpos<=mycontinuator->char_position)
       return mycontinuator;
    l=l->next;
  }
  return NULL;
}
int adms_preprocessor_get_line_position (p_preprocessor cr_preprocessor, int char_pos)
{
  int line_pos=cr_preprocessor->cur_line_position;
  p_continuator mycontinuator;
  p_slist Reversed=adms_slist_reverse(cr_preprocessor->cur_continuator_position);
  if((mycontinuator=adms_preprocessor_get_line_position_cb(Reversed,char_pos)))
    line_pos=mycontinuator->line_position;
  cr_preprocessor->cur_continuator_position = adms_slist_reverse(Reversed );
  return line_pos;
}