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

Last change on this file since 610 was 567, checked in by alain, 9 years ago

Introduce support for the IOC_SUBTYPE_SDC.

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