home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / patches / gcc-2_95_2-x86-win32-patches.zi / gcc-2.95.2-patches / broken-down / gcc-2.95.2-pragma-pack.diff < prev    next >
Encoding:
Text File  |  1999-11-08  |  14.4 KB  |  496 lines

  1. Fri Jul  2 22:13:14 1999  Mumit Khan  <khan@xraylith.wisc.edu>
  2.  
  3.     * c-pragma.h (PRAGMA_INSERT_ATTRIBUTES): Delete macro.
  4.     (insert_pack_attributes): Delete prototype.
  5.     (enum pragma_state): Simplify.
  6.  
  7.     * c-pragma.c (struct align_stack): Lose num_pushes field.
  8.     (push_alignment): Remove redundant check for valid alignment;
  9.     always push on stack.
  10.     (pop_alignment): Update prototype. Implement combination rule.
  11.     (insert_pack_attributes): Lose.
  12.     (handle_pragma_token): Document syntax. Simplify pack(push/pop)
  13.     attributes and implement documented syntax.
  14.  
  15. Index: gcc-2.95.2/gcc/c-pragma.c
  16. ===================================================================
  17. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/c-pragma.c,v
  18. retrieving revision 1.1.1.1
  19. diff -u -3 -p -r1.1.1.1 c-pragma.c
  20. --- gcc-2.95.2/gcc/c-pragma.c    1999/11/05 01:09:41    1.1.1.1
  21. +++ gcc-2.95.2/gcc/c-pragma.c    1999/11/05 06:37:53
  22. @@ -44,7 +44,6 @@ extern int maximum_field_alignment;
  23.  typedef struct align_stack
  24.  {
  25.    int                  alignment;
  26. -  unsigned int         num_pushes;
  27.    tree                 id;
  28.    struct align_stack * prev;
  29.  } align_stack;
  30. @@ -52,7 +51,7 @@ typedef struct align_stack
  31.  static struct align_stack * alignment_stack = NULL;
  32.  
  33.  static int  push_alignment PROTO((int, tree));
  34. -static int  pop_alignment  PROTO((tree));
  35. +static int  pop_alignment  PROTO((int, tree));
  36.  
  37.  /* Push an alignment value onto the stack.  */
  38.  static int
  39. @@ -60,159 +59,88 @@ push_alignment (alignment, id)
  40.       int alignment;
  41.       tree id;
  42.  {
  43. -  switch (alignment)
  44. +  align_stack * entry;
  45. +
  46. +  entry = (align_stack *) xmalloc (sizeof (* entry));
  47. +
  48. +  if (entry == NULL)
  49.      {
  50. -    case 0:
  51. -    case 1:
  52. -    case 2:
  53. -    case 4:
  54. -    case 8:
  55. -    case 16:
  56. -      break;
  57. -    default:
  58. -      warning ("\
  59. -Alignment must be a small power of two, not %d, in #pragma pack",
  60. -           alignment);
  61. +      warning ("Out of memory pushing #pragma pack");
  62.        return 0;
  63.      }
  64. -  
  65. -  if (alignment_stack == NULL
  66. -      || alignment_stack->alignment != alignment
  67. -      || id != NULL_TREE)
  68. -    {
  69. -      align_stack * entry;
  70.  
  71. -      entry = (align_stack *) xmalloc (sizeof (* entry));
  72. -
  73. -      if (entry == NULL)
  74. -    {
  75. -      warning ("Out of memory pushing #pragma pack");
  76. -      return 0;
  77. -    }
  78. +  entry->alignment  = maximum_field_alignment;
  79. +  entry->id         = id;
  80. +  entry->prev       = alignment_stack;
  81.  
  82. -      entry->alignment  = alignment;
  83. -      entry->num_pushes = 1;
  84. -      entry->id         = id;
  85. -      entry->prev       = alignment_stack;
  86. -      
  87. -      alignment_stack = entry;
  88. +  alignment_stack = entry;
  89.  
  90. -      maximum_field_alignment = alignment * 8;
  91. -    }
  92. -  else
  93. -    alignment_stack->num_pushes ++;
  94. +  maximum_field_alignment = alignment;
  95.  
  96.    return 1;
  97.  }
  98.  
  99. -/* Undo a push of an alignment onto the stack.  */
  100. +/* Undo a push of an alignment onto the stack.  If the alignment argument
  101. +   is > 0, then make that the new alignment after popping the stack instead 
  102. +   of using TOS. See the combination rules in the #pragma pack(pop[,id][,n]) 
  103. +   syntax for details.  */
  104.  static int
  105. -pop_alignment (id)
  106. +pop_alignment (alignment, id)
  107. +     int alignment;
  108.       tree id;
  109.  {
  110.    align_stack * entry;
  111. -      
  112. -  if (alignment_stack == NULL)
  113. +
  114. +  /* If popping an empty stack, revert to default packing. Defer warning
  115. +     if we got an identifier.  */
  116. +  if (alignment_stack == NULL && id == NULL)
  117.      {
  118. -      warning ("\
  119. -#pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
  120. -           );
  121. -      return 0;
  122. +      warning 
  123. +        ("#pragma pack(pop) encountered without matching #pragma pack(push)");
  124. +      maximum_field_alignment = 0;
  125. +      return 1;
  126.      }
  127.  
  128.    /* If we got an identifier, strip away everything above the target
  129. -     entry so that the next step will restore the state just below it.  */
  130. +     entry so that the next step will restore the state that existed
  131. +     just before the identifier was pushed on the stack.  */
  132.    if (id)
  133. -    {
  134. -      for (entry = alignment_stack; entry; entry = entry->prev)
  135. -    if (entry->id == id)
  136. -      {
  137. -        entry->num_pushes = 1;
  138. -        alignment_stack = entry;
  139. -        break;
  140. -      }
  141. -      if (entry == NULL)
  142. -    warning ("\
  143. -#pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
  144. -         , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
  145. -    }
  146. -
  147. -  if (-- alignment_stack->num_pushes == 0)
  148.      {
  149. -      entry = alignment_stack->prev;
  150. +      align_stack * popped = NULL;
  151. +      for (entry = alignment_stack; entry; 
  152. +           popped = entry, entry = entry->prev)
  153. +    {
  154. +      if (popped)
  155. +        free (popped);
  156.  
  157. +      if (entry->id == id)
  158. +        {
  159. +          alignment_stack = entry;
  160. +          break;
  161. +        }
  162. +    }
  163.        if (entry == NULL)
  164. -    maximum_field_alignment = 0;
  165. -      else
  166. -    maximum_field_alignment = entry->alignment * 8;
  167. -
  168. -      free (alignment_stack);
  169. -
  170. -      alignment_stack = entry;
  171. +        {
  172. +      warning
  173. +        ("#pragma pack(pop,%s) encountered without matching #pragma pack(push,%s)",
  174. +         IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
  175. +      alignment_stack = NULL;
  176. +      maximum_field_alignment = 0;
  177. +      return 1;
  178. +    }
  179.      }
  180.  
  181. -  return 1;
  182. -}
  183. +  entry = alignment_stack->prev;
  184.  
  185. -/* Generate 'packed' and 'aligned' attributes for decls whilst a
  186. -   #pragma pack(push... is in effect.  */
  187. -void
  188. -insert_pack_attributes (node, attributes, prefix)
  189. -     tree node;
  190. -     tree * attributes;
  191. -     tree * prefix;
  192. -{
  193. -  tree a;
  194. -  int field_alignment;
  195. +  /* The new alignment is either a user specified one (combination rule)
  196. +     or restored from the stack. */
  197. +  maximum_field_alignment = 
  198. +    (alignment > 0) ? alignment : alignment_stack->alignment;
  199.  
  200. -  /* If we are not packing, then there is nothing to do.  */
  201. -  if (maximum_field_alignment == 0
  202. -      || alignment_stack == NULL)
  203. -    return;
  204. -
  205. -  /* We are only interested in fields.  */
  206. -  if (TREE_CODE_CLASS (TREE_CODE (node)) != 'd'
  207. -      || TREE_CODE (node) != FIELD_DECL)
  208. -    return;
  209. -  
  210. -  field_alignment = TYPE_ALIGN (TREE_TYPE (node));
  211. -  if (field_alignment <= 0 || field_alignment > maximum_field_alignment)
  212. -    field_alignment = maximum_field_alignment;
  213. -
  214. -  /* Add a 'packed' attribute.  */
  215. -  * attributes = tree_cons (get_identifier ("packed"), NULL, * attributes);
  216. -  
  217. -  /* If the alignment is > 8 then add an alignment attribute as well.  */
  218. -  if (field_alignment > 8)
  219. -    {
  220. -      /* If the aligned attribute is already present then do not override it.  */
  221. -      for (a = * attributes; a; a = TREE_CHAIN (a))
  222. -    {
  223. -      tree name = TREE_PURPOSE (a);
  224. -      if (strcmp (IDENTIFIER_POINTER (name), "aligned") == 0)
  225. -        break;
  226. -    }
  227. -      
  228. -      if (a == NULL)
  229. -    for (a = * prefix; a; a = TREE_CHAIN (a))
  230. -      {
  231. -        tree name = TREE_PURPOSE (a);
  232. -        if (strcmp (IDENTIFIER_POINTER (name), "aligned") == 0)
  233. -          break;
  234. -      }
  235. -  
  236. -      if (a == NULL)
  237. -    {
  238. -      * attributes = tree_cons
  239. -          (get_identifier ("aligned"),
  240. -           tree_cons (NULL,
  241. -              build_int_2 (field_alignment / 8, 0),
  242. -              NULL),
  243. -           * attributes);
  244. -    }
  245. -    }
  246. +  free (alignment_stack);
  247. +  alignment_stack = entry;
  248.  
  249. -  return;
  250. +  return 1;
  251.  }
  252.  #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
  253.  
  254. @@ -227,8 +155,52 @@ insert_pack_attributes (node, attributes
  255.     the line containing the #pragma directive.  If STRING is NULL, the entire
  256.     line has now been presented to handle_pragma_token() and the return value
  257.     should be zero if the pragma flawed in some way, or if the pragma was not
  258. -   recognised, and non-zero if it was successfully handled.  */
  259. +   recognised, and non-zero if it was successfully handled.
  260.  
  261. +   If HANDLE_PRAGMA_WEAK is defined, handle the weak directives using
  262. +   the following syntax:
  263. +
  264. +   #pragma weak name [= alias]
  265. +
  266. +   If HANDLE_PRAGMA_PACK is defined, handle the pack directives using 
  267. +   the following syntax:
  268. +                                       
  269. +   #pragma pack([n])
  270. +   
  271. +   Specifies packing alignment for structure and union members. The 
  272. +   packing alignment is set at the data-declaration level by the pack 
  273. +   pragma. The pragma takes effect at the first structure or union 
  274. +   declaration after the pragma is seen; the pragma has no effect on 
  275. +   definitions.
  276. +   
  277. +   When you use #pragma pack(n), where n is 1, 2, 4, 8, or 16, each
  278. +   structure member after the first is stored on the smaller member type
  279. +   or n-byte boundaries. If you use #pragma pack without an argument,
  280. +   the alignment reverts to the default for the target.
  281. +   
  282. +   If HANDLE_PRAGMA_PACK_PUSH_POP is defined, also handle the 
  283. +   following syntax:
  284. +   
  285. +   #pragma pack({ push | pop} [,identifier] [,n])
  286. +   
  287. +   Each occurrence of a #pragma pack with a push argument stores the
  288. +   current packing alignment on a stack; If you provide a value for n, 
  289. +   that value becomes the new packing value. If you specify an 
  290. +   identifier, the identifier is associated with the new packing value.
  291. +   
  292. +   Each occurrence of a #pragma pack with a pop argument pops the stack
  293. +   and uses the value the new packing alignment. If you use pop and the 
  294. +   internal compiler stack is empty, the new alignment is set to be
  295. +   the default alignment for target, and an warning is issued.  If you 
  296. +   use pop and specify a value for n, that value becomes the new packing 
  297. +   value. If you use pop and specify an identifier, all values stored on 
  298. +   the stack are removed from the stack until a matching identifier is 
  299. +   found. The packing value associated with the identifier is also 
  300. +   removed from the stack and the packing value that existed just before 
  301. +   the identifier was pushed becomes the new packing value. If no 
  302. +   matching identifier is found, the packing value set to the default
  303. +   and a warning is issued.  */
  304. +
  305.  int
  306.  handle_pragma_token (string, token)
  307.       const char * string;
  308. @@ -238,7 +210,7 @@ handle_pragma_token (string, token)
  309.    static enum pragma_state type;
  310.    static char * name;
  311.    static char * value;
  312. -  static int align;
  313. +  static int align = -1;
  314.    static tree id;
  315.  
  316.    /* If we have reached the end of the #pragma directive then
  317. @@ -262,11 +234,14 @@ handle_pragma_token (string, token)
  318.      case ps_pack:
  319.        if (state == ps_right)
  320.          {
  321. -          maximum_field_alignment = align * 8;
  322. +          maximum_field_alignment = align;
  323.            ret_val = 1;
  324.          }
  325.        else
  326. -        warning ("malformed `#pragma pack'");
  327. +        {
  328. +          ret_val = 1; /* Ignore the pragma.  */
  329. +          warning ("malformed `#pragma pack'");
  330. +        }
  331.        break;
  332.  #endif /* HANDLE_PRAGMA_PACK */
  333.        
  334. @@ -275,14 +250,20 @@ handle_pragma_token (string, token)
  335.        if (state == ps_right)
  336.          ret_val = push_alignment (align, id);
  337.        else
  338. -        warning ("malformed '#pragma pack(push[,id],<n>)'");
  339. +        {
  340. +          ret_val = 1; /* Ignore the pragma.  */
  341. +          warning ("malformed '#pragma pack(push[,id][,n])'");
  342. +        }
  343.        break;
  344.        
  345.      case ps_pop:
  346.        if (state == ps_right)
  347. -        ret_val = pop_alignment (id);
  348. +        ret_val = pop_alignment (align, id);
  349.        else
  350. -        warning ("malformed '#pragma pack(pop[,id])'");
  351. +        {
  352. +          ret_val = 1; /* Ignore the pragma.  */
  353. +          warning ("malformed '#pragma pack(pop[,id][,n])'");
  354. +        }
  355.        break;
  356.  #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
  357.        
  358. @@ -305,6 +286,7 @@ handle_pragma_token (string, token)
  359.  
  360.        type = state = ps_start;
  361.        id = NULL_TREE;
  362. +      align = -1;
  363.        
  364.        return ret_val;
  365.      }
  366. @@ -418,6 +400,8 @@ handle_pragma_token (string, token)
  367.        break;
  368.  
  369.      handle_align:
  370. +      /* User specified alignment in bytes, but is converted to bits 
  371. +         before use.  */
  372.        align = TREE_INT_CST_LOW (token);
  373.        switch (align)
  374.      {
  375. @@ -426,10 +410,20 @@ handle_pragma_token (string, token)
  376.      case 4:
  377.      case 8:
  378.      case 16:
  379. +      align *= 8;
  380.        state = ps_align;
  381.        break;
  382.  
  383.      default:
  384. +#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
  385. +          warning 
  386. +        ("Alignment must be a small power of two, not %d, in #pragma %s",
  387. +         align, (type == ps_pop) ? "pop" : "pack");
  388. +#else
  389. +          warning 
  390. +        ("Alignment must be a small power of two, not %d, in #pragma pack",
  391. +         align);
  392. +#endif
  393.        state = ps_bad;
  394.        break;
  395.      }
  396. @@ -446,45 +440,48 @@ handle_pragma_token (string, token)
  397.  
  398.  #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
  399.      case ps_push:
  400. -      state = (strcmp (string, ",") ? ps_bad : ps_pushcomma);
  401. -      break;
  402. +      /* If no alignment is specified, push the current alignment.  */
  403. +      align = maximum_field_alignment;
  404.  
  405. -    case ps_pushid:
  406. -      state = (strcmp (string, ",") ? ps_bad : ps_pushcomma2);
  407. +      /* fall through. */
  408. +    case ps_pop:
  409. +      /* If no alignment is specified, we'll just use the stored one.  */
  410. +      if (strcmp (string, ",") == 0)
  411. +        state = ps_comma;
  412. +      else if (strcmp (string, ")") == 0)
  413. +        {
  414. +      state = ps_right;
  415. +    }
  416. +      else
  417. +    state = ps_bad;
  418.        break;
  419.  
  420. -    case ps_pushcomma:
  421. +    case ps_comma:
  422. +      /* Id or <n> or both specified. */
  423.        if (token && TREE_CODE (token) == IDENTIFIER_NODE)
  424.      {
  425.        id = token;
  426. -      state = ps_pushid;
  427. +      state = ps_id;
  428.        break;
  429.      }
  430.  
  431. -      /* else fall through */
  432. -    case ps_pushcomma2:
  433. +      /* else no id, but <n> specified, so fall through */
  434. +    case ps_comma2:
  435.        if (token && TREE_CODE (token) == INTEGER_CST)
  436.      goto handle_align;
  437.        else
  438.      state = ps_bad;
  439.        break;
  440.  
  441. -    case ps_pop:
  442. +    case ps_id:
  443.        if (strcmp (string, ",") == 0)
  444. -    state = ps_popcomma;
  445. +        state = ps_comma2;
  446. +      else if (strcmp (string, ")") == 0)
  447. +    state = ps_right;
  448.        else
  449. -    state = (strcmp (string, ")") ? ps_bad : ps_right);
  450. -      break;
  451. -
  452. -    case ps_popcomma:
  453. -      if (token && TREE_CODE (token) == IDENTIFIER_NODE)
  454. -    {
  455. -      id = token;
  456. -      state = ps_align;
  457. -    }
  458. -      else
  459.      state = ps_bad;
  460.        break;
  461. +
  462.  #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
  463.        
  464.      case ps_bad:
  465. Index: gcc-2.95.2/gcc/c-pragma.h
  466. ===================================================================
  467. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/c-pragma.h,v
  468. retrieving revision 1.1.1.1
  469. diff -u -3 -p -r1.1.1.1 c-pragma.h
  470. --- gcc-2.95.2/gcc/c-pragma.h    1999/11/05 01:09:41    1.1.1.1
  471. +++ gcc-2.95.2/gcc/c-pragma.h    1999/11/05 06:37:53
  472. @@ -39,9 +39,6 @@ Boston, MA 02111-1307, USA.  */
  473.  /* If we are supporting #pragma pack(push... then we automatically
  474.     support #pragma pack(<n>)  */
  475.  #define HANDLE_PRAGMA_PACK 1
  476. -#define PRAGMA_INSERT_ATTRIBUTES(node, pattr, prefix_attr) \
  477. -  insert_pack_attributes (node, pattr, prefix_attr)
  478. -extern void insert_pack_attributes PROTO((tree, tree *, tree *));
  479.  #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
  480.  
  481.  
  482. @@ -88,8 +85,11 @@ enum pragma_state
  483.    ps_right,
  484.  #endif
  485.  #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
  486. -  ps_push, ps_pushcomma, ps_pushid, ps_pushcomma2,
  487. -  ps_pop, ps_popcomma,
  488. +  ps_push, 
  489. +  ps_pop, 
  490. +  ps_comma, 
  491. +  ps_comma2,
  492. +  ps_id, 
  493.  #endif
  494.    ps_bad
  495.  };
  496.