Hi Mariano,<br><br><div class="gmail_quote">On Mon, Dec 20, 2010 at 1:22 AM, Mariano Martinez Peck <span dir="ltr">&lt;<a href="mailto:marianopeck@gmail.com">marianopeck@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
 <br>Hi Eliot. I noticed that in the Cog VM there are a lot of asserts in the code. I am trying to understand them but I have a couple of questions:<br><br>- What happens with the code execution when an assert fails?  it stops the execution?  it continues? it raises an error?  I tried to follow to the definition of assert() but I only came to this:<br>

# define assert(expr)  ((expr)||(warning(#expr &quot; &quot; __stringifyNum(__LINE__)),0))<br></blockquote><div><br></div><div>There are a few defines for asserts (in platforms/Cross/vm/sqAssert.h); here&#39;s the entire file:</div>
<div><br></div><div><div>/*</div><div> * An informative assert definition that prints expression and line number.</div><div> *</div><div> * assert&#39;s expression is evaluated only if NDEBUG is not defined and printed</div>
<div> * along with its the line number if it is false.</div><div> *</div><div> * asserta&#39;s expression is always evaluated but only printed if it is false and</div><div> * NDEBUG is not defined. (asserta =&gt; assert always)</div>
<div> *</div><div> * assertf&#39;s message is printed along with its line number if NDEBUG is not</div><div> * defined. (assertfd =&gt; assert fail)</div><div> */</div><div><br></div><div>extern void warning(char *);</div>
<div><br></div><div>#ifdef NDEBUG /* compatible with Mac OS X (FreeBSD) /usr/include/assert.h */</div><div># undef assert</div><div># define assert(expr) 0 /* hack disabling of asserts.  Better in makefile? */</div><div># define asserta(expr) (expr)</div>
<div># define assertf(msg) 0</div><div># define PRODUCTION 1</div><div>#elif 1</div><div># undef assert</div><div># define __stringify(foo) #foo</div><div># define __stringifyNum(n) __stringify(n)</div><div># define assert(expr)  ((expr)||(warning(#expr &quot; &quot; __stringifyNum(__LINE__)),0))</div>
<div># define asserta(expr) ((expr)||(warning(#expr &quot; &quot; __stringifyNum(__LINE__)),0))</div><div># define assertf(msg)  (warning(#msg &quot; &quot; __stringifyNum(__LINE__)),0)</div><div># define PRODUCTION 0</div>
<div>#endif</div><div><br></div><div>#define halt() warning(&quot;halt&quot;)</div></div><div><br></div><div> So there are three assert functions, assert, asserta and assertf.  These all print warnings if enabled; they do /not/ terminate execution - in the VW/HPS experience getting asserts right can be hard so having them produce warnings is more convenient than having them terminate execution.</div>
<div><br></div><div>Asserts are optional checks.  They are not present in the production VM (since evaluating asserts may cost performance).  You&#39;ll see in the Cog VM I build three VMs, the production one without asserts, the Assert VM with asserts enabled and with moderate optimization, good for smoke testing, and the Debug VM with asserts enabled but no optimization, good for debugging.</div>
<div><br></div><div>If enabled, assert prints a warning with the expression that failed, and the line number</div><div><br></div><div><div>asserta like assert prints a warning if its expression is false, but its expression is /always/ evaluated.  So if you disable asserts its expression is still evaluated.  Hence</div>
<div># define asserta(expr) (expr)</div><div>So if you have an expression that is being evaluated for effect but you still want to check, use asserta.</div><div><br></div><div>assertf, if enabled, always prints a warning, so you use it when control reaches a point it shouldn&#39;t, e.g. the end of a chain of if-the-elses.</div>
<div><br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>So....after time, I understood that when I compile in &quot;development&quot; and I run with GDB, and I have something printed in the console like:<br>

<br>(primitiveIndexOf(GIV(newMethod))) != 0 8913<br><br>this means that in the line 8913 the assert has failed?  so, do I understand correct?   when an assert fails, the code continues to execute, but it just prints the expresion + number of line in the console?<br>

if it doesn&#39;t fail, it doesn&#39;t print.<br></blockquote><div><br></div><div>that&#39;s right.</div><div><br></div><div>HTH</div><div>Eliot</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>thanks in advance,<br><br>Mariano<br><br>
<br></blockquote></div><br>