<div dir="ltr">Hi,<div><br></div><div>From your example, it's not clear to me what you want this feature to do. Do you want the finally block to execute:</div><div>1. When a C++ exception is thrown?</div><div>2. When normal control flow leaves the scope (goto, break, return)?</div><div>3. When a hardware trap occurs (div by zero, access violation, segv)?</div><div><br></div><div>3 is not implementable with LLVM today. It's not implemented properly even for __try / __finally.</div><div><br></div><div>1 and 2 are simple and can be accomplished with regular C++ destructors. You can do things like:</div><div>struct Cleanup {</div><div> Cleanup(std::function<void()> f) : f(f) {}</div><div> ~Cleanup() { f(); }</div><div> std::function<void()> f;<br>};</div><div>void foo() {</div><div> Cleanup c([&]() {</div><div> .. // any finally code here</div><div> });</div><div> // any try body code here</div><div>}</div><div><br></div><div>Reid</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018 at 9:26 AM João Paulo Labegalini de Carvalho via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-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 dir="ltr"><div>Hi,</div><div><br></div><div>I am working on a block annotation which should have a try-finally semantics. For example, given a code like this:</div><div><br></div><div><b>__speculate</b> <font color="#ff0000"><b>{</b></font></div><div> if (x > 2) {</div><div> y = 0;</div><div> }</div><div><font color="#ff0000"><b>}</b></font></div><div><br></div><div>clang should produce something like</div><div><br></div><div>__exec_mode = <b><font color="#ff0000">__begin_spec</font></b>();</div><div>if (__exec_mode == SW) {</div><div> if (__read_barrier(&x) > 2) {</div><div> __write_barrier(&y, 0);</div><div> }</div><div> <font color="#ff0000"><b>__end_spec</b></font>();</div><div>} else {</div><div> if (x > 2) {</div><div> y = 0;</div><div> }</div><div> <b><font color="#ff0000">__end_spec</font></b>();</div><div>}</div><div><br></div><div>such that __end_spec() is guaranteed to be called on every exit of the __speculate {} block.</div><div><br></div><div>It is my understanding that Clang implements try-finally statements as a Microsoft Specific Extension. However, I would not like to have such restriction, since such extension is only available for a reduced number of targets.</div><div><br></div><div>My question is, how should I implement try-finally semantics? Through an IR pass (fixing up every exit with a call to __end_spec()) or it is possible to extend from CXXTryStmt (reusing some CodeGen code)?</div>-- <br><div dir="ltr" class="m_688693615537025702m_863750444281404145gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">João Paulo L. de Carvalho<br>Computer Science | IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">joao.carvalho@ic.unicamp.br</a></div></div></div></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>