summaryrefslogtreecommitdiff
path: root/girepository/giroffsets.c
blob: 66d18555cdc2861f2b9298cee30b662cb6775e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* GObject introspection: Compute structure offsets
 *
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "girffi.h"
#include "girnode.h"

/* The C standard specifies that an enumeration can be any char or any signed
 * or unsigned integer type capable of resresenting all the values of the
 * enumeration. We use test enumerations to figure out what choices the
 * compiler makes.
 */

typedef enum {
  ENUM_1 = 1 /* compiler could use int8, uint8, int16, uint16, int32, uint32 */
} Enum1;

typedef enum {
  ENUM_2 = 128 /* compiler could use uint8, int16, uint16, int32, uint32 */
} Enum2;

typedef enum {
  ENUM_3 = 257 /* compiler could use int16, uint16, int32, uint32 */
} Enum3;

typedef enum {
  ENUM_4 = G_MAXSHORT + 1 /* compiler could use uint16, int32, uint32 */
} Enum4;

typedef enum {
  ENUM_5 = G_MAXUSHORT + 1 /* compiler could use int32, uint32 */
} Enum5;

typedef enum {
  ENUM_6 = ((guint)G_MAXINT) + 1 /* compiler could use uint32 */
} Enum6;

/* GIrNodeValue has guint32 values, so if it matters to the ABI whether
 * constant values are signed, we are in trouble. And we don't handle
 * enums with > 32 bit values. */

#if 0
typedef enum {
  ENUM_7 = -1 /* compiler could use int8, int16, int32 */
} Enum7;

/* etc... */
#endif

static gboolean
get_enum_size_alignment (GIrNodeEnum *enum_node,
			 gint        *size,
			 gint        *alignment)
{
  GList *l;
  guint32 max_value = 0;
  int width;
  ffi_type *type_ffi;

  for (l = enum_node->values; l; l = l->next)
    {
      GIrNodeValue *value = l->data;
      if (value->value > max_value)
	max_value = value->value;
    }

  if (max_value < 128)
    width = sizeof (Enum1);
  else if (max_value < 256)
    width = sizeof (Enum2);
  else if (max_value < G_MAXSHORT)
    width = sizeof (Enum3);
  else if (max_value < G_MAXUSHORT)
    width = sizeof (Enum4);
  else if (max_value < G_MAXINT)
    width = sizeof (Enum5);
  else
    width = sizeof (Enum6);

  if (width == 1)
    type_ffi = &ffi_type_sint8;
  else if (width == 2)
    type_ffi = &ffi_type_sint16;
  else if (width == 4)
    type_ffi = &ffi_type_sint32;
  else if (width == 8)
    type_ffi = &ffi_type_sint64;
  else
    g_error ("Unexpected enum width %d", width);

  *size = type_ffi->size;
  *alignment = type_ffi->alignment;

  return TRUE;
}

static gboolean
get_interface_size_alignment (GIrNodeField *field,
			      GIrNode      *parent_node,
			      GIrModule    *module,
			      GList        *modules,
			      gint         *size,
			      gint         *alignment)
{
  GIrNodeType *type = field->type;
  GIrNode *iface;
  GIrModule *iface_module;

  if (!g_ir_find_node (module, modules, type->interface, &iface, &iface_module))
    {
      g_warning ("Can't resolve type '%s' for field %s.%s.%s",
		 type->interface, module->name, parent_node->name, ((GIrNode *)field)->name);
      *size = -1;
      *alignment = -1;
      return FALSE;
    }

  g_ir_node_compute_offsets (iface, iface_module,
			     iface_module == module ? modules : NULL);

  switch (iface->type)
    {
    case G_IR_NODE_BOXED:
      {
	GIrNodeBoxed *boxed = (GIrNodeBoxed *)iface;
	*size = boxed->size;
	*alignment = boxed->alignment;
	break;
      }
    case G_IR_NODE_STRUCT:
      {
	GIrNodeStruct *struct_ = (GIrNodeStruct *)iface;
	*size = struct_->size;
	*alignment = struct_->alignment;
	break;
      }
    case G_IR_NODE_UNION:
      {
	GIrNodeUnion *union_ = (GIrNodeUnion *)iface;
	*size = union_->size;
	*alignment = union_->alignment;
	break;
      }
    case G_IR_NODE_ENUM:
    case G_IR_NODE_FLAGS:
      {
	return get_enum_size_alignment ((GIrNodeEnum *)iface,
					size, alignment);
      }
    case G_IR_NODE_CALLBACK:
      {
	*size = ffi_type_pointer.size;
	*alignment = ffi_type_pointer.alignment;
	break;
      }
    default:
      {
	g_warning ("Field %s.%s.%s has is not a pointer and is of type %s",
		   module->name, parent_node->name, ((GIrNode *)field)->name,
		   g_ir_node_type_to_string (iface->type));
	*size = -1;
	*alignment = -1;
	break;
      }
    }

  return *alignment > 0;
}

