Skip to content

Commit 517877f

Browse files
authored
Merge pull request #165 from jayrm/array-descriptor
fbc-int: add array descriptor API - new header in ./inc/fbc-int/array.bi - tests in ./tests/fbc-int/array.bas - new run time function fb_ArrayGetDesc() to return FBARRAY ptr
2 parents 16a5763 + d7ec6f6 commit 517877f

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Version 1.07.0
3535
- '-edebug' command line option to enable __FB_DEBUG__
3636
- '-edebuginfo' command line option to enable debug symbols
3737
- '-elocation' command line option to enable reporting error location
38+
- ./inc/fbc-int/array.bi - fbc internal API for array descriptor
3839

3940
[fixed]
4041
- sf.net #881: C backend: support for varadic function parameters in gcc using __builtin_va_list type and related macros

inc/fbc-int/array.bi

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef __FBC_INT_ARRAY_BI__
2+
#define __FBC_INT_ARRAY_BI__
3+
4+
# if __FB_LANG__ = "fb"
5+
namespace FBC
6+
# endif
7+
8+
'' declarations must follow ./src/rtlib/fb_array.h
9+
10+
const FB_MAXDIMENSIONS as integer = 8
11+
12+
type FBARRAYDIM
13+
dim as uinteger elements '' number of elements
14+
dim as integer lbound '' dimension lower bound
15+
dim as integer ubound '' dimension upper bound
16+
end type
17+
18+
type FBARRAY
19+
dim as any ptr index_ptr '' @array(0, 0, 0, ... )
20+
dim as any ptr base_ptr '' start of memory at array lowest bounds
21+
dim as uinteger size '' byte size of allocated contents
22+
dim as uinteger element_len '' byte size of single element
23+
dim as uinteger dimensions '' number of dimensions
24+
25+
'' take care with number of dimensions; fbc may allocate
26+
'' a smaller descriptor with fewer than FB_MAXDIMENSIONS
27+
'' in dimTb() if it is known at compile time that they
28+
'' are never needed. Always respsect number of
29+
'' dimensions when accessing dimTb()
30+
31+
dim as FBARRAYDIM dimTb(0 to FB_MAXDIMENSIONS-1)
32+
end type
33+
34+
# if __FB_LANG__ = "fb"
35+
end namespace
36+
# endif
37+
38+
declare function fb_ArrayGetDesc alias "fb_ArrayGetDesc" _
39+
( array() as any ) as FBC.FBARRAY ptr
40+
41+
#endif

src/rtlib/array_getdesc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* fbc-int API: array descriptor internals */
2+
3+
#include "fb.h"
4+
5+
FBCALL FBARRAY *fb_ArrayGetDesc( FBARRAY *array )
6+
{
7+
return array;
8+
}

src/rtlib/fb_array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ FBCALL int fb_ArrayClear ( FBARRAY *array, int isvarlen );
5151
FBCALL int fb_ArrayClearObj ( FBARRAY *array, FB_DEFCTOR ctor, FB_DEFCTOR dtor, int dofill );
5252
FBCALL int fb_ArrayErase ( FBARRAY *array, int isvarlen );
5353
FBCALL int fb_ArrayEraseObj ( FBARRAY *array, FB_DEFCTOR dtor );
54+
FBCALL FBARRAY *fb_ArrayGetDesc ( FBARRAY *array );
5455
FBCALL void fb_ArrayStrErase ( FBARRAY *array );
5556
int fb_ArrayRedim ( FBARRAY *array, size_t element_len, int preserve, size_t dimensions, ... );
5657
int fb_ArrayRedimEx ( FBARRAY *array, size_t element_len, int doclear, int isvarlen, size_t dimensions, ... );

tests/dirlist.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ data \
1717
datetime \
1818
dim \
1919
expressions \
20+
fbc-int \
2021
file \
2122
functions \
2223
gfx \

tests/fbc-int/array.bas

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "fbcunit.bi"
2+
#include "fbc-int/array.bi"
3+
4+
using FBC
5+
6+
'' tests for array descriptor internals
7+
8+
SUITE( fbc_tests.fbc_int.array )
9+
10+
#macro check_array( ap, b, s, e, d )
11+
if( ap ) then
12+
CU_ASSERT( ap->base_ptr = b )
13+
CU_ASSERT( ap->size = s )
14+
CU_ASSERT( ap->element_len = e )
15+
CU_ASSERT( ap->dimensions = d )
16+
else
17+
CU_FAIL()
18+
end if
19+
#endmacro
20+
21+
#macro check_dim( ap, d, e, l, u )
22+
if( ap ) then
23+
CU_ASSERT( ap->dimTb(d).elements = e )
24+
CU_ASSERT( ap->dimTb(d).lbound = l )
25+
CU_ASSERT( ap->dimTb(d).ubound = u )
26+
else
27+
CU_FAIL()
28+
end if
29+
#endmacro
30+
31+
TEST( static_fixed )
32+
'' static array will have descriptor because it is passed
33+
'' a procedure, in this case, fb_ArrayGetDesc()
34+
35+
static a1(2 to 11) as integer
36+
dim ap1 as FBARRAY ptr = fb_ArrayGetDesc( a1() )
37+
check_array( ap1, @a1(2), sizeof(integer) * 10, sizeof(integer), 1 )
38+
check_dim( ap1, 0, 10, 2, 11 )
39+
40+
static a2(2 to 6, 3 to 12) as integer
41+
dim ap2 as FBARRAY ptr = fb_ArrayGetDesc( a2() )
42+
check_array( ap2, @a2(2,3), sizeof(integer) * 5 * 10, sizeof(integer), 2 )
43+
check_dim( ap2, 0, 5, 2, 6 )
44+
check_dim( ap2, 1, 10, 3, 12 )
45+
END_TEST
46+
47+
TEST( static_empty )
48+
'' static array will have descriptor because it is dynamic
49+
50+
static a0() as integer
51+
dim ap0 as FBARRAY ptr = fb_ArrayGetDesc( a0() )
52+
check_array( ap0, 0, 0, sizeof(integer), 0 )
53+
54+
static a1(any) as integer
55+
dim ap1 as FBARRAY ptr = fb_ArrayGetDesc( a1() )
56+
check_array( ap1, 0, 0, sizeof(integer), 1 )
57+
check_dim( ap1, 0, 0, 0, 0 )
58+
59+
static a2(any,any) as integer
60+
dim ap2 as FBARRAY ptr = fb_ArrayGetDesc( a2() )
61+
check_array( ap2, 0, 0, sizeof(integer), 2 )
62+
check_dim( ap2, 0, 0, 0, 0 )
63+
check_dim( ap2, 1, 0, 0, 0 )
64+
END_TEST
65+
66+
TEST( local_fixed )
67+
dim a1(2 to 11) as integer
68+
dim ap1 as FBARRAY ptr = fb_ArrayGetDesc( a1() )
69+
check_array( ap1, @a1(2), sizeof(integer) * 10, sizeof(integer), 1 )
70+
check_dim( ap1, 0, 10, 2, 11 )
71+
72+
dim a2(2 to 6, 3 to 12) as integer
73+
dim ap2 as FBARRAY ptr = fb_ArrayGetDesc( a2() )
74+
check_array( ap2, @a2(2,3), sizeof(integer) * 5 * 10, sizeof(integer), 2 )
75+
check_dim( ap2, 0, 5, 2, 6 )
76+
check_dim( ap2, 1, 10, 3, 12 )
77+
END_TEST
78+
79+
TEST( local_empty )
80+
dim a0() as integer
81+
dim ap0 as FBARRAY ptr = fb_ArrayGetDesc( a0() )
82+
check_array( ap0, 0, 0, sizeof(integer), 0 )
83+
84+
dim a1(any) as integer
85+
dim ap1 as FBARRAY ptr = fb_ArrayGetDesc( a1() )
86+
check_array( ap1, 0, 0, sizeof(integer), 1 )
87+
check_dim( ap1, 0, 0, 0, 0 )
88+
89+
dim a2(any,any) as integer
90+
dim ap2 as FBARRAY ptr = fb_ArrayGetDesc( a2() )
91+
check_array( ap2, 0, 0, sizeof(integer), 2 )
92+
check_dim( ap2, 0, 0, 0, 0 )
93+
check_dim( ap2, 1, 0, 0, 0 )
94+
END_TEST
95+
96+
END_SUITE

0 commit comments

Comments
 (0)