FreeBASIC  0.91.0
ast-node-ptr.bas
Go to the documentation of this file.
1 '' AST deref pointer nodes
2 '' l = pointer expression; r = NULL
3 ''
4 '' chng: sep/2004 written [v1ctor]
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "ir.bi"
9 #include once "ast.bi"
10 
11 function astNewDEREF _
12  ( _
13  byval l as ASTNODE ptr, _
14  byval dtype as integer, _
15  byval subtype as FBSYMBOL ptr, _
16  byval ofs as longint _
17  ) as ASTNODE ptr
18 
19  dim as ASTNODE ptr n = any
20 
21  if( l <> NULL ) then
22  if( dtype = FB_DATATYPE_INVALID ) then
23  dtype = typeDeref( astGetFullType( l ) )
24  subtype = astGetSubType( l )
25  end if
26 
27  if( ofs = 0 ) then
28  '' skip any casting if they won't do any conversion
29  dim as ASTNODE ptr t = l
30  if( l->class = AST_NODECLASS_CONV ) then
31  if( l->cast.doconv = FALSE ) then
32  t = l->l
33  end if
34  end if
35 
36  '' convert *@ to nothing
37  dim as integer delchild = any
38  select case t->class
39  case AST_NODECLASS_ADDROF
40  delchild = TRUE
41 
42  case AST_NODECLASS_OFFSET
43  delchild = (t->ofs.ofs = 0)
44 
45  case else
46  delchild = FALSE
47  end select
48 
49  ''
50  if( delchild ) then
51  n = t->l
52  astDelNode( t )
53  if( t <> l ) then
54  astDelNode( l )
55  end if
56 
57  astSetType( n, dtype, subtype )
58  return n
59  end if
60  end if
61 
62  if( astIsCONST( l ) ) then
63  ofs += astConstFlushToInt( l )
64  l = NULL
65  end if
66  end if
67 
68  '' alloc new node
69  n = astNewNode( AST_NODECLASS_DEREF, dtype, subtype )
70 
71  n->l = l '' Can be NULL e.g. if it was a constant
72  n->ptr.ofs = ofs
73 
74  function = n
75 end function
76 
77 function astLoadDEREF( byval n as ASTNODE ptr ) as IRVREG ptr
78  dim as ASTNODE ptr l = any
79  dim as IRVREG ptr v1 = any, vp = any, vr = any
80 
81  l = n->l
82  '' no index? can happen with absolute addresses + ptr typecasting
83  if( l = NULL ) then
84  if( ast.doemit ) then
85  vr = irAllocVRPTR( astGetDataType( n ), n->subtype, n->ptr.ofs, NULL )
86  vr->vector = n->vector
87  end if
88  return vr
89  end if
90 
91  v1 = astLoad( l )
92 
93  ''
94  if( ast.doemit ) then
95  '' src is not a reg?
96  if( (irIsREG( v1 ) = FALSE) or _
97  (typeGetClass(v1->dtype) <> FB_DATACLASS_INTEGER) or _
98  (typeGetSize(v1->dtype) <> env.pointersize) ) then
99 
100  vp = irAllocVREG( typeAddrOf( astGetDataType( n ) ), n->subtype )
101  irEmitADDR( AST_OP_DEREF, v1, vp )
102  else
103  vp = v1
104  end if
105 
106  vr = irAllocVRPTR( astGetDataType( n ), n->subtype, n->ptr.ofs, vp )
107  vr->vector = n->vector
108  end if
109 
110  astDelNode( l )
111 
112  function = vr
113 end function
114