FreeBASIC  0.91.0
rtl-data.bas
Go to the documentation of this file.
1 '' intrinsic runtime lib data functions (DATA, RESTORE, READ)
2 ''
3 '' chng: oct/2004 written [v1ctor]
4 
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "ast.bi"
9 #include once "rtl.bi"
10 
11 '' globals
12  dim shared as FB_RTL_PROCDEF funcdata( 0 to ... ) = _
13  { _
14 /' sub fb_DataRestore( byval labeladdr as FB_DATADESC ptr ) '/ _
15  ( _
16  @FB_RTL_DATARESTORE, NULL, _
17  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
18  NULL, FB_RTL_OPT_NONE, _
19  1, _
20  { _
21  ( typeAddrOf( FB_DATATYPE_VOID ), FB_PARAMMODE_BYVAL, FALSE ) _
22  } _
23  ), _
24 /' sub fb_DataReadStr _
25  ( _
26  byref dst as any, _
27  byval dst_size as integer, _
28  byval fillrem as long = 1 _
29  ) '/ _
30  ( _
31  @FB_RTL_DATAREADSTR, NULL, _
32  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
33  NULL, FB_RTL_OPT_NONE, _
34  3, _
35  { _
36  ( FB_DATATYPE_VOID, FB_PARAMMODE_BYREF, FALSE ), _
37  ( FB_DATATYPE_INTEGER, FB_PARAMMODE_BYVAL, FALSE ), _
38  ( FB_DATATYPE_LONG, FB_PARAMMODE_BYVAL, TRUE, 1 ) _
39  } _
40  ), _
41 /' sub fb_DataReadWstr( byval dst as wstring ptr, byval dst_size as integer ) '/ _
42  ( _
43  @FB_RTL_DATAREADWSTR, NULL, _
44  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
45  NULL, FB_RTL_OPT_NONE, _
46  2, _
47  { _
48  ( typeAddrOf( FB_DATATYPE_WCHAR ),FB_PARAMMODE_BYVAL, FALSE ), _
49  ( FB_DATATYPE_INTEGER,FB_PARAMMODE_BYVAL, FALSE ) _
50  } _
51  ), _
52 /' sub fb_DataReadByte( byref dst as byte ) '/ _
53  ( _
54  @FB_RTL_DATAREADBYTE, NULL, _
55  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
56  NULL, FB_RTL_OPT_NONE, _
57  1, _
58  { _
59  ( FB_DATATYPE_BYTE, FB_PARAMMODE_BYREF, FALSE ) _
60  } _
61  ), _
62 /' sub fb_DataReadShort( byref dst as short ) '/ _
63  ( _
64  @FB_RTL_DATAREADSHORT, NULL, _
65  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
66  NULL, FB_RTL_OPT_NONE, _
67  1, _
68  { _
69  ( FB_DATATYPE_SHORT, FB_PARAMMODE_BYREF, FALSE ) _
70  } _
71  ), _
72 /' sub fb_DataReadInt( byref dst as long ) '/ _
73  ( _
74  @FB_RTL_DATAREADINT, NULL, _
75  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
76  NULL, FB_RTL_OPT_NONE, _
77  1, _
78  { _
79  ( FB_DATATYPE_LONG, FB_PARAMMODE_BYREF, FALSE ) _
80  } _
81  ), _
82 /' sub fb_DataReadLongint( byref dst as longint ) '/ _
83  ( _
84  @FB_RTL_DATAREADLONGINT, NULL, _
85  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
86  NULL, FB_RTL_OPT_NONE, _
87  1, _
88  { _
89  ( FB_DATATYPE_LONGINT, FB_PARAMMODE_BYREF, FALSE ) _
90  } _
91  ), _
92 /' sub fb_DataReadUByte( byref dst as ubyte ) '/ _
93  ( _
94  @FB_RTL_DATAREADUBYTE, NULL, _
95  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
96  NULL, FB_RTL_OPT_NONE, _
97  1, _
98  { _
99  ( FB_DATATYPE_UBYTE, FB_PARAMMODE_BYREF, FALSE ) _
100  } _
101  ), _
102 /' sub fb_DataReadUShort( byref dst as ushort ) '/ _
103  ( _
104  @FB_RTL_DATAREADUSHORT, NULL, _
105  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
106  NULL, FB_RTL_OPT_NONE, _
107  1, _
108  { _
109  ( FB_DATATYPE_USHORT, FB_PARAMMODE_BYREF, FALSE ) _
110  } _
111  ), _
112 /' sub fb_DataReadUInt( byref dst as ulong ) '/ _
113  ( _
114  @FB_RTL_DATAREADUINT, NULL, _
115  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
116  NULL, FB_RTL_OPT_NONE, _
117  1, _
118  { _
119  ( FB_DATATYPE_ULONG, FB_PARAMMODE_BYREF, FALSE ) _
120  } _
121  ), _
122 /' sub fb_DataReadULongint( byref dst as ulongint ) '/ _
123  ( _
124  @FB_RTL_DATAREADULONGINT, NULL, _
125  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
126  NULL, FB_RTL_OPT_NONE, _
127  1, _
128  { _
129  ( FB_DATATYPE_ULONGINT, FB_PARAMMODE_BYREF, FALSE ) _
130  } _
131  ), _
132 /' sub fb_DataReadSingle( byref dst as single ) '/ _
133  ( _
134  @FB_RTL_DATAREADSINGLE, NULL, _
135  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
136  NULL, FB_RTL_OPT_NONE, _
137  1, _
138  { _
139  ( FB_DATATYPE_SINGLE, FB_PARAMMODE_BYREF, FALSE ) _
140  } _
141  ), _
142 /' sub fb_DataReadDouble( byref dst as single ) '/ _
143  ( _
144  @FB_RTL_DATAREADDOUBLE, NULL, _
145  FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
146  NULL, FB_RTL_OPT_NONE, _
147  1, _
148  { _
149  ( FB_DATATYPE_DOUBLE, FB_PARAMMODE_BYREF, FALSE ) _
150  } _
151  ), _
152 /' EOL '/ _
153  ( _
154  NULL _
155  ) _
156  }
157 
158 '':::::
160 
162 
163 end sub
164 
165 '':::::
167 
168  '' procs will be deleted when symbEnd is called
169 
170 end sub
171 
172 '':::::
173 function rtlDataRead _
174  ( _
175  byval varexpr as ASTNODE ptr _
176  ) as integer
177 
178  dim as ASTNODE ptr proc = any
179  dim as FBSYMBOL ptr f = any
180  dim as integer args = any, dtype = any
181  dim as longint lgt = any
182 
183  function = FALSE
184 
185  f = NULL
186  args = 1
187  dtype = astGetDataType( varexpr )
188 
189  select case as const typeGet( dtype )
190  case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR, FB_DATATYPE_CHAR
191  f = PROCLOOKUP( DATAREADSTR )
192  args = 3
193 
194  case FB_DATATYPE_WCHAR
195  f = PROCLOOKUP( DATAREADWSTR )
196  args = 2
197 
198  case FB_DATATYPE_BYTE, FB_DATATYPE_UBYTE, _
199  FB_DATATYPE_SHORT, FB_DATATYPE_USHORT, _
200  FB_DATATYPE_INTEGER, FB_DATATYPE_ENUM, FB_DATATYPE_UINT, _
201  FB_DATATYPE_LONG, FB_DATATYPE_ULONG, FB_DATATYPE_POINTER, _
202  FB_DATATYPE_LONGINT, FB_DATATYPE_ULONGINT
203 
204  select case as const( typeGetSizeType( dtype ) )
205  case FB_SIZETYPE_INT8 : f = PROCLOOKUP( DATAREADBYTE )
206  case FB_SIZETYPE_UINT8 : f = PROCLOOKUP( DATAREADUBYTE )
207  case FB_SIZETYPE_INT16 : f = PROCLOOKUP( DATAREADSHORT )
208  case FB_SIZETYPE_UINT16 : f = PROCLOOKUP( DATAREADUSHORT )
209  case FB_SIZETYPE_INT32 : f = PROCLOOKUP( DATAREADINT )
210  case FB_SIZETYPE_UINT32 : f = PROCLOOKUP( DATAREADUINT )
211  case FB_SIZETYPE_INT64 : f = PROCLOOKUP( DATAREADLONGINT )
212  case FB_SIZETYPE_UINT64 : f = PROCLOOKUP( DATAREADULONGINT )
213  end select
214 
215  case FB_DATATYPE_SINGLE
216  f = PROCLOOKUP( DATAREADSINGLE )
217 
218  case FB_DATATYPE_DOUBLE
219  f = PROCLOOKUP( DATAREADDOUBLE )
220 
221  case FB_DATATYPE_STRUCT
222  exit function '' illegal
223 
224  case else
225  exit function
226  end select
227 
228  if( f = NULL ) then
229  exit function
230  end if
231 
232  proc = astNewCALL( f )
233 
234  if( args > 1 ) then
235  '' always calc len before pushing the param
236  lgt = rtlCalcStrLen( varexpr, dtype )
237  else
238  lgt = 0
239  end if
240 
241  '' byref var as any
242  if( astNewARG( proc, varexpr ) = NULL ) then
243  exit function
244  end if
245 
246  if( args > 1 ) then
247  '' byval dst_size as integer
248  if( astNewARG( proc, astNewCONSTi( lgt ) ) = NULL ) then
249  exit function
250  end if
251 
252  if( args > 2 ) then
253  '' byval fillrem as integer
254  if( astNewARG( proc, astNewCONSTi( dtype = FB_DATATYPE_FIXSTR ) ) = NULL ) then
255  exit function
256  end if
257  end if
258  end if
259 
260  ''
261  astAdd( proc )
262 
263  function = TRUE
264 
265 end function
266 
267 function rtlDataRestore _
268  ( _
269  byval label as FBSYMBOL ptr, _
270  byval afternode as ASTNODE ptr _
271  ) as integer
272 
273  dim as ASTNODE ptr proc = any, expr = any
274  dim as FBSYMBOL ptr sym = any
275 
276  function = FALSE
277 
278  proc = astNewCALL( PROCLOOKUP( DATARESTORE ), NULL )
279 
280  '' byval labeladdrs as void ptr
281  if( label = NULL ) then
282  '' blank RESTORE (no label), so use label of first DATA
283  sym = astGetFirstDataStmtSymbol( )
284 
285  '' blank RESTORE used before any DATA was found? damn..
286  if( sym = NULL ) then
287  '' create an empty stmt, it should just contain a link to the next DATA
288  expr = astDataStmtBegin( )
289  astDataStmtEnd( expr )
290  astDelNode( expr )
291 
292  sym = astGetFirstDataStmtSymbol( )
293  end if
294  else
295  sym = astDataStmtAdd( label, 0 )
296  end if
297 
298  if( astNewARG( proc, astNewADDROF( astNewVAR( sym ) ) ) = NULL ) then
299  exit function
300  end if
301 
302  if( afternode = NULL ) then
303  astAdd( proc )
304  else
305  astAddAfter( proc, afternode )
306  end if
307 
308  function = TRUE
309 end function
310