Fix a subtle bug where OCaml/C function stack are not aligned on a 16 byte
[SXSI/xpathcomp.git] / src / xml-tree_stub.cpp
index 47b6c63..5dbdf36 100644 (file)
@@ -19,14 +19,14 @@ static xml_tree::tag_t TAG(value i)
   return static_cast<xml_tree::tag_t>(Int_val(i));
 }
 
-extern "C"  value caml_xml_tree_save(value tree, value fd, value prefix)
+ML_BINDING  value caml_xml_tree_save(value tree, value fd, value prefix)
 {
   CAMLparam3(tree, fd, prefix);
   XMLTREE(tree)->save(Int_val(fd), String_val(prefix));
   CAMLreturn (Val_unit);
 }
 
-extern "C" value
+ML_BINDING value
 caml_xml_tree_load(value fd, value prefix, value load_tc, value sf)
 {
   CAMLparam4(fd, prefix, load_tc, sf);
@@ -50,187 +50,188 @@ caml_xml_tree_load(value fd, value prefix, value load_tc, value sf)
   return (Val_unit);
 }
 
-NoAlloc extern "C"  value caml_xml_tree_root(value tree)
+NoAlloc ML_BINDING  value caml_xml_tree_root(value tree)
 {
   return (Val_int(XMLTREE(tree)->root()));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_size(value tree)
+NoAlloc ML_BINDING  value caml_xml_tree_size(value tree)
 {
   return (Val_int(XMLTREE(tree)->size()));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_num_tags(value tree)
+NoAlloc ML_BINDING  value caml_xml_tree_num_tags(value tree)
 {
   return (Val_int(XMLTREE(tree)->num_tags()));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_subtree_size(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_subtree_size(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->subtree_size(TREENODE(node))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_subtree_tags(value tree, value node, value tag)
 {
   return (Val_int(XMLTREE(tree)->subtree_tags(TREENODE(node),
                                               TAG(tag))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_subtree_elements(value tree, value node)
+NoAlloc ML_BINDING value caml_xml_tree_subtree_elements(value tree,
+                                                        value node)
 {
   return (Val_int(XMLTREE(tree)->subtree_elements(TREENODE(node))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_is_leaf(value tree, value node){
+NoAlloc ML_BINDING value caml_xml_tree_is_leaf(value tree, value node){
   return (Val_bool(XMLTREE(tree)->is_leaf(TREENODE(node))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_is_ancestor(value tree, value node1, value node2)
 {
   return (Val_bool(XMLTREE(tree)->is_ancestor(TREENODE(node1),
                                               TREENODE(node2))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_is_child(value tree, value node1, value node2)
 {
   return (Val_bool(XMLTREE(tree)->is_child(TREENODE(node1),
                                            TREENODE(node2))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_is_first_child(value tree, value node)
+NoAlloc ML_BINDING value caml_xml_tree_is_first_child(value tree, value node)
 {
   return (Val_bool(XMLTREE(tree)->is_first_child(TREENODE(node))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_is_right_descendant(value tree, value x, value y)
 {
   return (Val_bool(XMLTREE(tree)->is_right_descendant(TREENODE(x),
                                                       TREENODE(y))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_num_children(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_num_children(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->num_children(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_child_pos(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_child_pos(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->child_pos(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_depth(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_depth(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->depth(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_preorder(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_preorder(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->preorder(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_postorder(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_postorder(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->postorder(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_tag(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_tag(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->tag(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_parent(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_parent(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->parent(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_child(value tree, value node, value idx)
+NoAlloc ML_BINDING  value caml_xml_tree_child(value tree, value node, value idx)
 {
   return (Val_int(XMLTREE(tree)->child(TREENODE(node), Int_val(idx))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_first_child(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_first_child(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->first_child(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_first_element(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_first_element(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->first_element(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_last_child(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_last_child(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->last_child(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_next_sibling(value tree, value node)
+NoAlloc ML_BINDING  value caml_xml_tree_next_sibling(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->next_sibling(TREENODE(node))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_next_element(value tree, value node)
+NoAlloc ML_BINDING value caml_xml_tree_next_element(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->next_element(TREENODE(node))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_prev_sibling(value tree, value node)
+NoAlloc ML_BINDING value caml_xml_tree_prev_sibling(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->prev_sibling(TREENODE(node))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_tagged_child(value tree, value node, value tag)
 {
   return (Val_int(XMLTREE(tree)->tagged_child(TREENODE(node),
                                               TAG(tag))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_select_child(value tree, value node, value tags)
 {
   return (Val_int(XMLTREE(tree)->select_child(TREENODE(node), TAGLIST(tags))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_tagged_sibling(value tree, value node, value tag)
 {
   return (Val_int(XMLTREE(tree)->tagged_sibling(TREENODE(node),
                                                 TAG(tag))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_select_sibling(value tree, value node, value tags)
 {
   return (Val_int(XMLTREE(tree)->select_sibling(TREENODE(node),
                                                 TAGLIST(tags))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_tagged_descendant(value tree, value node, value tag)
 {
   return (Val_int(XMLTREE(tree)->tagged_descendant(TREENODE(node),
                                                    TAG(tag))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_tagged_next(value tree, value node, value tag)
 {
   return (Val_int(XMLTREE(tree)->tagged_next(TREENODE(node),
                                              TAG(tag))));
 }
 
-NoAlloc extern "C" value
+NoAlloc ML_BINDING value
 caml_xml_tree_select_descendant(value tree, value node, value tags)
 {
   return (Val_int(XMLTREE(tree)->select_descendant(TREENODE(node),
                                                    TAGLIST(tags))));
 }
 
-NoAlloc extern "C" value caml_xml_tree_tagged_following_before(value tree,
+NoAlloc ML_BINDING value caml_xml_tree_tagged_following_before(value tree,
                                                                value node,
                                                                value tag,
                                                                value closing)
@@ -240,7 +241,7 @@ NoAlloc extern "C" value caml_xml_tree_tagged_following_before(value tree,
                                                          TREENODE(closing))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_select_following_before(value tree,
+NoAlloc ML_BINDING  value caml_xml_tree_select_following_before(value tree,
                                                                 value node,
                                                                 value tags,
                                                                 value closing)
@@ -252,7 +253,7 @@ NoAlloc extern "C"  value caml_xml_tree_select_following_before(value tree,
 
 
 
-extern "C" value caml_xml_tree_get_text_collection(value tree)
+ML_BINDING value caml_xml_tree_get_text_collection(value tree)
 {
   CAMLparam1(tree);
   CAMLlocal1(text);
@@ -261,17 +262,17 @@ extern "C" value caml_xml_tree_get_text_collection(value tree)
   CAMLreturn (text);
 }
 
-NoAlloc extern "C" value caml_xml_tree_closing(value tree, value node)
+NoAlloc ML_BINDING value caml_xml_tree_closing(value tree, value node)
 {
   return (Val_int(XMLTREE(tree)->closing(TREENODE(node))));
 }
 
-NoAlloc extern "C"  value caml_xml_tree_nullt(value unit){
+NoAlloc ML_BINDING  value caml_xml_tree_nullt(value unit){
   return (Val_int(xml_tree::NIL));
 }
 
 
-extern "C" value caml_xml_tree_print(value tree, value node, value fd)
+ML_BINDING value caml_xml_tree_print(value tree, value node, value fd)
 {
   CAMLparam3(tree, node, fd);
   XMLTREE(tree)->print(TREENODE(node), Int_val(fd));
@@ -279,7 +280,7 @@ extern "C" value caml_xml_tree_print(value tree, value node, value fd)
 }
 
 
-extern "C" value caml_xml_tree_get_tag_name(value tree, value tag)
+ML_BINDING value caml_xml_tree_get_tag_name(value tree, value tag)
 {
   CAMLparam2(tree, tag);
   CAMLlocal1(res);
@@ -288,13 +289,13 @@ extern "C" value caml_xml_tree_get_tag_name(value tree, value tag)
   CAMLreturn(res);
 }
 
-NoAlloc extern "C" value caml_xml_tree_flush(value tree, value fd)
+NoAlloc ML_BINDING value caml_xml_tree_flush(value tree, value fd)
 {
   XMLTREE(tree)->flush(Int_val(fd));
   return Val_unit;
 }
 
-extern "C" value caml_xml_tree_register_tag(value tree, value str)
+ML_BINDING value caml_xml_tree_register_tag(value tree, value str)
 {
   CAMLparam2(tree, str);
   value res;
@@ -305,7 +306,13 @@ extern "C" value caml_xml_tree_register_tag(value tree, value str)
 
 /** Full reporting into a bit vector
  */
-static std::vector<int32_t> sort_results(std::vector<int32_t> v)
+static void pr_vector(std::vector<int32_t> v)
+{
+  for(auto i = v.begin(); i!= v.end(); ++i)
+    fprintf(stderr, "%i ", *i);
+  fprintf(stderr, "\n");
+}
+static std::vector<int32_t> sort_results(std::vector<int32_t> v, xml_tree *t)
 {
   std::vector<int32_t> res;
   std::sort(v.begin(), v.end());
@@ -316,19 +323,19 @@ static std::vector<int32_t> sort_results(std::vector<int32_t> v)
       if (i == v.end()) return res;
     };
     prev = *i;
-    res.push_back(prev);
+    res.push_back(t->parent_node(prev));
   };
   return res;
 }
 
 #define BV_QUERY(pref, Pref) \
-  extern "C" value caml_text_collection_## pref ##_bv(value tree, value str, value dobvv){ \
-    CAMLparam3(tree, str, dobvv);                                              \
+  ML_BINDING value caml_text_collection_## pref ##_bv(value tree, value str, value dobvv){ \
+    CAMLparam3(tree, str, dobvv);                                      \
     CAMLlocal3(res, res_bv, res_array);                                        \
     int j;                                                             \
     uchar * cstr = (uchar *) strdup(String_val(str));                  \
     std::vector<int32_t> uresults = XMLTREE(tree)->Pref(cstr);         \
-    std::vector<int32_t> results = sort_results(uresults);                \
+    std::vector<int32_t> results = sort_results(uresults, XMLTREE(tree)); \
     bool dobv = Bool_val(dobvv);                                       \
     res_bv = caml_alloc_string(dobv ? ((XMLTREE(tree)->size() / 4) + 2) : 0); \
     unsigned long slen = caml_string_length(res_bv);                   \
@@ -336,12 +343,13 @@ static std::vector<int32_t> sort_results(std::vector<int32_t> v)
       memset(&(Byte(res_bv,0)), 0, slen);                              \
     res_array = caml_alloc_shr(results.size(), 0);                     \
     for (unsigned int i = 0; i < results.size(); ++i) {                        \
-      j = XMLTREE(tree)->parent_node(results[i]);                      \
+      j = results[i];                                                  \
       if (dobv)        {                                                       \
        Byte(res_bv, j >> 3) |=   (1 << (j & 7));                       \
       };                                                               \
       caml_initialize(&Field(res_array, i), Val_int(j));               \
     };                                                                 \
+    fprintf(stderr, "Raw results: %zu, Sorted results %zu\n", uresults.size(), results.size()); \
     free(cstr);                                                                \
     res = caml_alloc(2, 0);                                            \
     Store_field(res, 0, res_bv);                                       \