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-10-20 11:04
Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
PlatformOSOS Version
Product Version 
Target Version4.07.0+dev/beta2Fixed 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
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);
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.
xleroy (administrator)
2017-07-12 19:11

Wading through history, I see zero-initialization was used (correctly) until commit [^] and following, which insisted on Val_unit initialization.
nojebar (developer)
2017-07-18 15:48

From Github's discussion: this issue only affects versions prior to Visual 2013.
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; \
  } \
nojebar (developer)
2017-07-18 20:44

This works if CAMLlocalN is the last declaration in the enclosing function.
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).
xleroy (administrator)
2017-10-15 16:30

We need to decide a course of action.

One possibility is to partially revert [^] and following commits by supporting (again) zero-initialization of local roots (but only local roots). What this means is just an extra test "root != NULL" in the "Local C roots" part of [caml_oldify_local_roots].

Another possibility is to say that versions of MSVC prior to Visual 2013 are no longer officially supported.
shinwell (developer)
2017-10-20 11:04

@lpw25 and I were discussing adding an explicit Obj.null value (which would usually be represented by an actual zero). Doing this would necessitate the GC being safe against it even in no-naked-pointers mode. That value could then be used for the initialisation of the arrays in this issue.

For the moment I would suggest trying the inline function version, although I don't think you will be obtain a guarantee that the function is inlined.

- 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
2017-10-15 16:30 xleroy Note Added: 0018570
2017-10-15 16:31 xleroy Target Version => 4.07.0+dev/beta2
2017-10-20 11:04 shinwell Note Added: 0018604

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker