How to define array members in typedef?

31 views
Skip to first unread message

Kiwamu Okabe

unread,
Oct 12, 2020, 2:17:47 AM10/12/20
to ats-lang-users
Dear all,

I wrote following code:

```ats
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

#define MAXPATHLEN 1024

typedef kld_file_stat = @{ version = int, name = @[char][MAXPATHLEN]}

fun kern_kldstat {l:addr} (pf: !kld_file_stat @ l | p: ptr l): int = 0 where {
val () = !p.name.[0] := 'A'
}

implement main0 () = {
}
```

But it causes following compile error:

```
$ patscc -D_GNU_SOURCE -DATS_MEMALLOC_LIBC main.dats -lpthread
main_dats.c:122:1: warning: parameter names (without types) in
function declaration
122 | atstkind_t0ype(atstype_char) atstyarr_field(atslab__name) ;
| ^~~~~~~~~~~~~~
In file included from main_dats.c:14:
/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats_ccomp_typedefs.h:176:31:
error: field ‘atstyarr_field_undef’ declared as a function
176 | #define atstyarr_field(fname) atstyarr_field_undef(fname)
| ^~~~~~~~~~~~~~~~~~~~
main_dats.c:122:30: note: in expansion of macro ‘atstyarr_field’
122 | atstkind_t0ype(atstype_char) atstyarr_field(atslab__name) ;
| ^~~~~~~~~~~~~~
main_dats.c:127:1: warning: parameter names (without types) in
function declaration
127 | atstkind_t0ype(atstype_char) atstyarr_field(atslab__name) ;
| ^~~~~~~~~~~~~~
In file included from main_dats.c:14:
/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats_ccomp_typedefs.h:176:31:
error: field ‘atstyarr_field_undef’ declared as a function
176 | #define atstyarr_field(fname) atstyarr_field_undef(fname)
| ^~~~~~~~~~~~~~~~~~~~
main_dats.c:127:30: note: in expansion of macro ‘atstyarr_field’
127 | atstkind_t0ype(atstype_char) atstyarr_field(atslab__name) ;
| ^~~~~~~~~~~~~~
In file included from main_dats.c:15:
main_dats.c: In function ‘kern_kldstat_0’:
/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats_ccomp_instrset.h:226:45:
error: ‘postiats_tyrec_0’ has no member named ‘atslab__name’
226 | #define ATSSELfltrec(pmv, tyrec, lab) ((pmv).lab)
| ^
/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats_ccomp_instrset.h:339:34:
note: in definition of macro ‘ATSINSstore’
339 | #define ATSINSstore(pmv1, pmv2) (pmv1 = pmv2)
| ^~~~
main_dats.c:218:13: note: in expansion of macro ‘ATSSELarrptrind’
218 | ATSINSstore(ATSSELarrptrind(ATSSELfltrec(ATSderef(arg0,
postiats_tyrec_0), postiats_tyrec_1, atslab__name),
atstkind_t0ype(atstype_char), [ATSPMVi0nt(0)]), ATSPMVchar('A')) ;
| ^~~~~~~~~~~~~~~
main_dats.c:218:29: note: in expansion of macro ‘ATSSELfltrec’
218 | ATSINSstore(ATSSELarrptrind(ATSSELfltrec(ATSderef(arg0,
postiats_tyrec_0), postiats_tyrec_1, atslab__name),
atstkind_t0ype(atstype_char), [ATSPMVi0nt(0)]), ATSPMVchar('A')) ;
| ^~~~~~~~~~~~
make: *** [../../../../../../mk/ats2-fix.mk:2: all] Error 1
```

How to define array members in typedef using pure ATS language?
Or should we use the following idiom with C language?

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TESTATS/fieldarr.dats#L20

Best regards,
--
Kiwamu Okabe at METASEPI DESIGN

Dambaev Alexander

unread,
Oct 13, 2020, 10:49:40 AM10/13/20
to ats-lan...@googlegroups.com
Hi,

```
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

#define ATS_DYNLOADFLAG 0

%{^

#define \
atstyarr_field_undef(fname) fname[]
%} // end of [%{]

#define MAXPATHLEN 1024

typedef kld_file_stat = @{ version = int, name = (@[char][MAXPATHLEN])}


fun kern_kldstat {l:addr} (pf: !kld_file_stat @ l | p: ptr l): int = 0 where {
  val () = !p.name.[0] := 'A'
}

implement main0 () = {
}

пн, 12 окт. 2020 г. в 06:17, Kiwamu Okabe <kiw...@debian.or.jp>:
--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAEvX6dny%2BLS%2B4NDyGeon0j6VPPwKHATvsKFYOsT09ZD1rOEzUw%40mail.gmail.com.

Kiwamu Okabe

unread,
Oct 13, 2020, 7:09:48 PM10/13/20
to ats-lang-users
Thanks for your kind reply.

On Tue, Oct 13, 2020 at 11:49 PM Dambaev Alexander
<ice.r...@gmail.com> wrote:
> %{^
> #define \
> atstyarr_field_undef(fname) fname[]
> %} // end of [%{]

That works for me!
But I have questions. > Hongwei

A. Why is it not supported by the official ATS compiler?
B. Will it be supported by the ATS3 compiler?

Kiwamu Okabe

unread,
Oct 13, 2020, 7:22:40 PM10/13/20
to ats-lang-users
Dear all,

On Tue, Oct 13, 2020 at 11:49 PM Dambaev Alexander
<ice.r...@gmail.com> wrote:
> %{^
> #define \
> atstyarr_field_undef(fname) fname[]
> %} // end of [%{]
>
> #define MAXPATHLEN 1024
>
> typedef kld_file_stat = @{ version = int, name = (@[char][MAXPATHLEN])}

And following more complex code is not compilable:

https://github.com/jats-ug/practice-ats/blob/master/array_typedef/main.dats

```ats
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

%{^
#define atstyarr_field_undef(fname) fname[]
%} // end of [%{]

#define MAXPATHLEN 1024

typedef kld_file_stat = @{ version = int, name = @[char][MAXPATHLEN],
refs = int }

fun kern_kldstat {l:addr} (pf: !kld_file_stat @ l | p: ptr l): int = 0 where {
val () = !p.name.[0] := 'A'
val () = !p.refs := 1
}

implement main0 () = {
}
```

Dambaev Alexander

unread,
Oct 13, 2020, 7:50:18 PM10/13/20
to ats-lan...@googlegroups.com
GCC says, that it wants array field at the end of the struct, so:
```
typedef kld_file_stat = @{ version = int, refs = int, name = @[char][MAXPATHLEN] }
```
works


Kiwamu Okabe

unread,
Oct 13, 2020, 7:55:06 PM10/13/20
to ats-lang-users
Thanks for your reply:

On Wed, Oct 14, 2020 at 8:50 AM Dambaev Alexander <ice.r...@gmail.com> wrote:
> GCC says, that it wants array field at the end of the struct, so:
> ```
> typedef kld_file_stat = @{ version = int, refs = int, name = @[char][MAXPATHLEN] }
> ```

Yes. But there are more complex examples such as:

https://github.com/freebsd/freebsd/blob/master/sys/sys/linker.h#L334

```c
struct kld_file_stat {
int version; /* set to sizeof(struct kld_file_stat) */
char name[MAXPATHLEN];
int refs;
int id;
caddr_t address; /* load address */
size_t size; /* size in bytes */
char pathname[MAXPATHLEN];
};
```

It should have two arrays in a typedef.

Dambaev Alexander

unread,
Oct 13, 2020, 9:01:51 PM10/13/20
to ats-lan...@googlegroups.com
in this case the issue is that ```fname[]``` contains no size information.
This one works:
```
#define MAXPATHLEN 1024
%{^
#define MAXPATHLEN 1024
#define atstyarr_field_undef(fname) fname[MAXPATHLEN]

%} // end of [%{]


typedef kld_file_stat = @{ version = int, name = @[char][MAXPATHLEN], refs = int }
```

but I can't say how to make size not hardcoded

Dambaev Alexander

unread,
Oct 13, 2020, 9:07:51 PM10/13/20
to ats-lan...@googlegroups.com
so in this particular case it will work, but proper fix is to dig into codegen in order to fix the generation of atstyarr_field to have a size information


Kiwamu Okabe

unread,
Oct 13, 2020, 10:24:16 PM10/13/20
to ats-lang-users
Thanks for your kind reply.

Does it mean I should apply some patch to the ATS2 compiler?
If so, I would like to wait for the initial release of ATS3 and try that.

Dambaev Alexander

unread,
Oct 14, 2020, 10:49:28 AM10/14/20
to ats-lan...@googlegroups.com
By the way,

I decided to spend some more time on your issue and got this:
```
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

#define ATS_DYNLOADFLAG 0

#define FIELD0_SIZE 100
#define FIELD1_SIZE 200
%{^
#define FIELD0_SIZE 100
#define FIELD1_SIZE 200

struct kld_file_stat {
  int version;
  char name[FIELD0_SIZE];
  int refs;
  char test[FIELD1_SIZE];
};


%} // end of [%{]


typedef kld_file_stat = $extype_struct"struct kld_file_stat" of
{ version = int
, name = @[char][FIELD0_SIZE]
, refs = int
, test = @[char][FIELD1_SIZE]

}

fun kern_kldstat {l:addr} (pf: !kld_file_stat @ l | p: ptr l): int = 0 where {
  val () = !p.name.[0] := 'A'
  val () = !p.refs := 1
}

implement main0 () = {
}
```
So, codegen uses kld_file_stat, defined in C. At the same time, it should be noted, that typechecker will not be able to proof, that ATS record represented as the same struct in C, so you can have:
```
{ version = int
, name = @[char][FIELD0_SIZE]
, refs = int
, test = @[char][FIELD0_SIZE]
}
```

without errors from compiler

Kiwamu Okabe

unread,
Oct 14, 2020, 7:15:06 PM10/14/20
to ats-lang-users
Dear Dambaev,

On Wed, Oct 14, 2020 at 11:49 PM Dambaev Alexander
<ice.r...@gmail.com> wrote:
> I decided to spend some more time on your issue and got this:

Thanks for sharing. It also works in my environment.
It seems like following way:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TESTATS/fieldarr.dats#L20

It also makes hints to support it on pure ATS3 codegen.
Reply all
Reply to author
Forward
0 new messages