<div dir="ltr">Hello folks,<div><br></div><div>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><br></div><div>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></div><div><br></div><div><br></div><div><font face="monospace, monospace">__attribute__((annotate("pure"))</font></div><div><font face="monospace, monospace">int increment(int x){</font></div><div><font face="monospace, monospace">    return x + 1;</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">_</font><span style="font-family:monospace,monospace">_attribute__((annotate("pure")))</span></div><div><font face="monospace, monospace">int add(uint32_t a, uint32_t b)  {</font></div><div><font face="monospace, monospace">  int c = a;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  /* addition via pieno arithmetic */</font></div><div><font face="monospace, monospace">  while(b != 0){</font></div><div><font face="monospace, monospace">    c = increment(c);  // Okay to call pure code directly.</font></div><div><font face="monospace, monospace">    b--;</font></div><div><font face="monospace, monospace">  }</font></div><div><br></div><div><font face="monospace, monospace">  printf("%d + %d = %d\n", a, b, c) __attribute__((annotate("call_impure")));</font></div><div><font face="monospace, monospace">  // calling impure code requires explicit annotation</font></div><div><font face="monospace, monospace">  // all the arguments to the function are copied </font></div><div><font face="monospace, monospace">  // and don't share the same stack as pure code</font></div><div><font face="monospace, monospace">  return c;</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">int main(int argc, char* argvp[]){</font></div><div><font face="monospace, monospace">   a = 1;</font></div><div><font face="monospace, monospace">   b = 2;.</font></div><div><font face="monospace, monospace">   c = add(a,b)</font></div><div><font face="monospace, monospace">   printf("%d + %d = %d", a, b); // okay to call impure from impure</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div><br></div><div>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><br></div><div>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><br></div><div>Thanks</div><div>Suman</div><div><br></div><div>PS: I'm not subscribed to LLVM mailing list so please reply-all.</div></div>