static gboolean
get_field_size_alignment (GIrNodeField *field,
			  GIrNode      *parent_node,
			  GIrModule    *module,
			  GList        *modules,
			  gint         *size,
			  gint         *alignment)
{
  GIrNodeType *type = field->type;
  ffi_type *type_ffi;

  if (type->is_pointer)
    {
      type_ffi = &ffi_type_pointer;
    }
  else
    {
      if (type->tag == GI_TYPE_TAG_INTERFACE)
	{
	  return get_interface_size_alignment (field, parent_node,
					       module, modules,
					       size, alignment);
	}
      else
	{
	  type_ffi = g_ir_ffi_get_ffi_type (type->tag);

	  if (type_ffi == &ffi_type_void)
	    {
	      g_warning ("Field %s.%s.%s has void type",
			 module->name, parent_node->name, ((GIrNode *)field)->name);
	      *size = -1;
	      *alignment = -1;
	      return FALSE;
	    }
	  else if (type_ffi == &ffi_type_pointer)
	    {
	      g_warning ("Field %s.%s.%s has is not a pointer and is of type %s",
			 module->name, parent_node->name, ((GIrNode *)field)->name,
			 g_type_tag_to_string (type->tag));
	      *size = -1;
	      *alignment = -1;
	      return FALSE;
	    }
	}
    }

  g_assert (type_ffi);
  *size = type_ffi->size;
  *alignment = type_ffi->alignment;

  return TRUE;
}

#define ALIGN(n, align) (((n) + (align) - 1) & ~((align) - 1))

static gboolean
compute_struct_field_offsets (GIrNode     *node,
			      GList       *members,
			      GIrModule   *module,
			      GList       *modules,
			      gint        *size_out,
			      gint        *alignment_out)
{
  int size = 0;
  int alignment = 1;
  GList *l;
  gboolean have_error = FALSE;

  *alignment_out = -2; /* mark to detect recursion */

  for (l = members; l; l = l->next)
    {
      GIrNode *member = (GIrNode *)l->data;

      if (member->type == G_IR_NODE_FIELD)
	{
	  GIrNodeField *field = (GIrNodeField *)member;

	  if (!have_error)
	    {
	      int member_size;
	      int member_alignment;

	      if (get_field_size_alignment (field, node,
					    module, modules,
					    &member_size, &member_alignment))
		{
		  size = ALIGN (size, member_alignment);
		  alignment = MAX (alignment, member_alignment);
		  field->offset = size;
		  size += member_size;
		}
	      else
		have_error = TRUE;
	    }

	  if (have_error)
	    field->offset = -1;
	}
      else if (member->type == G_IR_NODE_CALLBACK)
	{
	  size = ffi_type_pointer.size;
	  alignment = ffi_type_pointer.alignment;
	}
    }

  /* Structs are tail-padded out to a multiple of their alignment */
  size = ALIGN (size, alignment);

  if (!have_error)
    {
      *size_out = size;
      *alignment_out = alignment;
    }
  else
    {
      *size_out = -1;
      *alignment_out = -1;
    }

  return !have_error;
}

static gboolean
compute_union_field_offsets (GIrNode     *node,
			     GList       *members,
			     GIrModule   *module,
			     GList       *modules,
			     gint        *size_out,
			     gint        *alignment_out)
{
  int size = 0;
  int alignment = 1;
  GList *l;
  gboolean have_error = FALSE;

  *alignment_out = -2; /* mark to detect recursion */

  for (l = members; l; l = l->next)
    {
      GIrNode *member = (GIrNode *)l->data;

      if (member->type == G_IR_NODE_FIELD)
	{
	  GIrNodeField *field = (GIrNodeField *)member;

	  if (!have_error)
	    {
	      int member_size;
	      int member_alignment;

	      if (get_field_size_alignment (field, node,
					    module, modules,
					    &member_size, &member_alignment))
		{
		  size = MAX (size, member_size);
		  alignment = MAX (alignment, member_alignment);
		}
	      else
		have_error = TRUE;
	    }
	}
    }

  /* Unions are tail-padded out to a multiple of their alignment */
  size = ALIGN (size, alignment);

  if (!have_error)
    {
      *size_out = size;
      *alignment_out = alignment;
    }
  else
    {
      *size_out = -1;
      *alignment_out = -1;
    }

  return !have_error;
}

static gboolean
check_needs_computation (GIrNode   *node,
			 GIrModule *module,
			 gint       alignment)
{
  /*
   *  0: Not yet computed
   * >0: Previously succeeded
   * -1: Previously failed
   * -2: In progress
   */
  if (alignment == -2)
    {
      g_warning ("Recursion encountered when computing the size of %s.%s",
		 module->name, node->name);
    }

  return alignment == 0;
}

/**
 * g_ir_node_compute_offsets:
 * @node: a #GIrNode
 * @module: Current module being processed
 * @moudles: all currently loaded modules
 *
 * If a node is a a structure or union, makes sure that the field
 * offsets have been computed, and also computes the overall size and
 * alignment for the type.
 */
void
g_ir_node_compute_offsets (GIrNode   *node,
			   GIrModule *module,
			   GList     *modules)
{
  switch (node->type)
    {
    case G_IR_NODE_BOXED:
      {
	GIrNodeBoxed *boxed = (GIrNodeBoxed *)node;

	if (!check_needs_computation (node, module, boxed->alignment))
	  return;

	compute_struct_field_offsets (node, boxed->members,
				      module, modules,
				      &boxed->size, &boxed->alignment);
	break;
      }
    case G_IR_NODE_STRUCT:
      {
	GIrNodeStruct *struct_ = (GIrNodeStruct *)node;

	if (!check_needs_computation (node, module, struct_->alignment))
	  return;

	compute_struct_field_offsets (node, struct_->members,
				      module, modules,
				      &struct_->size, &struct_->alignment);
	break;
      }
    case G_IR_NODE_UNION:
      {
	GIrNodeUnion *union_ = (GIrNodeUnion *)node;

	if (!check_needs_computation (node, module, union_->alignment))
	  return;

	compute_union_field_offsets (node, union_->members,
				     module, modules,
				     &union_->size, &union_->alignment);
	break;
      }
    default:
      /* Nothing to do */
      return;
    }
}