{"id":2440,"date":"2016-05-22T14:59:49","date_gmt":"2016-05-22T02:59:49","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=2440"},"modified":"2016-05-22T14:59:49","modified_gmt":"2016-05-22T02:59:49","slug":"fantasy-island-a-k-a-x86x64-codegen-using-oxygene","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/2440\/","title":{"rendered":"Fantasy Island [a.k.a x86\/x64 Codegen Using Oxygene]"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">4<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>This is a quick follow up post to further tease some of the exciting developments in the world of RemObjects Elements.  Yesterday I posted about implementing a Windows version of my trivially simple RandomNumber application.  Today, I present another Windows version.<\/p>\n<p>But this one doesn&#8217;t use .NET.<\/p>\n<p><!--more--><\/p>\n<p>First of all, the end result:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/win32.jpg?resize=640%2C46&#038;ssl=1\" alt=\"win32\" width=\"640\" height=\"46\" class=\"aligncenter size-full wp-image-2441\" srcset=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/win32.jpg?w=772&amp;ssl=1 772w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/win32.jpg?resize=300%2C22&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/win32.jpg?resize=768%2C56&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/win32.jpg?resize=500%2C36&amp;ssl=1 500w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/p>\n<p>True, it&#8217;s not as pretty as the OS X, Android or .NET versions of the application because as of right now the CPU-native codegen for x86\/x64 only supports console applications (the same holds true on the Linux side of things.  Currently).<\/p>\n<p>So let&#8217;s take a look at the code involved:<\/p>\n<pre class=\"brush: oxygene; title: ; notranslate\" title=\"\">\r\n\r\nnamespace RandomNumber.Win32;\r\n\r\n  uses\r\n    RandomNumber;\r\n\r\ntype\r\n  Program = class\r\n  public\r\n    class method Main(args: array of String): Int32;\r\n    begin\r\n      writeLn('The magic happens here.');\r\n      writeLn((new RandomNumber).next(100).ToString);\r\n    end;\r\n  end;\r\n\r\nend.\r\n<\/pre>\n<h2> Unified Class Declaration <\/h2>\n<p>The first thing to notice is that this looks a little strange for a Pascal implementation.<\/p>\n<p>The template for an <strong>Island<\/strong> Windows Console Application produces a source file using the new <em>Inline Implementation Syntax<\/em> (a.k.a <em>Unified Class Declaration<\/em>) support in the latest Oxygene compiler (beta).<\/p>\n<p>That is:  There are no separate <strong>interface<\/strong> and <strong>implementation<\/strong> sections.  <em>The implementation of methods can be provided inline as part of a class declaration itself<\/em>.<\/p>\n<p>Scratch one more reason for the curly bracket brigade to dismiss Pascal.  \ud83d\ude42<\/p>\n<p>For larger, more complex classes I personally think that the separation of declaration and implementation makes for more maintainable code, and you still have the option of using that syntax in those cases (or as you see fit).  But for small, lightweight classes such as this entry point class or extension methods etc, this inline syntax has a lot going for it in eliminating\/reducing duplication of method declarations.<\/p>\n<p>This syntax flexibility is available across all Oxygene platforms, obviously, and is not limited to only Island projects.<\/p>\n<h2> Cross Platform Concerns <\/h2>\n<p>The next thing to notice is that this implementation again makes use of my <strong>RandomNumber<\/strong> class.  I have extended the implementation of that class to cater for the native codegen platform.  With no .NET Framework available to a CPU-native application this means having to take a similar approach as adopted with the COCOA implementation for <strong>RandomNumber<\/strong>.  I could have implemented my own PRNG from first principles (and the lack of Posix style PRNG state buffers in Windows means that if I were intent on a comprehensive, cross-platform PRNG implementation then I might have to consider that).  But for the purposes of a quick proof of concept, I instead tried to use the C RTL functions provided by Elements&#8217; own <strong>rtl<\/strong> namespace.<\/p>\n<p>I say &#8220;tried&#8221; because I ran into a bit of a problem here however in that trying to use the <strong>rand<\/strong> and <strong>srand<\/strong> functions of the <strong>rtl<\/strong> resulted in a linker failure.  The <strong>timeGetTime<\/strong> function presented no such problem however.<\/p>\n<p>It&#8217;s perhaps to be expected that there will be some teething troubles like this in what is after all the very first beta drop of this technology.<\/p>\n<p>Something else to mention here is that importing functions from DLL&#8217;s is supported in Island projects, so I could have imported these functions from the Windows C RTL DLL myself.  But for now, a Not-at-all-PRNG was good enough for this exercise.  I could have simply <a href=\"https:\/\/xkcd.com\/221\/\">rolled a dice<\/a>; any solution here would be a temporary kludge but I settled on returning the current timer value <strong>mod<\/strong>&#8216;ed to the specified limiting range:<\/p>\n<pre class=\"brush: oxygene; title: ; notranslate\" title=\"\">\r\n  method RandomNumber.Next(limit: Integer): Integer;\r\n  begin\r\n    \/\/ Temporary bodge:\r\n    result := (rtl.timeGetTime mod limit) + 1;\r\n  end;\r\n<\/pre>\n<p>Other aspects of the RTL are clearly working perfectly, such as the availability of a <strong>ToString()<\/strong> method on the <strong>Integer<\/strong> type return value of my <strong>next()<\/strong> method.<\/p>\n<p>The final thing to note w.r.t cross platform questions and this code is that there is <em>nothing<\/em> in this program source file that is platform dependent.  You could place this <strong>program.pas<\/strong> file in a shared code project and then re-use this program source <em>directly<\/em> in Java, .NET and OS X console applications, as well as this Win32 application.<\/p>\n<h2> Garbage Collection <\/h2>\n<p>You might be thinking that without .NET to hold my hand I appear to have recklessly leaked a <strong>RandomNumber<\/strong> instance.  But if we take a look at the default references setup for my project in the solution we can see a clue as to why this is not a problem:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/gc.jpg?resize=239%2C190&#038;ssl=1\" alt=\"gc\" width=\"239\" height=\"190\" class=\"aligncenter size-full wp-image-2442\" data-recalc-dims=\"1\" \/><\/p>\n<p>Yes.  Island projects are CPU-native codegen but they also benefit from having a Garbage Collector.  For those interested in such details, I understand it is an implementation of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Boehm_garbage_collector\">Boehm GC<\/a><\/p>\n<h2> Linux <\/h2>\n<p>I mentioned yesterday that Island also supports Linux CPU-native codegen.  I personally don&#8217;t play in the Linux space and do not have a Linux VM to hand with which to repeat the exercise for Linux, but I have no difficulty believing that it would be just as straightforward.<\/p>\n<h2> Beta Access and Availability <\/h2>\n<p>If you are interested in these developments, you might be wondering how you can get involved in these beta&#8217;s ?<\/p>\n<p>It&#8217;s easy.<\/p>\n<p>RemObjects makes beta versions available to <em>all<\/em> current subscribers to their compilers, so if you are interested in exploring the capabilities of these new platforms (and of course the existing .NET, Java\/Android, iOS and OS X platform support) you only need to be a customer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">4<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> This is a quick follow up post to further tease some of the exciting developments in the world of RemObjects Elements. Yesterday I posted about implementing a Windows version of my trivially simple RandomNumber application. Today, I present another Windows version. But this one doesn&#8217;t use .NET.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4,300,180],"tags":[181],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-Dm","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2416,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2416\/","url_meta":{"origin":2440,"position":0},"title":"Relight My Fire","date":"01 May 2016","format":false,"excerpt":"I'm not sure how many more song inspired Fire references I can keep coming up with, but here at least is one more. In response to my previous post Wouter commented that he had a much simpler cross platform solution to the random number problem: Which is very pithy but\u2026","rel":"","context":"In &quot;Cooper&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2433,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2433\/","url_meta":{"origin":2440,"position":1},"title":"Fire, Walk With Me","date":"21 May 2016","format":false,"excerpt":"This final post in the mini-series re-creating a random number app for OS X, Android and .NET has taken a while not because it's complicated but because I've been distracted by a far more significant cross-platform project and some significant and exciting developments in the world of Fire and Elements.\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2574,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2574\/","url_meta":{"origin":2440,"position":2},"title":"Anonymous Classes: Identity Exposed!","date":"18 Feb 2017","format":false,"excerpt":"In my previous post on Anonymous Classes I erroneously referred to them as \"dynamic objects\" (thanks to commentors for pulling me up on that). Dynamic objects are something else entirely (although what precisely they might mean can vary on different platforms and in different languages). I have now corrected that\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2393,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2393\/","url_meta":{"origin":2440,"position":3},"title":"Come on Baby, Light My Fire","date":"25 Apr 2016","format":false,"excerpt":"Earlier this year, the Fire IDE for Elements was officially released after a fairly extensive beta. I have previously stuck with Visual Studio for the [relatively little] Elements work I have been doing but problems with my VM solution on a recently acquired MacBook Pro gave me the impetus to\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Fire-Initial-Project-300x214.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2597,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2597\/","url_meta":{"origin":2440,"position":4},"title":"(True = 1) and (True = &#8216;-1&#8217;) ?","date":"17 Mar 2017","format":false,"excerpt":"It has been observed that the Delphi documentation states that the constants True and False have the values 1 and 0 respectively, not the -1 and 0 that the default string conversions apply. This does actually make sense but also lays a trap for the unwary. True, True, Wherefore Art\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2460,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2460\/","url_meta":{"origin":2440,"position":5},"title":"The 9th Element","date":"24 Nov 2016","format":false,"excerpt":"In the periodic table of the elements, at #9 we find Fluorine. Curiously though the name \"Fluorine\" is not used (that I am aware of) anywhere in the Elements 9.0 release which dropped this week. But there is plenty of interest in this release, aside from Period Table curios. I'm\u2026","rel":"","context":"In &quot;Cooper&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2440"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=2440"}],"version-history":[{"count":5,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2440\/revisions"}],"predecessor-version":[{"id":2447,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2440\/revisions\/2447"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=2440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=2440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=2440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}