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

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

Introducing support for MWMR coprocessors.

  • 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 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        }
1067        else
1068        {
1069            printf("[XML ERROR] illegal subtype for IOC peripheral\n");
1070            exit(1);
1071        }
1072    }
1073    else
1074    {
1075        periph[periph_index]->subtype = 0XFFFFFFFF;
1076    }
1077
1078    ////////// get subtype if MWR
1079    if (periph[periph_index]->type == PERIPH_TYPE_MWR )
1080    {
1081        char* subtype = getStringValue(reader, "subtype", &ok);
1082        if (ok)
1083        {
1084#if XML_PARSER_DEBUG
1085printf("      subtype     = %s\n", str);
1086#endif
1087            if      (strcmp(subtype, "GCD") == 0) 
1088            periph[periph_index]->subtype = MWR_SUBTYPE_GCD;
1089            else if (strcmp(subtype, "DCT") == 0) 
1090            periph[periph_index]->subtype = MWR_SUBTYPE_DCT;
1091            else if (strcmp(subtype, "CPY") == 0) 
1092            periph[periph_index]->subtype = MWR_SUBTYPE_CPY;
1093        }
1094        else
1095        {
1096            printf("[XML ERROR] illegal subtype for MWR peripheral\n");
1097            exit(1);
1098        }
1099    }
1100    else
1101    {
1102        periph[periph_index]->subtype = 0XFFFFFFFF;
1103    }
1104
1105    ////////////// set irq_offset attribute
1106    periph[periph_index]->irq_offset = irq_index;
1107
1108    ///////////// get IRQs
1109    int status = xmlTextReaderRead(reader);
1110    while (status == 1) 
1111    {
1112        const char * tag = (const char *) xmlTextReaderConstName(reader);
1113
1114        if (strcmp(tag, "irq") == 0) 
1115        {
1116            if ( (periph[periph_index]->type != PERIPH_TYPE_XCU) &&
1117                 (periph[periph_index]->type != PERIPH_TYPE_PIC) )
1118            {
1119                printf("[XML ERROR] periph %d in cluster(%d,%d) "
1120                       " only XCU and PIC can contain IRQs",
1121                periph_loc_index, cluster[cluster_index]->x, cluster[cluster_index]->y);
1122                exit(1);
1123            }
1124            else
1125            {
1126                  irqNode(reader);
1127            }
1128        }
1129        else if (strcmp(tag, "#text")    == 0) { }
1130        else if (strcmp(tag, "#comment") == 0) { }
1131        else if (strcmp(tag, "periph")   == 0) 
1132        {
1133            periph[periph_index]->irqs = irq_loc_index;
1134            cluster[cluster_index]->periphs++;
1135            periph_loc_index++;
1136            periph_index++;
1137
1138#if XML_PARSER_DEBUG
1139printf("      irqs        = %d\n", irq_loc_index);
1140printf("      irq_offset  = %d\n", irq_index);
1141#endif
1142            return;
1143        }
1144        else 
1145        {
1146            printf("[XML ERROR] Unknown tag %s", tag);
1147            exit(1);
1148        }
1149        status = xmlTextReaderRead(reader);
1150    }
1151} // end periphNode
1152
1153//////////////////////////////////////
1154void procNode(xmlTextReaderPtr reader) 
1155{
1156    unsigned int ok;
1157    unsigned int value;
1158
1159    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1160
1161    if (proc_index >= MAX_PROCS) 
1162    {
1163        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1164        exit(1);
1165    }
1166
1167#if XML_PARSER_DEBUG
1168printf("\n    proc %d\n", proc_index);
1169#endif
1170
1171    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1172
1173    /////////// get index attribute (optional)
1174    value = getIntValue(reader, "index", &ok);
1175    if (ok && (value != proc_loc_index)) 
1176    {
1177        printf("[XML ERROR] wrong local proc index / expected value is %d", 
1178                proc_loc_index);
1179        exit(1);
1180    }
1181    proc[proc_index]->index = proc_loc_index;
1182
1183    cluster[cluster_index]->procs++;
1184    proc_loc_index++;
1185    proc_index++;
1186} // end procNode()
1187
1188
1189//////////////////////////////////////
1190void psegNode(xmlTextReaderPtr reader) 
1191{
1192    unsigned int ok;
1193    paddr_t      ll_value;
1194    char * str;
1195
1196    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1197
1198    if (pseg_index >= MAX_PSEGS) 
1199    {
1200        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1201        exit(1);
1202    }
1203
1204#if XML_PARSER_DEBUG
1205printf("    pseg %d\n", pseg_index);
1206#endif
1207
1208    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1209
1210    /////// get name attribute
1211    str = getStringValue(reader, "name", &ok);
1212#if XML_PARSER_DEBUG
1213printf("      name = %s\n", str);
1214#endif
1215    if (ok) 
1216    {
1217        strncpy(pseg[pseg_index]->name, str, 31);
1218    }
1219    else 
1220    {
1221        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1222                pseg_index, cluster_index);
1223        exit(1);
1224    }
1225
1226    //////// get type attribute
1227    str = getStringValue(reader, "type", &ok);
1228#if XML_PARSER_DEBUG
1229printf("      type = %s\n", str);
1230#endif
1231    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1232    else if (ok && (strcmp(str, "ROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1233    else if (ok && (strcmp(str, "DROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1234    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1235    else 
1236    {
1237        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1238                pseg[pseg_index]->name, cluster_index);
1239        exit(1);
1240    }
1241
1242    //////// get base attribute
1243    ll_value = getPaddrValue(reader, "base", &ok);
1244#if XML_PARSER_DEBUG
1245printf("      base = 0x%llx\n", ll_value);
1246#endif
1247    if (ok) 
1248    {
1249        pseg[pseg_index]->base = ll_value;
1250    }
1251    else {
1252        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1253                pseg[pseg_index]->name, cluster_index);
1254        exit(1);
1255    }
1256
1257    //////// get length attribute
1258    ll_value = getPaddrValue(reader, "length", &ok);
1259#if XML_PARSER_DEBUG
1260printf("      length = 0x%llx\n", ll_value);
1261#endif
1262    if (ok) 
1263    {
1264        pseg[pseg_index]->length = ll_value;
1265    } 
1266    else 
1267    {
1268        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1269                pseg[pseg_index]->name, cluster_index);
1270        exit(1);
1271    }
1272
1273    //////// set cluster attribute
1274    pseg[pseg_index]->clusterid = cluster_index;
1275
1276    //////// set next_vseg attribute
1277    pseg[pseg_index]->next_vseg = 0;
1278
1279    pseg_index++;
1280    cluster[cluster_index]->psegs++;
1281} // end psegNode()
1282
1283
1284/////////////////////////////////////////
1285void clusterNode(xmlTextReaderPtr reader) 
1286{
1287    unsigned int ok;
1288    unsigned int value;
1289
1290    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1291
1292    //initialise variables that will be incremented by *Node() functions
1293    cluster[cluster_index]->psegs = 0;
1294    cluster[cluster_index]->procs = 0;
1295    cluster[cluster_index]->coprocs = 0;
1296    cluster[cluster_index]->periphs = 0;
1297
1298    //initialise global variables
1299    proc_loc_index = 0;
1300    coproc_loc_index = 0;
1301    periph_loc_index = 0;
1302
1303    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)  return;
1304
1305#if XML_PARSER_DEBUG
1306printf("\n  cluster %d\n", cluster_index);
1307#endif
1308
1309    /////////// get x coordinate
1310    value = getIntValue(reader, "x", &ok);
1311#if XML_PARSER_DEBUG
1312printf("    x             = %d\n", value);
1313#endif
1314    if (ok && (value < header->x_size) ) 
1315    {
1316        cluster[cluster_index]->x = value;
1317    }
1318    else
1319    {
1320        printf("[XML ERROR] Illegal or missing < x > attribute for cluster %d", 
1321                cluster_index);
1322        exit(1);
1323    }
1324
1325    /////////// get y coordinate
1326    value = getIntValue(reader, "y", &ok);
1327#if XML_PARSER_DEBUG
1328printf("    y             = %d\n", value);
1329#endif
1330    if (ok && (value < header->y_size) ) 
1331    {
1332        cluster[cluster_index]->y = value;
1333    }
1334    else
1335    {
1336        printf("[XML ERROR] Illegal or missing < y > attribute for cluster %d", 
1337                cluster_index);
1338        exit(1);
1339    }
1340
1341    ////////// set offsets
1342    cluster[cluster_index]->pseg_offset = pseg_index;
1343    cluster[cluster_index]->proc_offset = proc_index;
1344    cluster[cluster_index]->coproc_offset = coproc_index;
1345    cluster[cluster_index]->periph_offset = periph_index;
1346
1347#if XML_PARSER_DEBUG
1348printf("    pseg_offset   = %d\n", pseg_index);
1349printf("    proc_offset   = %d\n", proc_index);
1350printf("    coproc_offset = %d\n", coproc_index);
1351printf("    periph_offset = %d\n", coproc_index);
1352#endif
1353
1354    ////////// get psegs, procs, coprocs and periphs
1355    int status = xmlTextReaderRead(reader);
1356
1357    while (status == 1) 
1358    {
1359        const char * tag = (const char *) xmlTextReaderConstName(reader);
1360
1361        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
1362        else if (strcmp(tag, "proc")     == 0) procNode(reader);
1363        else if (strcmp(tag, "coproc")   == 0) coprocNode(reader);
1364        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
1365        else if (strcmp(tag, "#text")    == 0) { }
1366        else if (strcmp(tag, "#comment") == 0) { }
1367        else if (strcmp(tag, "cluster")  == 0) 
1368        {
1369
1370#if XML_PARSER_DEBUG
1371printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1372printf("    procs   = %d\n", cluster[cluster_index]->procs);
1373printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1374printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1375printf("    end cluster %d\n", cluster_index);
1376#endif
1377            cluster_index++;
1378            return;
1379        }
1380        status = xmlTextReaderRead(reader);
1381    }
1382} // end clusterNode()
1383
1384
1385//////////////////////////////////////////////
1386void clusterSetNode(xmlTextReaderPtr reader) 
1387{
1388    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1389
1390#if XML_PARSER_DEBUG
1391printf("\n  clusters set\n");
1392#endif
1393
1394    int status = xmlTextReaderRead(reader);
1395    while (status == 1) 
1396    {
1397        const char * tag = (const char *) xmlTextReaderConstName(reader);
1398
1399        if      (strcmp(tag, "cluster")    == 0) { clusterNode(reader); }
1400        else if (strcmp(tag, "#text")      == 0) { }
1401        else if (strcmp(tag, "#comment")   == 0) { }
1402        else if (strcmp(tag, "clusterset") == 0) 
1403        {
1404            // checking number of clusters
1405            if ( cluster_index != (header->x_size * header->y_size) ) 
1406            {
1407                printf("[XML ERROR] Wrong number of clusters\n");
1408                exit(1);
1409            }
1410
1411#if XML_PARSER_DEBUG
1412            printf("  end cluster set\n\n");
1413#endif
1414            header->psegs = pseg_index;
1415            header->procs = proc_index;
1416            header->irqs = irq_index;
1417            header->coprocs = coproc_index;
1418            header->periphs = periph_index;
1419            return;
1420        }
1421        else 
1422        {
1423            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1424            exit(1);
1425        }
1426        status = xmlTextReaderRead(reader);
1427    }
1428} // end clusterSetNode()
1429
1430
1431///////////////////////////////////////////
1432void globalSetNode(xmlTextReaderPtr reader) 
1433{
1434    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1435
1436#if XML_PARSER_DEBUG
1437    printf("  globals set\n");
1438#endif
1439
1440    int status = xmlTextReaderRead(reader);
1441    while (status == 1) 
1442    {
1443        const char * tag = (const char *) xmlTextReaderConstName(reader);
1444
1445        if      (strcmp(tag, "vseg")      == 0) 
1446        { 
1447            vsegNode( reader ); 
1448            header->globals = header->globals + 1;
1449        }
1450        else if (strcmp(tag, "#text")     == 0) { }
1451        else if (strcmp(tag, "#comment")  == 0) { }
1452        else if (strcmp(tag, "globalset") == 0) 
1453        {
1454#if XML_PARSER_DEBUG
1455            printf("  end global set\n\n");
1456#endif
1457            vseg_loc_index = 0;
1458            return;
1459        }
1460        else 
1461        {
1462            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1463            exit(1);
1464        }
1465        status = xmlTextReaderRead(reader);
1466    }
1467} // end globalSetNode()
1468
1469
1470///////////////////////////////////////////
1471void vspaceSetNode(xmlTextReaderPtr reader)
1472{
1473    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1474        return;
1475    }
1476
1477#if XML_PARSER_DEBUG
1478    printf("\n  vspaces set\n");
1479#endif
1480
1481    int status = xmlTextReaderRead ( reader );
1482    while (status == 1) {
1483        const char * tag = (const char *) xmlTextReaderConstName(reader);
1484
1485        if (strcmp(tag, "vspace") == 0) {
1486            vspaceNode(reader);
1487        }
1488        else if (strcmp(tag, "#text"    ) == 0 ) { }
1489        else if (strcmp(tag, "#comment" ) == 0 ) { }
1490        else if (strcmp(tag, "vspaceset") == 0 ) 
1491        {
1492            header->vsegs = vseg_index;
1493            header->tasks = task_index;
1494            return;
1495        }
1496        else 
1497        {
1498            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1499            exit(1);
1500        }
1501        status = xmlTextReaderRead(reader);
1502    }
1503} // end globalSetNode()
1504
1505
1506////////////////////////////////////////
1507void headerNode(xmlTextReaderPtr reader) 
1508{
1509    char * name;
1510    unsigned int value;
1511    unsigned int ok;
1512
1513    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1514
1515#if XML_PARSER_DEBUG
1516    printf("mapping_info\n");
1517#endif
1518
1519    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
1520
1521    ////////// get name attribute
1522    name = getStringValue(reader, "name", &ok);
1523    if (ok) 
1524    {
1525#if XML_PARSER_DEBUG
1526        printf("  name = %s\n", name);
1527#endif
1528        strncpy( header->name, name, 31);
1529    }
1530    else 
1531    {
1532        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1533        exit(1);
1534    }
1535
1536    /////////// get signature attribute
1537    value = getIntValue(reader, "signature", &ok);
1538    if ( ok && (value == IN_MAPPING_SIGNATURE) )
1539    {
1540#if XML_PARSER_DEBUG
1541        printf("  signature = %x\n", value);
1542#endif
1543        header->signature = IN_MAPPING_SIGNATURE;
1544    }
1545    else
1546    {
1547        printf("[XML ERROR] illegal or missing <signature> for mapping %s\n",
1548        header->name );
1549        exit(1);
1550    }
1551
1552    /////////// get x_width attribute
1553    value = getIntValue(reader, "x_width", &ok);
1554    if (ok) 
1555    {
1556#if XML_PARSER_DEBUG
1557        printf("  x_width = %d\n", value);
1558#endif
1559        header->x_width = value;
1560    }
1561
1562    /////////// get y_width attribute
1563    value = getIntValue(reader, "y_width", &ok);
1564    if (ok) 
1565    {
1566#if XML_PARSER_DEBUG
1567        printf("  y_width = %d\n", value);
1568#endif
1569        header->y_width = value;
1570    }
1571
1572    /////////// get x_size attribute
1573    unsigned int x_size = getIntValue(reader, "x_size", &ok);
1574    if (ok) 
1575    {
1576#if XML_PARSER_DEBUG
1577        printf("  x_size  = %d\n", x_size);
1578#endif
1579        header->x_size = x_size;
1580    }
1581    else 
1582    {
1583        printf("[XML ERROR] illegal or missing <x_size> attribute in header\n");
1584        exit(1);
1585    }
1586
1587    /////////// get y_size attribute
1588    unsigned int y_size = getIntValue(reader, "y_size", &ok);
1589    if (ok) 
1590    {
1591#if XML_PARSER_DEBUG
1592        printf("  y_size  = %d\n", y_size);
1593#endif
1594        header->y_size = y_size;
1595    }
1596    else 
1597    {
1598        printf("[XML ERROR] illegal or missing <y_size> attribute in header\n");
1599        exit(1);
1600    }
1601
1602    /////////// get x_io attribute
1603    unsigned int x_io = getIntValue(reader, "x_io", &ok);
1604#if XML_PARSER_DEBUG
1605        printf("  x_io      = %d\n", x_io);
1606#endif
1607    if ( ok && (x_io < x_size) ) 
1608    {
1609        header->x_io = x_io;
1610    }
1611    else 
1612    {
1613        printf("[XML ERROR] illegal or missing <x_io> attribute in header\n");
1614        exit(1);
1615    }
1616
1617    /////////// get y_io attribute
1618    unsigned int y_io = getIntValue(reader, "y_io", &ok);
1619#if XML_PARSER_DEBUG
1620        printf("  y_io      = %d\n", y_io);
1621#endif
1622    if ( ok &&(y_io < y_size) ) 
1623    {
1624        header->y_io = y_io;
1625    }
1626    else 
1627    {
1628        printf("[XML ERROR] illegal or missing <y_io> attribute in header\n");
1629        exit(1);
1630    }
1631
1632    // check the number of cluster
1633    if ( (x_size * y_size) >= MAX_CLUSTERS )
1634    {
1635        printf("[XML ERROR] Number of clusters cannot be larger than %d\n", MAX_CLUSTERS);
1636        exit(1);
1637    }
1638
1639    ///////// get irq_per_proc attribute
1640    value = getIntValue(reader, "irq_per_proc", &ok);
1641    if (ok) 
1642    {
1643#if XML_PARSER_DEBUG
1644        printf("  irq_per_proc = %d\n", value);
1645#endif
1646        header->irq_per_proc = value;
1647    }
1648    else 
1649    {
1650        printf("[XML ERROR] illegal or missing <irq_per_proc> attribute in mapping\n");
1651        exit(1);
1652    }
1653
1654    ///////// get use_ram_disk attribute (default = 0)
1655    value = getIntValue(reader, "use_ram_disk", &ok);
1656    if (ok) 
1657    {
1658#if XML_PARSER_DEBUG
1659        printf("  use_ram_disk = %d\n", value);
1660#endif
1661        header->use_ram_disk = value;
1662    }
1663    else 
1664    {
1665        header->use_ram_disk = 0;
1666    }
1667
1668    ///////// set other header fields
1669    header->globals   = 0;
1670    header->vspaces   = 0;
1671    header->psegs     = 0;
1672    header->vsegs     = 0;
1673    header->tasks     = 0;
1674    header->procs     = 0;
1675    header->irqs      = 0;
1676    header->coprocs   = 0;
1677    header->periphs   = 0;
1678
1679
1680    int status = xmlTextReaderRead(reader);
1681    while (status == 1) 
1682    {
1683        const char * tag = (const char *) xmlTextReaderConstName(reader);
1684
1685        if      (strcmp(tag, "clusterset")   == 0) { clusterSetNode(reader); }
1686        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
1687        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
1688        else if (strcmp(tag, "#text")        == 0) { }
1689        else if (strcmp(tag, "#comment")     == 0) { }
1690        else if (strcmp(tag, "mapping_info") == 0) 
1691        {
1692#if XML_PARSER_DEBUG
1693            printf("end mapping_info\n");
1694#endif
1695            return;
1696        }
1697        else 
1698        {
1699            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1700            exit(1);
1701        }
1702        status = xmlTextReaderRead(reader);
1703    }
1704} // end headerNode()
1705
1706
1707////////////////////////////////////
1708void BuildTable( int          fdout, 
1709                 const char * type, 
1710                 unsigned int nb_elem,
1711                 unsigned int elem_size, 
1712                 char ** table ) 
1713{
1714    unsigned int i;
1715    for (i = 0; i < nb_elem; i++) 
1716    {
1717        if (elem_size != write(fdout, table[i], elem_size)) 
1718        {
1719            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1720            exit(1);
1721        }
1722
1723#if XML_PARSER_DEBUG
1724        printf("Building binary: writing %s %d\n", type, i);
1725#endif
1726    }
1727}
1728
1729/////////////////////////////////////
1730int open_file(const char * file_path) 
1731{
1732    //open file
1733    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1734    if (fdout < 0) 
1735    {
1736        perror("open");
1737        exit(1);
1738    }
1739
1740    //reinitialise the file
1741    if (ftruncate(fdout, 0)) 
1742    {
1743        perror("truncate");
1744        exit(1);
1745    }
1746
1747    //#if XML_PARSER_DEBUG
1748    printf("%s\n", file_path);
1749    //#endif
1750
1751    return fdout;
1752}
1753
1754
1755/////////////////////////////////////
1756void buildBin(const char * file_path) 
1757{
1758    unsigned int length;
1759
1760    int fdout = open_file(file_path);
1761
1762#if XML_PARSER_DEBUG
1763printf("Building map.bin for %s\n", header->name);
1764printf("signature = %x\n", header->signature);
1765printf("x_size    = %d\n", header->x_size);
1766printf("y_size    = %d\n", header->y_size);
1767printf("x_width   = %d\n", header->x_width);
1768printf("y_width   = %d\n", header->y_width);
1769printf("vspaces   = %d\n", header->vspaces);
1770printf("psegs     = %d\n", header->psegs);
1771printf("vsegs     = %d\n", header->vsegs);
1772printf("tasks     = %d\n", header->tasks);
1773printf("procs     = %d\n", header->procs);
1774printf("irqs      = %d\n", header->irqs);
1775printf("coprocs   = %d\n", header->coprocs);
1776printf("periphs   = %d\n", header->periphs);
1777#endif
1778
1779    // write header to binary file
1780    length = write(fdout, (char *) header, sizeof(mapping_header_t));
1781    if (length != sizeof(mapping_header_t)) 
1782    {
1783        printf("write header error : length = %d \n", length);
1784        exit(1);
1785    }
1786
1787    // write clusters
1788    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
1789    // write psegs
1790    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
1791    // write vspaces
1792    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
1793    // write vsegs
1794    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
1795    // write tasks array
1796    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char **) task);
1797    //building procs array
1798    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
1799    //building irqs array
1800    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
1801    //building coprocs array
1802    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char **) coproc);
1803    //building periphs array
1804    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
1805
1806    close(fdout);
1807
1808} // end buildBin()
1809
1810
1811//////////////////////////////////////////////////////
1812char * buildPath(const char * path, const char * name) 
1813{
1814    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
1815    strcat(res, path);
1816    strcat(res, "/");
1817    strcat(res, name);
1818    return res; 
1819}
1820
1821
1822//////////////////////////////////
1823int main(int argc, char * argv[]) 
1824{
1825    if (argc < 3) 
1826    {
1827        printf("Usage: xml2bin <input_file_path> <output_path>\n");
1828        return 1;
1829    }
1830
1831    struct stat dir_st;
1832    if (stat( argv[2], &dir_st)) 
1833    {
1834        perror("bad path");
1835        exit(1);
1836    }
1837
1838    if ((dir_st.st_mode & S_IFDIR) == 0) 
1839    {
1840        printf("path is not a dir: %s", argv[2] );
1841        exit(1);
1842    }
1843
1844    char * map_path = buildPath(argv[2], "map.bin"); 
1845
1846    LIBXML_TEST_VERSION;
1847
1848    int status;
1849    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
1850
1851    if (reader != NULL) 
1852    {
1853        status = xmlTextReaderRead (reader);
1854        while (status == 1) 
1855        {
1856            const char * tag = (const char *) xmlTextReaderConstName(reader);
1857
1858            if (strcmp(tag, "mapping_info") == 0) 
1859            { 
1860                headerNode(reader);
1861                buildBin(map_path);
1862            }
1863            else 
1864            {
1865                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
1866                return 1;
1867            }
1868            status = xmlTextReaderRead(reader);
1869        }
1870        xmlFreeTextReader(reader);
1871
1872        if (status != 0) 
1873        {
1874            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
1875            return 1;
1876        }
1877    }
1878    return 0;
1879} // end main()
1880
1881
1882// Local Variables:
1883// tab-width: 4
1884// c-basic-offset: 4
1885// c-file-offsets:((innamespace . 0)(inline-open . 0))
1886// indent-tabs-mode: nil
1887// End:
1888// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1889
Note: See TracBrowser for help on using the repository browser.