home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 5 / CD_Magazyn_EXEC_nr_5.iso / Programy / MorphOS / morphosdev-150201.lha / emultools / geninclude.pl < prev    next >
Encoding:
Perl Script  |  2001-02-14  |  5.7 KB  |  239 lines

  1. #! /bin/perl
  2. #
  3. # Extends include-header-files with __attribute__((packed))
  4. # to make ixemul and os-includes ppc-compatible
  5. #
  6. # 07.06.98 Samuel Devulder: __attribute((aligned(2)) for all >2 byte elements
  7. # 18.06.98 Holger Jakob:    It is not enough to use a modified
  8. #                           exec/types.h only :(
  9. # 21.05.2000 Emmanuel Lesueur: don't put __attribute__((aligned(2)) for structure
  10. #                              elements that are on correct boudaries.
  11. #
  12. require 5.002;
  13.  
  14. # structs that have a length non multiple of 4:
  15.  
  16. %oddstructs=(
  17.     AChain => 2,
  18.     AnchorPath => 1,
  19.     AnimComp => 2,
  20.     AnimOb => 2,
  21.     AppMessage => 2,
  22.     AvailFonts => 2,
  23.     AvailFontsHeader => 2,
  24.     CardHandle => 1,
  25.         CDInfo => 2,
  26.     CIA => 2,
  27.     ClipboardUnitPartial => 2,
  28.     ClockData => 2,
  29.     ColorRegister => 1,
  30.     Conductor => 1,
  31.     CopIns => 2,
  32.     CopList => 2,
  33.     Custom => 2,
  34.     DTSpecialInfo => 2,
  35.     DataType => 2,
  36.     DateTime => 2,
  37.     Device => 2,
  38.     DeviceTData => 2,
  39.     DiscResourceUnit => 2,
  40.     DiskFontHeader => 2,
  41.     DiskObject => 2,
  42.     DosInfo => 2,
  43.     DosLibrary => 2,
  44.     DrawInfo => 2,
  45.     DrawerData => 2,
  46.     FileSysEntry => 2,
  47.     GadgetInfo => 2,
  48.     GlyphWidthEntry => 2,
  49.     IEPointerTablet => 2,
  50.     IODRPReq => 2,
  51.     IOExtPar => 2,
  52.     IOExtSer => 2,
  53.     IOPrtCmdReq => 2,
  54.     InputEvent => 2,
  55.     Interrupt => 2,
  56.     Isrvstr => 2,
  57.     KeyMapNode => 2,
  58.     Layer_Info => 2,
  59.     Library => 2,
  60.     List => 2,
  61.     MPEGBorderParams => 1,
  62.     Menu => 2,
  63.     MsgPort => 2,
  64.     Node => 2,
  65.     NotifyMessage => 2,
  66.     PrefHeader => 2,
  67.     PrinterData => 2,
  68.     PrinterExtendedData => 2,
  69.     PrinterGfxPrefs => 2,
  70.     PrinterSegment => 2,
  71.     PrtInfo => 2,
  72.     PubScreenNode => 1,
  73.     RGBTable => 1,
  74.     Resident => 2,
  75.     RexxTask => 2,
  76.     SCSICmd => 2,
  77.     SatisfyMsg => 2,
  78.     SerialPrefs => 1,
  79.     SignalSemaphore => 2,
  80.     SpecialMonitor => 2,
  81.     TAvailFonts => 2,
  82.     TOCEntry => 2,
  83.     TOCSummary => 2,
  84.     ToolNode => 2,
  85.     Unit => 2,
  86.     View => 2,
  87.     ViewPortExtra => 2,
  88.     bltnode => 2,
  89.     cprlist => 2,
  90. );
  91.  
  92. undef $/;
  93.  
  94. die "no include" if not $include=@ARGV[0];
  95. print STDERR "Copying $include ... ";
  96.  
  97. open(INP,"<$include");
  98. $source=<INP>;
  99.  
  100. # Check for additional patches here...
  101. # user.h, (screens.h), setjmp.h...
  102.  
  103. #if( $include=~ /include\/user.h$/i ) { $source=~ s/(\W)jmp_buf(\W)/$1jmp_buf_m68k$2/g; }
  104. #if( $include=~ /include\/setjmp.h$/i ) {
  105. #  $source=~ s/(#define\s+_JBLEN)(\s+\d+)/$1_M68K $2\n$1\t\t26+17*2/;
  106. #  $source=~ s/(typedef\s+int\s+sigjmp_buf)(.*)(_JBLEN)(.*)/$1_m68k$2$3_M68K$4\n$1$2$3$4/;
  107. #  $source=~ s/(typedef\s+int\s+jmp_buf)(.*)(_JBLEN)(.*)/$1_m68k$2$3_M68K$4\n$1$2$3$4/;
  108. #}
  109. if( $include=~ /include\/sys\/syscall.h$/i ) { $source=~ s/#ifndef\s+?_KERNEL(.*?)#endif//s; }
  110.  
  111. #
  112. #
  113.  
  114. $source=~ s/\/\*.*?\*\///sg;  # Sorry, no comments
  115. $source=~ s/^(\s*)struct((.|\n)*?)({(.|\n)*?});/&ins_packed_struct($2,$4)/meg;
  116. # ToDo: same for typdef
  117. #$source=~ s/^typedef((.|\n)*?)((\w|\n)*?);/&ins_packed_typedef($1,$3)/meg;
  118. print $source;
  119.  
  120. close(INP);
  121. print STDERR "Applied ";
  122. print STDERR ($source=~ s/__attribute/__attribute/g) || "no";
  123. print STDERR " patches.\n";
  124.  
  125. $alignment=0;
  126. $max_align=0;
  127.  
  128. sub ins_packed_struct
  129. {
  130.     local ($name,$text)=@_;
  131.     local ($return);
  132.  
  133.     $alignment=0;
  134.     $max_align=0;
  135.  
  136. #       $text=~ s/(LONG|struct)([^;])/$1$2 __attribute__((aligned(2))) /g;
  137.  
  138.     $return="struct".$name.$text." __attribute__((packed));";
  139.  
  140. #FIXME: /* ; */ is not recogniced and 2-word types(eg. unsigned int) as well
  141. #FIXED!?
  142.     $return=~ s/^(\s*)(\w*)(\s*)([a-zA-Z0-9_]*)(.*?);/&ins_align($1,$2,$3,$4,$5)/ge;
  143.  
  144.     if($max_align>1 && $alignment>0 && ($alignment&1)!=0) {
  145.         $return=~ s/(.*)}(\s*__attribute__\(\(packed\)\).*)/$1\tchar __pad__;\n}$2/;
  146.     }
  147.     return $return;
  148. }
  149. sub ins_packed_typedef
  150. {
  151.     local ($text,$name)=@_;
  152.     local ($return);
  153.  
  154.  
  155. #       $text=~ s/(LONG|struct)([^;])/$1$2 __attribute__((aligned(2))) /g;
  156.  
  157. #       $return="struct".$name.$text." __attribute__((packed));";
  158.  
  159. #FIXME: /* ; */ is not recogniced and 2-word types(eg. unsigned int) as well
  160. #FIXED!?
  161. #       $return=~ s/^(\s*)(\w*)(\s*)([a-zA-Z_]*)(.*?);/&ins_align($1,$2,$3,$4,$5)/ge;
  162.  
  163.     return "typedef $name;";
  164. }
  165.  
  166. sub ins_align
  167. {
  168.     local ($space,$type,$space2,$type2,$part1)=@_;
  169.     local ($size,$x);
  170.     $size=0;
  171.  
  172.     if ( $part1=~ /^\s*\*/ ) {
  173.         $size=4;
  174.         if ( $alignment!= 0 ) {
  175.         if ( $part1=~ /^\s*(\**)(\w*)(.*)/ ) {
  176.             $part1=$1." __attribute__((aligned(2))) ".$2.$3;
  177.         }
  178.         }
  179.     } elsif( $type=~ /^(BYTE|UBYTE|BYTEBITS|TEXT|char)$/ ) {
  180.         $size=1;
  181.     } elsif( $type=~ /^(WORD|UWORD|SHORT|USHORT|BOOL|COUNT|UCOUNT|WORDBITS|short)$/ ) {
  182.         if ($alignment&1) {
  183.         $type=$type." __attribute__((aligned(2)))";
  184.         }
  185.         $size=2;
  186.     } elsif( $type=~ /^struct$/ ) {
  187.         if ($alignment != 0) {
  188.         $type2=$type2." __attribute__((aligned(2)))";
  189.         }
  190.         if( exists $oddstructs{$type2} ) {
  191.         $size=$oddstructs{$type2};
  192.         } else {
  193.         $size=4;
  194.         }
  195.     } elsif( $alignment!=0 ) {
  196.         if( $type=~ /^([AC]PTR|STRPTR|LONG|LONGBITS|ULONG|FLOAT|DOUBLE|BPTR|BSTR)$/ ) {
  197.         $type=$type." __attribute__((aligned(2)))";
  198.         $size=4;
  199.         } elsif( $type=~ /^unsigned$/ ) {
  200.         $type2=$type2." __attribute__((aligned(2)))";
  201.         $size=4;
  202.         } elsif( $type=~ /^(int|long)$/ ) {
  203.         $type=$type." __attribute__((aligned(2)))";
  204.         $size=4;
  205.         } elsif( $part1=~ /^(\*|\(\*)/ ) {
  206.         $type=$type." __attribute__((aligned(2)))";
  207.         $size=4;
  208.         }
  209.     }
  210.     if( $alignment==1 && $size>1 ) {
  211.         $alignment=2;
  212.     } elsif( $alignment==3 && $size>1) {
  213.         $alignment=0;
  214.     }
  215.     if( $size!=0 && $alignment!=-1 ) {
  216.         $x=$part1;
  217.         while ($alignment!=-1 && $x=~/\[/) {
  218.         if( $x=~ /\[(\d*)\](.*)/ ) {
  219.             $size*=$1;
  220.             $x=$2;
  221.         } else {
  222.             $alignment=-1;
  223.         }
  224.         }
  225.         $x=$part1;
  226.         $x=~s/\[.*\]//g;
  227.         $x=~s/[^,]//g;
  228.         $size*=length($x)+1;
  229.         if( $alignment!=-1 ) {
  230.         $alignment=($alignment+$size)&3;
  231.         }
  232.     }
  233.     if( $size > $max_align) {
  234.         $max_align = $size;
  235.     }
  236. #        return "/* ".$alignment.",".$max_align." */ ".$space.$type.$space2.$type2.$part1.";";
  237.     return $space.$type.$space2.$type2.$part1.";";
  238. }
  239.