[M3devel] suspicious order of evaluation?

Antony Hosking hosking at cs.purdue.edu
Thu Oct 25 16:43:27 CEST 2012


I’m not sure that Modula-3 defines any order of initialization (unlike Java constructors and the JMM), so implementations are free to choose.
So, programmers that care must be careful not to expose uninitialized objects by assigning them to globals only after the initializer has run.
And of course in a multi-core setting you’d need to enforce fences accordingly.
I agree that it might make sense to implement they way you suggest.

Antony Hosking | Associate Professor | Computer Science | Purdue University
305 N. University Street | West Lafayette | IN 47907 | USA
Mobile +1 765 427 5484





On Oct 25, 2012, at 1:42 AM, Jay K <jay.krell at cornell.edu> wrote:

>   m3tohtml/src/Main.m3:  
> 
>   sources is global  
> 
>   
>     PROCEDURE Add() =    
>       BEGIN    
>         sources := NEW (Source, next := sources,    
>                         from := Pathname.Join(pkgpath, file, NIL),    
>                         to   := Pathname.Join(pkg, FixDerived (file), NIL),    
>                         kind := kind);    
>   
> 
>   It APPEARS this compiles to like:    
>   
> 
>   temp = RTHooks__AllocateTracedRef(...);  
>   sources = temp  
>   if temp == NULL  
>     raise exception  
>   sources->from = ...  
>   sources->to = ...  
>   
> 
>   To me this seems a bit suspicious.  
> 
>   
>   First, the null check should be against the temp.  
>   Sources should not be assigned and then checked.  
>     
>     
>   Second, probably, the fields of sources should be filled in before  
>   the global.  
>     
>     
>   So, it should be more like:  
> 
>     
>   temp = RTHooks__AllocateTracedRef();  
>   if temp == NULL  
>     raise exception  
>   temp->from = ...  
>   temp->to = ...  
>   sources = temp  
>   ...  
> 
> 
> Just as efficient, but "safer".
> 
> 
> Now, I realize, without a lock or barrier, the compiler is free to reorder like this.
> Maybe ideally temp would be gone but the original order used?
> 
> 
> C++ would implement it more like how I showed.
> 
> 
> Thoughts?
> 
> 
>  - Jay

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20121025/4ca68b1b/attachment-0002.html>


More information about the M3devel mailing list