source: soft/giet_vm/giet_xml/xml_parser.c @ 760

Last change on this file since 760 was 709, checked in by alain, 9 years ago

Major release: Change the task model to implement the POSIX threads API.

  • The shell "exec" and "kill" commands can be used to activate/de-activate the applications.
  • The "pause", "resume", and "context" commands can be used to stop, restart, a single thtead or to display the thread context.

This version has been tested on the following multi-threaded applications,
that have been modified to use the POSIX threads:

  • classif
  • convol
  • transpose
  • gameoflife
  • raycast
  • Property svn:executable set to *
File size: 53.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////
2// File     : xml_parser.c
3// Date     : 14/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////////
7// This program translate a "map.xml" source file to a binary file "map.bin" that
8// can be directly loaded in memory and used by the GIET-VM operating system.
9//
10// This map.xml file contains :
11// 1) the multi-cluster/multi-processors hardware architecture description
12// 2) the various multi-threaded software applications
13// 3) the mapping directives bor both the threads and the virtual segments.
14// The corresponding C structures are defined in the "mapping_info.h" file.
15///////////////////////////////////////////////////////////////////////////////////////
16
17#include  <stdlib.h>
18#include  <fcntl.h>
19#include  <sys/types.h>
20#include  <sys/stat.h>
21#include  <unistd.h>
22#include  <stdio.h>
23#include  <string.h>
24#include  <assert.h>
25#include  <libxml/xmlreader.h>
26#include  <mapping_info.h>
27#include  <irq_handler.h>
28#include  <giet_config.h>
29
30#define MAX_CLUSTERS   1024
31#define MAX_PSEGS      4096
32#define MAX_VSPACES    1024
33#define MAX_THREADS      4096
34#define MAX_VSEGS      4096
35#define MAX_PROCS      1024
36#define MAX_IRQS       8192
37#define MAX_PERIPHS    8192
38
39#define XML_PARSER_DEBUG  1
40
41///////////////////////////////////////////////////////////////////////////////////
42//     global variables used to store and index the data structures
43///////////////////////////////////////////////////////////////////////////////////
44
45mapping_header_t *   header;
46mapping_cluster_t *  cluster[MAX_CLUSTERS];  // cluster array
47mapping_pseg_t *     pseg[MAX_PSEGS];        // pseg array
48mapping_vspace_t *   vspace[MAX_VSPACES];    // vspace array
49mapping_vseg_t *     vseg[MAX_VSEGS];        // vseg array
50mapping_thread_t *   thread[MAX_THREADS];      // thread array
51mapping_proc_t *     proc[MAX_PROCS];        // proc array
52mapping_irq_t *      irq[MAX_IRQS];          // irq array
53mapping_periph_t *   periph[MAX_PERIPHS];    // peripheral array
54
55// Index for the various arrays
56
57unsigned int cluster_index  = 0;
58unsigned int vspace_index = 0;
59unsigned int global_index = 0;
60unsigned int pseg_index = 0;       
61
62unsigned int proc_index = 0;
63unsigned int proc_loc_index = 0;
64
65unsigned int irq_index = 0;
66unsigned int irq_loc_index  = 0;
67
68unsigned int periph_index = 0;
69unsigned int periph_loc_index = 0;
70
71unsigned int vseg_index = 0;
72unsigned int vseg_loc_index = 0;
73
74unsigned int thread_index = 0;
75unsigned int thread_loc_index = 0;
76
77
78//////////////////////////////////////////////////
79unsigned int getIntValue( xmlTextReaderPtr reader, 
80                          const char * attributeName, 
81                          unsigned int * ok) 
82{
83    unsigned int value = 0;
84    unsigned int i;
85    char c;
86
87    char * string = (char *) xmlTextReaderGetAttribute(reader, 
88                                (const xmlChar *) attributeName);
89
90    if (string == NULL) 
91    {
92        // missing argument
93        *ok = 0;
94        return 0;
95    }
96    else 
97    {
98        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
99        {
100            // Hexa
101            for (i = 2 ; (string[i] != 0) && (i < 10) ; i++) 
102            {
103                c = string[i];
104                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
105                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
106                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
107                else 
108                {
109                    *ok = 0;
110                    return 0;
111                }
112            }
113        }
114        else 
115        {
116            // Decimal
117            for (i = 0; (string[i] != 0) && (i < 9); i++) 
118            {
119                c = string[i];
120                if ((c >= '0') && (c <= '9')) value = (value * 10) + string[i] - 48;
121                else 
122                {
123                    *ok = 0;
124                    return 0;
125                }
126            }
127        }
128        *ok = 1;
129        return value; 
130    }
131} // end getIntValue()
132
133////////////////////////////////////////////////
134paddr_t getPaddrValue( xmlTextReaderPtr reader, 
135                       const char * attributeName, 
136                       unsigned int * ok) 
137{
138    paddr_t value = 0;
139    unsigned int i;
140    char c;
141
142    char * string = (char *) xmlTextReaderGetAttribute(reader, 
143                                (const xmlChar *) attributeName);
144
145    if (string == NULL) 
146    {
147        // missing argument
148        *ok = 0;
149        return 0;
150    }
151    else 
152    {
153        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
154        {
155            // Hexa
156            for (i = 2 ; (string[i] != 0) && (i < 18) ; i++) 
157            {
158                c = string[i];
159                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
160                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
161                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
162                else 
163                {
164                    *ok = 0;
165                    return 0;
166                }
167            }
168        }
169        else 
170        {
171            // Decimal not supported for paddr_t
172            *ok = 0;
173            return 0;
174        }
175        *ok = 1;
176        return value; 
177    }
178} // end getPaddrValue()
179
180////////////////////////////////////////////////
181char * getStringValue( xmlTextReaderPtr reader, 
182                       const char * attributeName, 
183                       unsigned int * ok ) 
184{
185    char * string = (char *) xmlTextReaderGetAttribute(reader, 
186                               (const xmlChar *) attributeName);
187
188
189    if (string == NULL) 
190    {
191        // missing argument
192        *ok = 0;
193        return NULL;
194    }
195    else 
196    {
197        //we read only string smaller than 32 byte
198        if (strlen(string) > 32) 
199        {
200            printf("[XML ERROR] all strings must be less than 32 bytes\n");
201            exit(1);
202        }
203
204        *ok = 1;
205        return string;
206    }
207} // end getStringValue()
208
209///////////////////////////////////////////////////////////////
210int getClusterId( unsigned int x, unsigned int y )
211{
212    // associative search of cluster index
213    unsigned int cluster_id;
214
215    for( cluster_id = 0 ; cluster_id < (header->x_size * header->y_size) ; cluster_id++ )
216    {
217        if( (cluster[cluster_id]->x == x) && (cluster[cluster_id]->y == y) )
218        {
219            return cluster_id;
220        }
221    }
222    return -1;
223}  // end getClusterId()
224
225///////////////////////////////////////////////////////////////
226int getPsegId(unsigned int x, unsigned int y, char * pseg_name) 
227{
228    int cluster_id = getClusterId( x, y );
229
230    if ( cluster_id == -1 ) return -1;
231
232    // associative search for pseg index
233    unsigned int pseg_id;
234    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
235    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
236
237    for (pseg_id = pseg_min; pseg_id < pseg_max; pseg_id++) 
238    {
239        if (strcmp(pseg[pseg_id]->name, pseg_name) == 0) 
240        {
241            return pseg_id;
242        }
243    }
244    return -1;
245}  // end getPsegId()
246
247///////////////////////////////////
248int getVspaceId(char * vspace_name) 
249{
250    unsigned int vspace_id;
251
252    for (vspace_id = 0; vspace_id < vspace_index; vspace_id++) 
253    {
254        if (strcmp(vspace[vspace_id]->name, vspace_name) == 0) 
255        {
256            return vspace_id;
257        }
258    }
259    return -1;
260}
261
262///////////////////////////////////////////////////
263int getVsegId( unsigned int vspace_id, char *name )
264{
265    unsigned int vseg_id;
266    unsigned int vseg_min = vspace[vspace_id]->vseg_offset;
267    unsigned int vseg_max = vseg_min + vspace[vspace_id]->vsegs;
268
269    for (vseg_id = vseg_min ; vseg_id < vseg_max ; vseg_id++) 
270    {
271        if (strcmp(vseg[vseg_id]->name, name) == 0) return vseg_id;
272    }
273    return -1;
274}
275
276
277////////////////////////////////////////
278void threadNode(xmlTextReaderPtr reader) 
279{
280    unsigned int ok;
281    unsigned int value;
282    unsigned int x,y;
283    char * str;
284
285    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
286
287    if (thread_index >= MAX_THREADS) 
288    {
289        printf("[XML ERROR] The number of threads is larger than %d\n", MAX_THREADS);
290        exit(1);
291    }
292
293#if XML_PARSER_DEBUG
294printf("   thread %d\n", thread_loc_index);
295#endif
296
297    thread[thread_index] = (mapping_thread_t *) malloc(sizeof(mapping_thread_t));
298
299    ////////// get name attribute
300    str = getStringValue(reader, "name", &ok);
301    if (ok) 
302    {
303#if XML_PARSER_DEBUG
304printf("      name      = %s\n", str);
305#endif
306        strncpy( thread[thread_index]->name, str, 31 );
307    }
308    else 
309    {
310        printf("[XML ERROR] illegal or missing <name> for thread %d in vspace %d\n", 
311                thread_loc_index, vspace_index);
312        exit(1);
313    }
314
315    ///////// get is_main attribute
316    value = getIntValue(reader, "is_main", &ok);
317    if ( ok ) 
318    {
319#if XML_PARSER_DEBUG
320printf("      is_main   = %d\n", value );
321#endif
322        thread[thread_index]->is_main = value;
323    }
324    else
325    {
326        printf("[XML ERROR] illegal or missing <is_main> for thread %d in vspace %d\n", 
327                thread_loc_index, vspace_index);
328    }
329
330    ///////// get x coordinate
331    x = getIntValue(reader, "x", &ok);
332#if XML_PARSER_DEBUG
333printf("      x         = %d\n", x);
334#endif
335    if ( !(ok && (x < header->x_size)) ) 
336    {
337        printf("[XML ERROR] illegal or missing < x > for thread %d in vspace %d)\n",
338                thread_loc_index, vspace_index);
339        exit(1);
340    } 
341
342    ///////// get y coordinate
343    y = getIntValue(reader, "y", &ok);
344#if XML_PARSER_DEBUG
345printf("      y         = %d\n", y);
346#endif
347    if ( !(ok && (y < header->y_size)) ) 
348    {
349        printf("[XML ERROR] illegal or missing < y > for thread %d in vspace %d)\n",
350                thread_loc_index, vspace_index);
351        exit(1);
352    } 
353
354    ///////// set clusterid attribute
355    int index = getClusterId( x, y );
356#if XML_PARSER_DEBUG
357printf("      clusterid = %d\n", index);
358#endif
359    if( index >= 0 )
360    {
361        thread[thread_index]->clusterid = index;
362    }
363    else
364    {
365        printf("[XML ERROR] <clusterid> not found for thread %d in vspace %d)\n",
366                thread_loc_index, vspace_index);
367        exit(1);
368    }
369
370    ////////// get p attribute
371    value = getIntValue(reader, "p", &ok);
372    if (ok) 
373    {
374#if XML_PARSER_DEBUG
375printf("      proclocid = %x\n", value);
376#endif
377        if (value >= cluster[thread[thread_index]->clusterid]->procs) 
378        {
379            printf("[XML ERROR] <proclocid> too large for thread %d in vspace %d\n",
380                    thread_loc_index, vspace_index);
381            exit(1);
382        }
383        thread[thread_index]->proclocid = value;
384    } 
385    else 
386    {
387        printf("[XML ERROR] illegal or missing < p > for thread %d in vspace %d)\n",
388                thread_loc_index, vspace_index);
389        exit(1);
390    }
391
392    ////////// get stackname attribute
393    char* stack_name = getStringValue(reader, "stackname" , &ok);
394    if (ok) 
395    {
396#if XML_PARSER_DEBUG
397printf("      stackname = %s\n", str);
398#endif
399        int index = getVsegId( vspace_index , stack_name );
400        if (index >= 0) 
401        {
402#if XML_PARSER_DEBUG
403printf("      stack_id  = %d\n", index);
404#endif
405            thread[thread_index]->stack_vseg_id = index;
406        }
407        else 
408        {
409            printf("[XML ERROR] illegal or missing <stackname> for thread %d in vspace %d)\n",
410                    thread_loc_index, vspace_index);
411            exit(1);
412        }
413    } 
414    else 
415    {
416        printf("[XML ERROR] illegal or missing <stackname> for thread %d in vspace %d)\n",
417                thread_loc_index, vspace_index);
418        exit(1);
419    }
420
421    ////////// get heap attribute
422    char* heap_name = getStringValue(reader, "heapname", &ok);
423    if (ok) 
424    {
425#if XML_PARSER_DEBUG
426printf("      heapname  = %s\n", str);
427#endif
428        int index = getVsegId( vspace_index , heap_name );
429        if (index >= 0) 
430        {
431#if XML_PARSER_DEBUG
432printf("      heap_id   = %d\n", index );
433#endif
434            thread[thread_index]->heap_vseg_id = index;
435        }
436        else 
437        {
438            printf("[XML ERROR] illegal or missing <heapname> for thread %d in vspace %d)\n", 
439                   thread_loc_index, vspace_index);
440            exit(1);
441        }
442    } 
443    else 
444    {
445        thread[thread_index]->heap_vseg_id = -1;
446    }
447
448    ////////// get startid  attribute
449    value = getIntValue(reader, "startid", &ok);
450    if (ok) 
451    {
452#if XML_PARSER_DEBUG
453printf("      startid   = %x\n", value);
454#endif
455        thread[thread_index]->startid = value;
456    } 
457    else 
458    {
459        printf("[XML ERROR] illegal or missing <startid> for thread %d in vspace %d\n", 
460                thread_loc_index, vspace_index);
461        exit(1);
462    }
463
464    thread_index++;
465    thread_loc_index++;
466} // end threadNode()
467
468
469//////////////////////////////////////
470void vsegNode(xmlTextReaderPtr reader) 
471{
472    unsigned int ok;
473    unsigned int value;
474    unsigned int x,y;
475    char * str;
476
477    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
478
479    if (vseg_index >= MAX_VSEGS) 
480    {
481        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
482        exit(1);
483    }
484
485#if XML_PARSER_DEBUG
486printf("    vseg %d\n", vseg_loc_index);
487#endif
488
489    vseg[vseg_index] = (mapping_vseg_t *) malloc(sizeof(mapping_vseg_t));
490
491    ///////// set mapped attribute
492    vseg[vseg_index]->mapped = 0;
493
494    ///////// get name attribute
495    str = getStringValue(reader, "name", &ok);
496    if (ok) 
497    {
498#if XML_PARSER_DEBUG
499printf("      name        = %s\n", str);
500#endif
501        strncpy( vseg[vseg_index]->name, str, 31);
502    }
503    else 
504    {
505        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
506                vspace_index, vseg_loc_index);
507        exit(1);
508    }
509
510    ////////// get ident attribute (optional : 0 if missing)
511    value = getIntValue(reader, "ident", &ok);
512    if (ok) 
513    {
514#if XML_PARSER_DEBUG
515printf("      ident       = %d\n", value);
516#endif
517        vseg[vseg_index]->ident = value;
518    } 
519    else 
520    {
521        vseg[vseg_index]->ident = 0;
522    }
523
524    ////////// get local attribute (optional : 0 if missing)
525    value = getIntValue(reader, "local", &ok);
526    if (ok) 
527    {
528#if XML_PARSER_DEBUG
529printf("      local       = %d\n", value);
530#endif
531        vseg[vseg_index]->local = value;
532    } 
533    else 
534    {
535        vseg[vseg_index]->local = 0;
536    }
537
538    ////////// get big attribute (optional : 0 if missing)
539    value = getIntValue(reader, "big", &ok);
540    if (ok) 
541    {
542#if XML_PARSER_DEBUG
543printf("      big         = %d\n", value);
544#endif
545        vseg[vseg_index]->big = value;
546    } 
547    else 
548    {
549        vseg[vseg_index]->big = 0;
550    }
551
552    /////////// get vbase attribute
553    value = getIntValue(reader, "vbase", &ok);
554    if (ok) 
555    {
556#if XML_PARSER_DEBUG
557printf("      vbase       = 0x%x\n", value);
558#endif
559        vseg[vseg_index]->vbase = value;
560    }
561    else 
562    {
563        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
564                vspace_index, vseg_loc_index);
565        exit(1);
566    }
567
568    ////////// get length attribute
569    value = getIntValue(reader, "length", &ok);
570    if (ok)
571    {
572#if XML_PARSER_DEBUG
573printf("      length      = %x\n", value);
574#endif
575        vseg[vseg_index]->length = value;
576    }
577    else 
578    {
579        printf("[XML ERROR] illegal or missing <length> attribute for vseg (%d,%d)\n",
580                vspace_index, vseg_loc_index);
581        exit(1);
582    }
583
584    //////// get type attribute
585    str = getStringValue(reader, "type", &ok);
586
587#if XML_PARSER_DEBUG
588printf("      type        = %s\n", str);
589#endif
590
591    if      (ok && (strcmp(str, "ELF")    == 0)) vseg[vseg_index]->type = VSEG_TYPE_ELF;
592    else if (ok && (strcmp(str, "PERI")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_PERI;
593    else if (ok && (strcmp(str, "BLOB")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_BLOB;
594    else if (ok && (strcmp(str, "PTAB")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_PTAB;
595    else if (ok && (strcmp(str, "BUFFER") == 0)) vseg[vseg_index]->type = VSEG_TYPE_BUFFER;
596    else if (ok && (strcmp(str, "SCHED")  == 0)) vseg[vseg_index]->type = VSEG_TYPE_SCHED;
597    else if (ok && (strcmp(str, "HEAP")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_HEAP;
598    else
599    {
600        printf("[XML ERROR] illegal or missing <type> attribute for vseg (%d,%d)\n",
601                vspace_index, vseg_loc_index);
602        exit(1);
603    }
604
605    ////////// get x coordinate
606    x = getIntValue(reader, "x", &ok);
607#if XML_PARSER_DEBUG
608printf("      x           = %d\n", x);
609#endif
610    if ( !(ok && (x < header->x_size)) ) 
611    {
612        printf("[XML ERROR] illegal or missing < x > attribute for vseg %d\n", 
613                vseg_loc_index);
614        exit(1);
615    }
616
617    ////////// get y coordinate
618    y = getIntValue(reader, "y", &ok);
619#if XML_PARSER_DEBUG
620printf("      y           = %d\n", y);
621#endif
622    if ( !(ok && (y < header->y_size)) ) 
623    {
624        printf("[XML ERROR] illegal or missing < y > attribute for vseg %d\n", 
625                vseg_loc_index);
626        exit(1);
627    }
628
629    ///////// get psegname attribute
630    str = getStringValue(reader, "psegname", &ok);
631#if XML_PARSER_DEBUG
632printf("      psegname    = %s\n", str);
633#endif
634    if (ok == 0) 
635    {
636        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
637                vseg_loc_index);
638        exit(1);
639    }
640
641    /////////// set psegid field
642    int psegid = getPsegId( x, y, str );
643#if XML_PARSER_DEBUG
644printf("      psegid      = %d\n", psegid);
645#endif
646    if (psegid >= 0) 
647    {
648        vseg[vseg_index]->psegid = psegid;
649    }
650    else 
651    {
652        printf("[XML ERROR] pseg not found for vseg %d / x = %d / y = %d / psegname = %s\n", 
653                vseg_loc_index, x, y, str );
654        exit(1);
655    } 
656
657    //////// get mode attribute
658    str = getStringValue(reader, "mode", &ok);
659#if XML_PARSER_DEBUG
660printf("      mode        = %s\n", str);
661#endif
662    if      (ok && (strcmp(str, "CXWU") == 0)) { vseg[vseg_index]->mode = 0xF; }
663    else if (ok && (strcmp(str, "CXW_") == 0)) { vseg[vseg_index]->mode = 0xE; }
664    else if (ok && (strcmp(str, "CX_U") == 0)) { vseg[vseg_index]->mode = 0xD; }
665    else if (ok && (strcmp(str, "CX__") == 0)) { vseg[vseg_index]->mode = 0xC; }
666    else if (ok && (strcmp(str, "C_WU") == 0)) { vseg[vseg_index]->mode = 0xB; }
667    else if (ok && (strcmp(str, "C_W_") == 0)) { vseg[vseg_index]->mode = 0xA; }
668    else if (ok && (strcmp(str, "C__U") == 0)) { vseg[vseg_index]->mode = 0x9; }
669    else if (ok && (strcmp(str, "C___") == 0)) { vseg[vseg_index]->mode = 0x8; }
670    else if (ok && (strcmp(str, "_XWU") == 0)) { vseg[vseg_index]->mode = 0x7; }
671    else if (ok && (strcmp(str, "_XW_") == 0)) { vseg[vseg_index]->mode = 0x6; }
672    else if (ok && (strcmp(str, "_X_U") == 0)) { vseg[vseg_index]->mode = 0x5; }
673    else if (ok && (strcmp(str, "_X__") == 0)) { vseg[vseg_index]->mode = 0x4; }
674    else if (ok && (strcmp(str, "__WU") == 0)) { vseg[vseg_index]->mode = 0x3; }
675    else if (ok && (strcmp(str, "__W_") == 0)) { vseg[vseg_index]->mode = 0x2; }
676    else if (ok && (strcmp(str, "___U") == 0)) { vseg[vseg_index]->mode = 0x1; }
677    else if (ok && (strcmp(str, "____") == 0)) { vseg[vseg_index]->mode = 0x0; }
678    else {
679        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
680                vspace_index, vseg_loc_index);
681        exit(1);
682    }
683
684    ////////// get binpath attribute (optional : "" if missing)
685    str = getStringValue(reader, "binpath", &ok);
686    if (ok)
687    {
688#if XML_PARSER_DEBUG
689printf("      binpath = %s\n", str);
690#endif
691        strncpy(vseg[vseg_index]->binpath, str, 63);
692    }
693    else
694    {
695        vseg[vseg_index]->binpath[0] = 0;
696    }
697
698    vseg_index++;
699    vseg_loc_index++;
700} // end vsegNode()
701
702////////////////////////////////////////
703void vspaceNode(xmlTextReaderPtr reader) 
704{
705    unsigned int ok;
706
707    vseg_loc_index   = 0;
708    thread_loc_index = 0;
709
710    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
711
712    vspace[vspace_index] = (mapping_vspace_t *) malloc(sizeof(mapping_vspace_t));
713    header->vspaces      = header->vspaces + 1;
714
715    ////////// get name attribute
716    char* vspace_name = getStringValue(reader, "name", &ok);
717    if (ok) 
718    {
719#if XML_PARSER_DEBUG
720printf("\n  vspace = %s\n", vspace_name );
721#endif
722        strncpy( vspace[vspace_index]->name, vspace_name , 31 );
723    }
724    else 
725    {
726        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
727                vspace_index);
728        exit(1);
729    }
730
731    ////////// get active attribute (optional / default is 0)
732    unsigned int value = getIntValue(reader, "active", &ok);
733    if (ok) vspace[vspace_index]->active = value;
734    else    vspace[vspace_index]->active = 0;
735   
736    ////////// set vseg_offset and thread_offset attributes
737    vspace[vspace_index]->vseg_offset = vseg_index;
738    vspace[vspace_index]->thread_offset = thread_index;
739
740    ////////// initialise vsegs and threads attributes
741    vspace[vspace_index]->vsegs = 0;
742    vspace[vspace_index]->threads = 0;
743
744    ////////// get startname attribute
745    char* start_name = getStringValue(reader, "startname", &ok);
746    if (ok == 0) 
747    {
748        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
749                vspace[vspace_index]->name);
750        exit(1);
751    }
752
753    int status = xmlTextReaderRead(reader);
754    while (status == 1) 
755    {
756        const char * tag = (const char *) xmlTextReaderConstName(reader);
757
758        if (strcmp(tag, "vseg") == 0) 
759        {
760            vsegNode(reader);
761            vspace[vspace_index]->vsegs += 1;
762        }
763        else if (strcmp(tag, "thread") == 0) 
764        {
765            threadNode(reader);
766            vspace[vspace_index]->threads += 1;
767        }
768        else if (strcmp(tag, "#text")    == 0) { }
769        else if (strcmp(tag, "#comment") == 0) { }
770        else if (strcmp(tag, "vspace")   == 0) 
771        {
772            // get index of the vseg containing the start vector
773            int index = getVsegId( vspace_index, start_name );
774            if (index == -1) 
775            {
776                printf("[XML ERROR] vseg containing start vector not found in vspace %s\n",
777                        vspace[vspace_index]->name);
778                exit(1);
779            }
780            else 
781            {
782                vspace[vspace_index]->start_vseg_id = index;
783            }
784
785#if XML_PARSER_DEBUG
786printf("      vsegs         = %d\n", vspace[vspace_index]->vsegs );
787printf("      threads       = %d\n", vspace[vspace_index]->threads );
788printf("      vseg_offset   = %d\n", vspace[vspace_index]->vseg_offset );
789printf("      thread_offset = %d\n", vspace[vspace_index]->thread_offset );
790printf("      start_id      = %d\n", vspace[vspace_index]->start_vseg_id );
791printf("      active        = %x\n", vspace[vspace_index]->active );
792printf("  end vspace %d\n\n", vspace_index);
793#endif
794            vspace_index++;
795            return;
796        }
797        else 
798        {
799            printf("[XML ERROR] Unknown tag %s", tag);
800            exit(1);
801        }
802        status = xmlTextReaderRead(reader);
803    }
804} // end vspaceNode()
805
806/////////////////////////////////////
807void irqNode(xmlTextReaderPtr reader) 
808{
809    unsigned int ok;
810    unsigned int value;
811    char * str;
812
813    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
814
815    if (irq_index >= MAX_IRQS) 
816    {
817        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
818    }
819
820#if XML_PARSER_DEBUG
821printf("      irq %d\n", irq_loc_index);
822#endif
823
824    irq[irq_index] = (mapping_irq_t *) malloc(sizeof(mapping_irq_t));
825
826    ///////// get srcid attribute
827    value = getIntValue(reader, "srcid", &ok);
828    if (ok) 
829    {
830#if XML_PARSER_DEBUG
831printf("        srcid   = %d\n", value);
832#endif
833        irq[irq_index]->srcid = value;
834        if (value >= 32) 
835        {
836            printf("[XML ERROR] IRQ <srcid> too large for periph %d in cluster %d\n",
837                    cluster_index, periph_loc_index);
838            exit(1);
839        }
840    }
841    else 
842    {
843        printf("[XML ERROR] missing IRQ <srcid> for periph %d in cluster %d\n",
844                cluster_index, periph_loc_index);
845        exit(1);
846    }
847
848    ///////// get srctype attribute
849    str = getStringValue(reader, "srctype", &ok);
850    if (ok) 
851    {
852#if XML_PARSER_DEBUG
853printf("        srctype = %s\n", str);
854#endif
855        if      ( strcmp(str, "HWI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_HWI;
856        else if ( strcmp(str, "WTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_WTI;
857        else if ( strcmp(str, "PTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_PTI;
858        else   
859        {
860            printf("[XML ERROR] illegal IRQ <srctype> for periph %d in cluster %d\n",
861                   cluster_index, periph_loc_index);
862            exit(1);
863        }
864    }
865    else 
866    {
867        printf("[XML ERROR] missing IRQ <srctype> for periph %d in cluster %d\n",
868               cluster_index, periph_loc_index);
869        exit(1);
870    }
871
872    ///////// get isr attribute
873    str = getStringValue(reader, "isr", &ok);
874    if (ok) 
875    {
876#if XML_PARSER_DEBUG
877printf("        isr     = %s\n", str);
878#endif
879        if      (strcmp(str, "ISR_DEFAULT") == 0)  irq[irq_index]->isr = ISR_DEFAULT;
880        else if (strcmp(str, "ISR_TICK"   ) == 0)  irq[irq_index]->isr = ISR_TICK;
881        else if (strcmp(str, "ISR_TTY_RX" ) == 0)  irq[irq_index]->isr = ISR_TTY_RX;
882        else if (strcmp(str, "ISR_TTY_TX" ) == 0)  irq[irq_index]->isr = ISR_TTY_TX;
883        else if (strcmp(str, "ISR_BDV"    ) == 0)  irq[irq_index]->isr = ISR_BDV;
884        else if (strcmp(str, "ISR_TIMER"  ) == 0)  irq[irq_index]->isr = ISR_TIMER;
885        else if (strcmp(str, "ISR_WAKUP"  ) == 0)  irq[irq_index]->isr = ISR_WAKUP;
886        else if (strcmp(str, "ISR_NIC_RX" ) == 0)  irq[irq_index]->isr = ISR_NIC_RX;
887        else if (strcmp(str, "ISR_NIC_TX" ) == 0)  irq[irq_index]->isr = ISR_NIC_TX;
888        else if (strcmp(str, "ISR_CMA"    ) == 0)  irq[irq_index]->isr = ISR_CMA;
889        else if (strcmp(str, "ISR_MMC"    ) == 0)  irq[irq_index]->isr = ISR_MMC;
890        else if (strcmp(str, "ISR_DMA"    ) == 0)  irq[irq_index]->isr = ISR_DMA;
891        else if (strcmp(str, "ISR_SDC"    ) == 0)  irq[irq_index]->isr = ISR_SDC;
892        else if (strcmp(str, "ISR_MWR"    ) == 0)  irq[irq_index]->isr = ISR_MWR;
893        else if (strcmp(str, "ISR_HBA"    ) == 0)  irq[irq_index]->isr = ISR_HBA;
894        else 
895        {
896            printf("[XML ERROR] illegal IRQ <isr> for periph %d in cluster %d\n",
897                   periph_loc_index, cluster_index );
898            exit(1);
899        }
900    } 
901    else 
902    {
903        printf("[XML ERROR] missing IRQ <isr> for periph %d in cluster %d\n",
904                cluster_index, periph_loc_index);
905        exit(1);
906    }
907
908    ///////// get channel attribute (optionnal : 0 if missing)
909    value = getIntValue(reader, "channel", &ok);
910    if (ok) 
911    {
912#if XML_PARSER_DEBUG
913printf("        channel = %d\n", value);
914#endif
915        irq[irq_index]->channel = value;
916    }
917    else 
918    {
919        irq[irq_index]->channel = 0;
920    }
921
922    irq_index++;
923    irq_loc_index++;
924
925} // end irqNode
926
927
928////////////////////////////////////////
929void periphNode(xmlTextReaderPtr reader) 
930{
931    char * str;
932    unsigned int value;
933    unsigned int ok;
934
935    irq_loc_index = 0;
936
937    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
938
939    if (periph_index >= MAX_PERIPHS) 
940    {
941        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
942        exit(1);
943    }
944
945#if XML_PARSER_DEBUG
946printf("\n    periph %d\n", periph_index);
947#endif
948
949    periph[periph_index] = (mapping_periph_t *) malloc(sizeof(mapping_periph_t));
950
951    ///////// get channels attribute (optionnal : 1 if missing)
952    value = getIntValue(reader, "channels", &ok);
953    if (ok) 
954    {
955#if XML_PARSER_DEBUG
956printf("      channels    = %d\n", value);
957#endif
958        periph[periph_index]->channels = value;
959    }
960    else 
961    {
962        periph[periph_index]->channels = 1;
963    }
964
965    ///////// get arg0 attribute (optionnal : undefined if missing)
966    value = getIntValue(reader, "arg0", &ok);
967    if (ok) 
968    {
969#if XML_PARSER_DEBUG
970printf("      arg0        = %d\n", value);
971#endif
972        periph[periph_index]->arg0 = value;
973    }
974
975    ///////// get arg1 attribute (optionnal : undefined if missing)
976    value = getIntValue(reader, "arg1", &ok);
977    if (ok) 
978    {
979#if XML_PARSER_DEBUG
980printf("      arg1        = %d\n", value);
981#endif
982        periph[periph_index]->arg1 = value;
983    }
984
985    ///////// get arg2 attribute (optionnal : undefined if missing)
986    value = getIntValue(reader, "arg2", &ok);
987    if (ok) 
988    {
989#if XML_PARSER_DEBUG
990printf("      arg2        = %d\n", value);
991#endif
992        periph[periph_index]->arg2 = value;
993    }
994
995    ///////// get arg3 attribute (optionnal : undefined if missing)
996    value = getIntValue(reader, "arg3", &ok);
997    if (ok) 
998    {
999#if XML_PARSER_DEBUG
1000printf("      arg3        = %d\n", value);
1001#endif
1002        periph[periph_index]->arg3 = value;
1003    }
1004
1005    /////////// get psegname attribute
1006    str = getStringValue(reader, "psegname", &ok);
1007    if (ok == 0) 
1008    {
1009        printf("[XML ERROR] illegal or missing <psegname> for periph %d in cluster %d\n", 
1010                periph_index, cluster_index);
1011        exit(1);
1012    }
1013
1014    /////////// set psegid attribute
1015    int index = getPsegId( cluster[cluster_index]->x, cluster[cluster_index]->y, str);
1016    if (index >= 0) 
1017    {
1018#if XML_PARSER_DEBUG
1019printf("      clusterid   = %d\n", cluster_index);
1020printf("      psegname    = %s\n", str);
1021printf("      psegid      = %d\n", index);
1022#endif
1023        periph[periph_index]->psegid = index;
1024    }
1025    else 
1026    {
1027        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
1028                periph_loc_index, cluster_index, str );
1029        exit(1);
1030    } 
1031
1032    /////////// get type attribute
1033    str = getStringValue(reader, "type", &ok);
1034    if (ok) 
1035    {
1036#if XML_PARSER_DEBUG
1037printf("      type        = %s\n", str);
1038#endif
1039        if      (strcmp(str, "CMA" ) == 0) periph[periph_index]->type = PERIPH_TYPE_CMA;
1040        else if (strcmp(str, "DMA" ) == 0) periph[periph_index]->type = PERIPH_TYPE_DMA;
1041        else if (strcmp(str, "FBF" ) == 0) periph[periph_index]->type = PERIPH_TYPE_FBF;
1042        else if (strcmp(str, "IOB" ) == 0) periph[periph_index]->type = PERIPH_TYPE_IOB;
1043        else if (strcmp(str, "IOC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_IOC;
1044        else if (strcmp(str, "MMC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_MMC;
1045        else if (strcmp(str, "MWR" ) == 0) periph[periph_index]->type = PERIPH_TYPE_MWR;
1046        else if (strcmp(str, "NIC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_NIC;
1047        else if (strcmp(str, "ROM" ) == 0) periph[periph_index]->type = PERIPH_TYPE_ROM;
1048        else if (strcmp(str, "SIM" ) == 0) periph[periph_index]->type = PERIPH_TYPE_SIM;
1049        else if (strcmp(str, "SIM" ) == 0) periph[periph_index]->type = PERIPH_TYPE_TIM;
1050        else if (strcmp(str, "TTY" ) == 0) periph[periph_index]->type = PERIPH_TYPE_TTY;
1051        else if (strcmp(str, "XCU" ) == 0) periph[periph_index]->type = PERIPH_TYPE_XCU;
1052        else if (strcmp(str, "PIC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_PIC;
1053        else if (strcmp(str, "DROM") == 0) periph[periph_index]->type = PERIPH_TYPE_DROM;
1054        else
1055        {
1056            printf("[XML ERROR] illegal peripheral type: %s in cluster %d\n",
1057                    str, cluster_index);
1058            exit(1);
1059        }
1060    }
1061
1062    ////////// get subtype if IOC
1063    if (periph[periph_index]->type == PERIPH_TYPE_IOC )
1064    {
1065        char* subtype = getStringValue(reader, "subtype", &ok);
1066        if (ok)
1067        {
1068#if XML_PARSER_DEBUG
1069printf("      subtype     = %s\n", str);
1070#endif
1071            if      (strcmp(subtype, "BDV") == 0) 
1072            periph[periph_index]->subtype = IOC_SUBTYPE_BDV;
1073            else if (strcmp(subtype, "HBA") == 0) 
1074            periph[periph_index]->subtype = IOC_SUBTYPE_HBA;
1075            else if (strcmp(subtype, "SDC") == 0) 
1076            periph[periph_index]->subtype = IOC_SUBTYPE_SDC;
1077            else if (strcmp(subtype, "SPI") == 0) 
1078            periph[periph_index]->subtype = IOC_SUBTYPE_SPI;
1079        }
1080        else
1081        {
1082            printf("[XML ERROR] illegal subtype for IOC peripheral\n");
1083            exit(1);
1084        }
1085    }
1086    else
1087    {
1088        periph[periph_index]->subtype = 0XFFFFFFFF;
1089    }
1090
1091    ////////// get subtype if MWR
1092    if (periph[periph_index]->type == PERIPH_TYPE_MWR )
1093    {
1094        char* subtype = getStringValue(reader, "subtype", &ok);
1095        if (ok)
1096        {
1097#if XML_PARSER_DEBUG
1098printf("      subtype     = %s\n", str);
1099#endif
1100            if      (strcmp(subtype, "GCD") == 0) 
1101            periph[periph_index]->subtype = MWR_SUBTYPE_GCD;
1102            else if (strcmp(subtype, "DCT") == 0) 
1103            periph[periph_index]->subtype = MWR_SUBTYPE_DCT;
1104            else if (strcmp(subtype, "CPY") == 0) 
1105            periph[periph_index]->subtype = MWR_SUBTYPE_CPY;
1106        }
1107        else
1108        {
1109            printf("[XML ERROR] illegal subtype for MWR peripheral\n");
1110            exit(1);
1111        }
1112    }
1113    else
1114    {
1115        periph[periph_index]->subtype = 0XFFFFFFFF;
1116    }
1117
1118    ////////////// set irq_offset attribute
1119    periph[periph_index]->irq_offset = irq_index;
1120
1121    ///////////// get IRQs
1122    int status = xmlTextReaderRead(reader);
1123    while (status == 1) 
1124    {
1125        const char * tag = (const char *) xmlTextReaderConstName(reader);
1126
1127        if (strcmp(tag, "irq") == 0) 
1128        {
1129            if ( (periph[periph_index]->type != PERIPH_TYPE_XCU) &&
1130                 (periph[periph_index]->type != PERIPH_TYPE_PIC) )
1131            {
1132                printf("[XML ERROR] periph %d in cluster(%d,%d) "
1133                       " only XCU and PIC can contain IRQs",
1134                periph_loc_index, cluster[cluster_index]->x, cluster[cluster_index]->y);
1135                exit(1);
1136            }
1137            else
1138            {
1139                  irqNode(reader);
1140            }
1141        }
1142        else if (strcmp(tag, "#text")    == 0) { }
1143        else if (strcmp(tag, "#comment") == 0) { }
1144        else if (strcmp(tag, "periph")   == 0) 
1145        {
1146            periph[periph_index]->irqs = irq_loc_index;
1147            cluster[cluster_index]->periphs++;
1148            periph_loc_index++;
1149            periph_index++;
1150
1151#if XML_PARSER_DEBUG
1152printf("      irqs        = %d\n", irq_loc_index);
1153printf("      irq_offset  = %d\n", irq_index);
1154#endif
1155            return;
1156        }
1157        else 
1158        {
1159            printf("[XML ERROR] Unknown tag %s", tag);
1160            exit(1);
1161        }
1162        status = xmlTextReaderRead(reader);
1163    }
1164} // end periphNode
1165
1166//////////////////////////////////////
1167void procNode(xmlTextReaderPtr reader) 
1168{
1169    unsigned int ok;
1170    unsigned int value;
1171
1172    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1173
1174    if (proc_index >= MAX_PROCS) 
1175    {
1176        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1177        exit(1);
1178    }
1179
1180#if XML_PARSER_DEBUG
1181printf("\n    proc %d\n", proc_index);
1182#endif
1183
1184    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1185
1186    /////////// get index attribute (optional)
1187    value = getIntValue(reader, "index", &ok);
1188    if (ok && (value != proc_loc_index)) 
1189    {
1190        printf("[XML ERROR] wrong local proc index / expected value is %d", 
1191                proc_loc_index);
1192        exit(1);
1193    }
1194    proc[proc_index]->index = proc_loc_index;
1195
1196    cluster[cluster_index]->procs++;
1197    proc_loc_index++;
1198    proc_index++;
1199} // end procNode()
1200
1201
1202//////////////////////////////////////
1203void psegNode(xmlTextReaderPtr reader) 
1204{
1205    unsigned int ok;
1206    paddr_t      ll_value;
1207    char * str;
1208
1209    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1210
1211    if (pseg_index >= MAX_PSEGS) 
1212    {
1213        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1214        exit(1);
1215    }
1216
1217#if XML_PARSER_DEBUG
1218printf("    pseg %d\n", pseg_index);
1219#endif
1220
1221    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1222
1223    /////// get name attribute
1224    str = getStringValue(reader, "name", &ok);
1225#if XML_PARSER_DEBUG
1226printf("      name = %s\n", str);
1227#endif
1228    if (ok) 
1229    {
1230        strncpy(pseg[pseg_index]->name, str, 31);
1231    }
1232    else 
1233    {
1234        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1235                pseg_index, cluster_index);
1236        exit(1);
1237    }
1238
1239    //////// get type attribute
1240    str = getStringValue(reader, "type", &ok);
1241#if XML_PARSER_DEBUG
1242printf("      type = %s\n", str);
1243#endif
1244    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1245    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1246    else 
1247    {
1248        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1249                pseg[pseg_index]->name, cluster_index);
1250        exit(1);
1251    }
1252
1253    //////// get base attribute
1254    ll_value = getPaddrValue(reader, "base", &ok);
1255#if XML_PARSER_DEBUG
1256printf("      base = 0x%llx\n", ll_value);
1257#endif
1258    if (ok) 
1259    {
1260        pseg[pseg_index]->base = ll_value;
1261    }
1262    else {
1263        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1264                pseg[pseg_index]->name, cluster_index);
1265        exit(1);
1266    }
1267
1268    //////// get length attribute
1269    ll_value = getPaddrValue(reader, "length", &ok);
1270#if XML_PARSER_DEBUG
1271printf("      length = 0x%llx\n", ll_value);
1272#endif
1273    if (ok) 
1274    {
1275        pseg[pseg_index]->length = ll_value;
1276    } 
1277    else 
1278    {
1279        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1280                pseg[pseg_index]->name, cluster_index);
1281        exit(1);
1282    }
1283
1284    //////// set cluster attribute
1285    pseg[pseg_index]->clusterid = cluster_index;
1286
1287    //////// set next_vseg attribute
1288    pseg[pseg_index]->next_vseg = 0;
1289
1290    pseg_index++;
1291    cluster[cluster_index]->psegs++;
1292} // end psegNode()
1293
1294
1295/////////////////////////////////////////
1296void clusterNode(xmlTextReaderPtr reader) 
1297{
1298    unsigned int ok;
1299    unsigned int value;
1300
1301    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1302
1303    //initialise variables that will be incremented by *Node() functions
1304    cluster[cluster_index]->psegs = 0;
1305    cluster[cluster_index]->procs = 0;
1306    cluster[cluster_index]->periphs = 0;
1307
1308    //initialise global variables
1309    proc_loc_index = 0;
1310    periph_loc_index = 0;
1311
1312    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)  return;
1313
1314#if XML_PARSER_DEBUG
1315printf("\n  cluster %d\n", cluster_index);
1316#endif
1317
1318    /////////// get x coordinate
1319    value = getIntValue(reader, "x", &ok);
1320#if XML_PARSER_DEBUG
1321printf("    x             = %d\n", value);
1322#endif
1323    if (ok && (value < header->x_size) ) 
1324    {
1325        cluster[cluster_index]->x = value;
1326    }
1327    else
1328    {
1329        printf("[XML ERROR] Illegal or missing < x > attribute for cluster %d", 
1330                cluster_index);
1331        exit(1);
1332    }
1333
1334    /////////// get y coordinate
1335    value = getIntValue(reader, "y", &ok);
1336#if XML_PARSER_DEBUG
1337printf("    y             = %d\n", value);
1338#endif
1339    if (ok && (value < header->y_size) ) 
1340    {
1341        cluster[cluster_index]->y = value;
1342    }
1343    else
1344    {
1345        printf("[XML ERROR] Illegal or missing < y > attribute for cluster %d", 
1346                cluster_index);
1347        exit(1);
1348    }
1349
1350    ////////// set offsets
1351    cluster[cluster_index]->pseg_offset = pseg_index;
1352    cluster[cluster_index]->proc_offset = proc_index;
1353    cluster[cluster_index]->periph_offset = periph_index;
1354
1355#if XML_PARSER_DEBUG
1356printf("    pseg_offset   = %d\n", pseg_index);
1357printf("    proc_offset   = %d\n", proc_index);
1358printf("    periph_offset = %d\n", periph_index);
1359#endif
1360
1361    ////////// get psegs, procs, and periphs
1362    int status = xmlTextReaderRead(reader);
1363
1364    while (status == 1) 
1365    {
1366        const char * tag = (const char *) xmlTextReaderConstName(reader);
1367
1368        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
1369        else if (strcmp(tag, "proc")     == 0) procNode(reader);
1370        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
1371        else if (strcmp(tag, "#text")    == 0) { }
1372        else if (strcmp(tag, "#comment") == 0) { }
1373        else if (strcmp(tag, "cluster")  == 0) 
1374        {
1375
1376#if XML_PARSER_DEBUG
1377printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1378printf("    procs   = %d\n", cluster[cluster_index]->procs);
1379printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1380printf("    end cluster %d\n", cluster_index);
1381#endif
1382            cluster_index++;
1383            return;
1384        }
1385        status = xmlTextReaderRead(reader);
1386    }
1387} // end clusterNode()
1388
1389
1390//////////////////////////////////////////////
1391void clusterSetNode(xmlTextReaderPtr reader) 
1392{
1393    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1394
1395#if XML_PARSER_DEBUG
1396printf("\n  clusters set\n");
1397#endif
1398
1399    int status = xmlTextReaderRead(reader);
1400    while (status == 1) 
1401    {
1402        const char * tag = (const char *) xmlTextReaderConstName(reader);
1403
1404        if      (strcmp(tag, "cluster")    == 0) { clusterNode(reader); }
1405        else if (strcmp(tag, "#text")      == 0) { }
1406        else if (strcmp(tag, "#comment")   == 0) { }
1407        else if (strcmp(tag, "clusterset") == 0) 
1408        {
1409            // checking number of clusters
1410            if ( cluster_index != (header->x_size * header->y_size) ) 
1411            {
1412                printf("[XML ERROR] Wrong number of clusters\n");
1413                exit(1);
1414            }
1415
1416#if XML_PARSER_DEBUG
1417            printf("  end cluster set\n\n");
1418#endif
1419            header->psegs   = pseg_index;
1420            header->procs   = proc_index;
1421            header->irqs    = irq_index;
1422            header->periphs = periph_index;
1423            return;
1424        }
1425        else 
1426        {
1427            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1428            exit(1);
1429        }
1430        status = xmlTextReaderRead(reader);
1431    }
1432} // end clusterSetNode()
1433
1434
1435///////////////////////////////////////////
1436void globalSetNode(xmlTextReaderPtr reader) 
1437{
1438    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1439
1440#if XML_PARSER_DEBUG
1441    printf("  globals set\n");
1442#endif
1443
1444    int status = xmlTextReaderRead(reader);
1445    while (status == 1) 
1446    {
1447        const char * tag = (const char *) xmlTextReaderConstName(reader);
1448
1449        if      (strcmp(tag, "vseg")      == 0) 
1450        { 
1451            vsegNode( reader ); 
1452            header->globals = header->globals + 1;
1453        }
1454        else if (strcmp(tag, "#text")     == 0) { }
1455        else if (strcmp(tag, "#comment")  == 0) { }
1456        else if (strcmp(tag, "globalset") == 0) 
1457        {
1458#if XML_PARSER_DEBUG
1459            printf("  end global set\n\n");
1460#endif
1461            vseg_loc_index = 0;
1462            return;
1463        }
1464        else 
1465        {
1466            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1467            exit(1);
1468        }
1469        status = xmlTextReaderRead(reader);
1470    }
1471} // end globalSetNode()
1472
1473
1474///////////////////////////////////////////
1475void vspaceSetNode(xmlTextReaderPtr reader)
1476{
1477    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1478        return;
1479    }
1480
1481#if XML_PARSER_DEBUG
1482    printf("\n  vspaces set\n");
1483#endif
1484
1485    int status = xmlTextReaderRead ( reader );
1486    while (status == 1) {
1487        const char * tag = (const char *) xmlTextReaderConstName(reader);
1488
1489        if (strcmp(tag, "vspace") == 0) {
1490            vspaceNode(reader);
1491        }
1492        else if (strcmp(tag, "#text"    ) == 0 ) { }
1493        else if (strcmp(tag, "#comment" ) == 0 ) { }
1494        else if (strcmp(tag, "vspaceset") == 0 ) 
1495        {
1496            header->vsegs = vseg_index;
1497            header->threads = thread_index;
1498            return;
1499        }
1500        else 
1501        {
1502            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1503            exit(1);
1504        }
1505        status = xmlTextReaderRead(reader);
1506    }
1507} // end globalSetNode()
1508
1509
1510////////////////////////////////////////
1511void headerNode(xmlTextReaderPtr reader) 
1512{
1513    char * name;
1514    unsigned int value;
1515    unsigned int ok;
1516
1517    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1518
1519#if XML_PARSER_DEBUG
1520    printf("mapping_info\n");
1521#endif
1522
1523    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
1524
1525    ////////// get name attribute
1526    name = getStringValue(reader, "name", &ok);
1527    if (ok) 
1528    {
1529#if XML_PARSER_DEBUG
1530        printf("  name = %s\n", name);
1531#endif
1532        strncpy( header->name, name, 31);
1533    }
1534    else 
1535    {
1536        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1537        exit(1);
1538    }
1539
1540    /////////// get signature attribute
1541    value = getIntValue(reader, "signature", &ok);
1542    if ( ok && (value == IN_MAPPING_SIGNATURE) )
1543    {
1544#if XML_PARSER_DEBUG
1545        printf("  signature = %x\n", value);
1546#endif
1547        header->signature = IN_MAPPING_SIGNATURE;
1548    }
1549    else
1550    {
1551        printf("[XML ERROR] illegal or missing <signature> for mapping %s\n",
1552        header->name );
1553        exit(1);
1554    }
1555
1556    /////////// get x_width attribute
1557    value = getIntValue(reader, "x_width", &ok);
1558    if (ok) 
1559    {
1560#if XML_PARSER_DEBUG
1561        printf("  x_width = %d\n", value);
1562#endif
1563        header->x_width = value;
1564    }
1565
1566    /////////// get y_width attribute
1567    value = getIntValue(reader, "y_width", &ok);
1568    if (ok) 
1569    {
1570#if XML_PARSER_DEBUG
1571        printf("  y_width = %d\n", value);
1572#endif
1573        header->y_width = value;
1574    }
1575
1576    /////////// get x_size attribute
1577    unsigned int x_size = getIntValue(reader, "x_size", &ok);
1578    if (ok) 
1579    {
1580#if XML_PARSER_DEBUG
1581        printf("  x_size  = %d\n", x_size);
1582#endif
1583        header->x_size = x_size;
1584    }
1585    else 
1586    {
1587        printf("[XML ERROR] illegal or missing <x_size> attribute in header\n");
1588        exit(1);
1589    }
1590
1591    /////////// get y_size attribute
1592    unsigned int y_size = getIntValue(reader, "y_size", &ok);
1593    if (ok) 
1594    {
1595#if XML_PARSER_DEBUG
1596        printf("  y_size  = %d\n", y_size);
1597#endif
1598        header->y_size = y_size;
1599    }
1600    else 
1601    {
1602        printf("[XML ERROR] illegal or missing <y_size> attribute in header\n");
1603        exit(1);
1604    }
1605
1606    /////////// get x_io attribute
1607    unsigned int x_io = getIntValue(reader, "x_io", &ok);
1608#if XML_PARSER_DEBUG
1609        printf("  x_io      = %d\n", x_io);
1610#endif
1611    if ( ok && (x_io < x_size) ) 
1612    {
1613        header->x_io = x_io;
1614    }
1615    else 
1616    {
1617        printf("[XML ERROR] illegal or missing <x_io> attribute in header\n");
1618        exit(1);
1619    }
1620
1621    /////////// get y_io attribute
1622    unsigned int y_io = getIntValue(reader, "y_io", &ok);
1623#if XML_PARSER_DEBUG
1624        printf("  y_io      = %d\n", y_io);
1625#endif
1626    if ( ok &&(y_io < y_size) ) 
1627    {
1628        header->y_io = y_io;
1629    }
1630    else 
1631    {
1632        printf("[XML ERROR] illegal or missing <y_io> attribute in header\n");
1633        exit(1);
1634    }
1635
1636    // check the number of cluster
1637    if ( (x_size * y_size) >= MAX_CLUSTERS )
1638    {
1639        printf("[XML ERROR] Number of clusters cannot be larger than %d\n", MAX_CLUSTERS);
1640        exit(1);
1641    }
1642
1643    ///////// get irq_per_proc attribute
1644    value = getIntValue(reader, "irq_per_proc", &ok);
1645    if (ok) 
1646    {
1647#if XML_PARSER_DEBUG
1648        printf("  irq_per_proc = %d\n", value);
1649#endif
1650        header->irq_per_proc = value;
1651    }
1652    else 
1653    {
1654        printf("[XML ERROR] illegal or missing <irq_per_proc> attribute in mapping\n");
1655        exit(1);
1656    }
1657
1658    ///////// get use_ram_disk attribute (default = 0)
1659    value = getIntValue(reader, "use_ram_disk", &ok);
1660    if (ok) 
1661    {
1662#if XML_PARSER_DEBUG
1663        printf("  use_ram_disk = %d\n", value);
1664#endif
1665        header->use_ram_disk = value;
1666    }
1667    else 
1668    {
1669        header->use_ram_disk = 0;
1670    }
1671
1672    ///////// set other header fields
1673    header->globals   = 0;
1674    header->vspaces   = 0;
1675    header->psegs     = 0;
1676    header->vsegs     = 0;
1677    header->threads   = 0;
1678    header->procs     = 0;
1679    header->irqs      = 0;
1680    header->periphs   = 0;
1681
1682    int status = xmlTextReaderRead(reader);
1683    while (status == 1) 
1684    {
1685        const char * tag = (const char *) xmlTextReaderConstName(reader);
1686
1687        if      (strcmp(tag, "clusterset")   == 0) { clusterSetNode(reader); }
1688        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
1689        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
1690        else if (strcmp(tag, "#text")        == 0) { }
1691        else if (strcmp(tag, "#comment")     == 0) { }
1692        else if (strcmp(tag, "mapping_info") == 0) 
1693        {
1694#if XML_PARSER_DEBUG
1695            printf("end mapping_info\n");
1696#endif
1697            return;
1698        }
1699        else 
1700        {
1701            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1702            exit(1);
1703        }
1704        status = xmlTextReaderRead(reader);
1705    }
1706} // end headerNode()
1707
1708
1709////////////////////////////////////
1710void BuildTable( int          fdout, 
1711                 const char * type, 
1712                 unsigned int nb_elem,
1713                 unsigned int elem_size, 
1714                 char ** table ) 
1715{
1716    unsigned int i;
1717    for (i = 0; i < nb_elem; i++) 
1718    {
1719        if (elem_size != write(fdout, table[i], elem_size)) 
1720        {
1721            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1722            exit(1);
1723        }
1724
1725#if XML_PARSER_DEBUG
1726        printf("Building binary: writing %s %d\n", type, i);
1727#endif
1728    }
1729}
1730
1731/////////////////////////////////////
1732int open_file(const char * file_path) 
1733{
1734    //open file
1735    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1736    if (fdout < 0) 
1737    {
1738        perror("open");
1739        exit(1);
1740    }
1741
1742    //reinitialise the file
1743    if (ftruncate(fdout, 0)) 
1744    {
1745        perror("truncate");
1746        exit(1);
1747    }
1748
1749    //#if XML_PARSER_DEBUG
1750    printf("%s\n", file_path);
1751    //#endif
1752
1753    return fdout;
1754}
1755
1756
1757/////////////////////////////////////
1758void buildBin(const char * file_path) 
1759{
1760    unsigned int length;
1761
1762    int fdout = open_file(file_path);
1763
1764#if XML_PARSER_DEBUG
1765printf("Building map.bin for %s\n", header->name);
1766printf("signature = %x\n", header->signature);
1767printf("x_size    = %d\n", header->x_size);
1768printf("y_size    = %d\n", header->y_size);
1769printf("x_width   = %d\n", header->x_width);
1770printf("y_width   = %d\n", header->y_width);
1771printf("vspaces   = %d\n", header->vspaces);
1772printf("psegs     = %d\n", header->psegs);
1773printf("vsegs     = %d\n", header->vsegs);
1774printf("threads   = %d\n", header->threads);
1775printf("procs     = %d\n", header->procs);
1776printf("irqs      = %d\n", header->irqs);
1777printf("periphs   = %d\n", header->periphs);
1778#endif
1779
1780    // write header to binary file
1781    length = write(fdout, (char *) header, sizeof(mapping_header_t));
1782    if (length != sizeof(mapping_header_t)) 
1783    {
1784        printf("write header error : length = %d \n", length);
1785        exit(1);
1786    }
1787
1788    // write clusters
1789    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
1790    // write psegs
1791    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
1792    // write vspaces
1793    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
1794    // write vsegs
1795    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
1796    // write threads array
1797    BuildTable(fdout, "thread", thread_index, sizeof(mapping_thread_t), (char **) thread);
1798    //building procs array
1799    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
1800    //building irqs array
1801    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
1802    //building periphs array
1803    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
1804
1805    close(fdout);
1806
1807} // end buildBin()
1808
1809
1810//////////////////////////////////////////////////////
1811char * buildPath(const char * path, const char * name) 
1812{
1813    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
1814    strcat(res, path);
1815    strcat(res, "/");
1816    strcat(res, name);
1817    return res; 
1818}
1819
1820
1821//////////////////////////////////
1822int main(int argc, char * argv[]) 
1823{
1824    if (argc < 3) 
1825    {
1826        printf("Usage: xml2bin <input_file_path> <output_path>\n");
1827        return 1;
1828    }
1829
1830    struct stat dir_st;
1831    if (stat( argv[2], &dir_st)) 
1832    {
1833        perror("bad path");
1834        exit(1);
1835    }
1836
1837    if ((dir_st.st_mode & S_IFDIR) == 0) 
1838    {
1839        printf("path is not a dir: %s", argv[2] );
1840        exit(1);
1841    }
1842
1843    char * map_path = buildPath(argv[2], "map.bin"); 
1844
1845    LIBXML_TEST_VERSION;
1846
1847    int status;
1848    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
1849
1850    if (reader != NULL) 
1851    {
1852        status = xmlTextReaderRead (reader);
1853        while (status == 1) 
1854        {
1855            const char * tag = (const char *) xmlTextReaderConstName(reader);
1856
1857            if (strcmp(tag, "mapping_info") == 0) 
1858            { 
1859                headerNode(reader);
1860                buildBin(map_path);
1861            }
1862            else 
1863            {
1864                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
1865                return 1;
1866            }
1867            status = xmlTextReaderRead(reader);
1868        }
1869        xmlFreeTextReader(reader);
1870
1871        if (status != 0) 
1872        {
1873            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
1874            return 1;
1875        }
1876    }
1877    return 0;
1878} // end main()
1879
1880
1881// Local Variables:
1882// tab-width: 4
1883// c-basic-offset: 4
1884// c-file-offsets:((innamespace . 0)(inline-open . 0))
1885// indent-tabs-mode: nil
1886// End:
1887// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1888
Note: See TracBrowser for help on using the repository browser.