<div>This probably a better question for the clang developer list cfe-dev.</div><div><br><div class="gmail_quote"><div>On Sun, Apr 9, 2017 at 12:01 PM Suman Thakur via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_msg">Hello folks,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I'm not a compiler expert or subscribed to this mailing list, but I have a unique problem. I need to split a large piece of C/C++ code into two separate libraries: one library that only has pure code (i.e., code that doesn't require operating system interactions) and other library that can have both pure code and side-effecting code. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I was was wondering if it's possible to achieve this by adding something like __attribute__((annotate("pure"))) and __attribute__((annotate("call_impure"))) and how much effort will it be to add such a functionality. Basically, I want "pure" attribute to be sticky, in the sense that every function that's pure, can only call pure code unless explicitly marked inside the function to make impure call. For example, the following code should create one library for pure code, and a driver which has all the impure code.<br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">__attribute__((annotate("pure"))</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">int increment(int x){</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">    return x + 1;</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">}</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">_</font><span style="font-family:monospace,monospace" class="gmail_msg">_attribute__((annotate("pure")))</span></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">int add(uint32_t a, uint32_t b)  {</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  int c = a;</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  /* addition via pieno arithmetic */</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  while(b != 0){</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">    c = increment(c);  // Okay to call pure code directly.</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">    b--;</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  }</font></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  printf("%d + %d = %d\n", a, b, c) __attribute__((annotate("call_impure")));</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  // calling impure code requires explicit annotation</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  // all the arguments to the function are copied </font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  // and don't share the same stack as pure code</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">  return c;</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">}</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">int main(int argc, char* argvp[]){</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">   a = 1;</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">   b = 2;.</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">   c = add(a,b)</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">   printf("%d + %d = %d", a, b); // okay to call impure from impure</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">}</font></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">The call to printf in pure code should create a stub function call printf_impure_call(), which in the impure library just calls printf. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I know very little about compilers (mathematician by training), but I will really appreciate if someone can comment about the feasibility of this.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Thanks</div><div class="gmail_msg">Suman</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">PS: I'm not subscribed to LLVM mailing list so please reply-all.</div></div>
_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
</blockquote></div></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature">~Craig</div>