Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007581OCamlruntime system and C interfacepublic2017-07-11 21:032017-07-26 18:44
Reporternojebar 
Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0007581: CAMLlocalN does not compile with msvc compiler
DescriptionThe msvc compiler allows variable declarations only after opening braces. As such the CAMLlocalN macro does not errors out.

Is there some C macro wizardy that would make this work ?
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0018065)
yallop (developer)
2017-07-12 10:23

The difficulty is apparently caused by the loop that initializes 'x':

  for (caml__i_##x = 0; caml__i_##x < size; caml__i_##x ++) { \
    x[caml__i_##x] = Val_unit; \
  } \

CAMLlocalN inserts the output of CAMLxparamN (i.e. some additional declarations) after the loop, but this kind of mixing of declarations and code is not allowed in C89.

One possible fix is to move the array initialization code to an inline function (assuming it can be guaranteed that the function is actually always inlined) and replace the loop by a dummy declaration whose rhs calls the function:

  CAMLunused_start int caml__dummy_##x = (caml_initialize_array(x, size), 0);
(0018070)
xleroy (administrator)
2017-07-12 19:07

Would it be possible to initialize the array with zeros instead of Val_unit? (I think it's OK with the current GC but maybe not with the no-naked-pointer stuff.) In this case,

  int x[N] = {0, };

is enough to initialize the local array x with all zeroes, regardless of its size N.
(0018071)
xleroy (administrator)
2017-07-12 19:11

Wading through history, I see zero-initialization was used (correctly) until commit https://github.com/ocaml/ocaml/commit/05100e597e4296a2e79e6c2d9cd75b7e1cc595c9 [^] and following, which insisted on Val_unit initialization.
(0018092)
nojebar (reporter)
2017-07-18 15:48

From Github's discussion: this issue only affects versions prior to Visual 2013.
(0018096)
stedolan (developer)
2017-07-18 20:41

It's also possible to just add some more curly braces. C89 insists that declarations come at the start of a block, not necessarily at the start of a function.

Something like this should work:

{ \
  int caml__i_##x; \
  for (caml__i_##x = 0; caml__i_##x < size; caml__i_##x ++) { \
    x[caml__i_##x] = Val_unit; \
  } \
}
(0018097)
nojebar (reporter)
2017-07-18 20:44

This works if CAMLlocalN is the last declaration in the enclosing function.
(0018140)
stedolan (developer)
2017-07-26 18:44

> This works if CAMLlocalN is the last declaration in the enclosing function.

Of course, you're right. I think yallop's solution works in all cases, as does zero-initialisation (with the appropriate check in caml_do_local_roots).

- Issue History
Date Modified Username Field Change
2017-07-11 21:03 nojebar New Issue
2017-07-12 10:23 yallop Note Added: 0018065
2017-07-12 19:07 xleroy Note Added: 0018070
2017-07-12 19:07 xleroy Status new => acknowledged
2017-07-12 19:11 xleroy Note Added: 0018071
2017-07-18 15:48 nojebar Note Added: 0018092
2017-07-18 20:41 stedolan Note Added: 0018096
2017-07-18 20:44 nojebar Note Added: 0018097
2017-07-26 18:44 stedolan Note Added: 0018140


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker