<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bytecode &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/bytecode/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Mon, 21 Jun 2010 20:10:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>bytecode &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Scripting Flash apps: design the language</title>
		<link>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/</link>
					<comments>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sat, 29 Nov 2008 18:28:35 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[pixelbender]]></category>
		<category><![CDATA[scripting]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=268</guid>

					<description><![CDATA[Finally I had a bit of time to go back to the discussion I started about a month ago: scripting your flash applications. I spent a bit of time thinking which is the best approach to start discussing this kind of topic; it is not as easy as it seems because the risk is to &#8230; <a href="https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/" class="more-link">Continue reading <span class="screen-reader-text">Scripting Flash apps: design the language</span></a>]]></description>
										<content:encoded><![CDATA[<p>Finally I had a bit of time to go back to the <a title="Scripting your Flash applications …" href="https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/">discussion</a> I started about a month ago: <strong>scripting your flash applications</strong>. I spent a bit of time thinking which is the best approach to start discussing this kind of topic; it is not as easy as it seems because the risk is to fall into a too complex discussion that a lot of people won&#8217;t benefit from.</p>
<p>So I decided to start with a simple example that is somehow related to <a href="http://aviary.com">what I&#8217;m doing</a> in these months; more examples will come and I&#8217;ll analyze different aspects of scripting trying always to keep the discussion as simple as possible even if this may bring me to do avoid some hard to discuss approaches or optimizations.</p>
<p>Our first scripting language will be an extremely simple language that will be used to <strong>manipulate images</strong> (like <a href="http://labs.adobe.com/technologies/pixelbender/">Pixel Bender</a> does). Obviously the goal is not to write something that can be compared with Pixel Bender, but at least we would like to provide an implementation that will lead to <strong>scripts as fast as native code</strong> (at least we hope so :P).</p>
<p>The first step is to design the language, and to do that we need to do the step zero that is to define the goals we want to achive with the first implementation:</p>
<ul>
<li>The language must be extremely <strong>simple to design and implement</strong> (it is a didactic stuff indeed!);</li>
<li>The language must be <strong>as simple as possible to learn and use</strong>: we won&#8217;t be an alternative to Pixel Bender (because we can&#8217;t &#8230; yet ;P)</li>
<li>The language must be <strong>quick to compile and execute</strong>.</li>
<li>A script must accept some parmeters to control its behaviour.</li>
<li>We must support comments!</li>
</ul>
<p>Now that we decided which are the goals, let&#8217;s start definign some limitation or features we must include to quickly reach a solution:</p>
<ul>
<li>The first implementation of the language will support a <strong>sigle data type: Numbers</strong>. We will move further adding more data types later &#8211; I don&#8217;t want to focus on type checking yet.</li>
<li>We will add support for basic expression operators, basic control flow. No support for functions yet.</li>
<li>The language will provide some useful operators or <strong>builtin functions</strong> to manage common operations like packing/unpacking color components and peform some mathematical operations.</li>
<li>Arguments to a script will be only Numbers at first.</li>
<li>A script will be defined as a simple sequence of arguments followed by a function definition. This function will be applied sequentially to every pixel of one or more source images.</li>
<li>We will provide to implementations for the language: a simple (and slow) <strong>interpreter</strong> at first, <strong>then we will generate native SWF code</strong> to speedup the performances.</li>
</ul>
<p>Even if briefly, it is quite important to define the goals of our language and then the features, because a lack of design (even if extremely simple and poor as the current one :P) may lead to unexpected issues during implementation.</p>
<p>Now that we defined those basic pointers, let&#8217;s see how the language will look like:<br />
<code>// Simple crossfade filter<br />
// The crossfade intensity<br />
argument intensity;<br />
/**<br />
* A quick explanation of the used builtins:<br />
*  - in( id, x, y ): gets the ARGB color at x and y of the image identified by id (there can be more than one image as input);<br />
*  - out( id, x, y, color ): sets the ARGB color at x and y for the output image identified by id;<br />
*  - argb( a, r, b, b ): build an ARGB color from its components (clamping and rounding the values as required);<br />
*/<br />
evaluatePixel( x, y )<br />
{<br />
// this kind of assignment is similar to the python unpack assignment<br />
// but extracts the 4 components (ARGB) from a color.<br />
a1, r1, g1, b1 = in( 0, x, y );<br />
a2, r2, g2, b2 = in( 1, x, y );<br />
out( 0, x, y, argb(<br />
( 1 - intensity ) * a1 + intensity * a2,<br />
( 1 - intensity ) * r1 + intensity * r2,<br />
( 1 - intensity ) * g1 + intensity * g2,<br />
( 1 - intensity ) * b1 + intensity * b2 ));<br />
}<br />
</code><br />
As a side not, I must say that the language may change during implementation because of unexpected limitations we will find during development.</p>
<p>Next week we will move forward start implementing the language. Stay tuned!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Scripting your Flash applications &#8230;</title>
		<link>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/</link>
					<comments>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Mon, 27 Oct 2008 22:59:03 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[sandbox]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[virtual machine]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=266</guid>

					<description><![CDATA[As someone of you may know, I really like studying programming languages and sometimes implement for fun (or for work) simple scripting languages that help me out during development. The fight between scripting and programming languages will probably never end. Well, it is not really a fight but more an infinite race where the first &#8230; <a href="https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/" class="more-link">Continue reading <span class="screen-reader-text">Scripting your Flash applications &#8230;</span></a>]]></description>
										<content:encoded><![CDATA[<p>As someone of you may know, I really like studying programming languages and sometimes implement for fun (or for work) simple <strong>scripting languages</strong> that help me out during development.</p>
<p>The <em>fight</em> between scripting and programming languages will probably never end. Well, it is not really a fight but more an infinite race where the first position is exchanged continuosly between the two participants.<br />
We need <strong>flexibility</strong> first, and so we add to our complex applications a way to support scripting to be able to easilly perform changes without the need to run the long time taking compilation process a low level language might take; then, the more the scripts became complex, the more we need an <strong>increase in performances</strong>, and so we rapidly move to compiled plugin egines; plugins offer a bit of flexibility and usually a good speed. Then we grant other users the permission to write plugins and so we need a sandbox to run the plugins in and to be sure every single line of code that is executed doesn&#8217;t harm our application. And so we revert back to scripting &#8230; and so on.<br />
The race will never end but the technologies will continue to increase in speed and sometimes in complexity.</p>
<p>All the introduction is to say that its not the point to choose if scripting is useful or not for Flash applications, but to say that <strong>someone might need it</strong> for reasons we cannot think about right now, and someone else might not need it because they are fine with recompiling everything from scratch at each single small change.</p>
<p>I don&#8217;t think there are some many Flash  applications that support scripting. I built myself a few of them for <a href="http://alittleb.it">my company</a> but they are for a particular purpose and probably the same motivations that made me decide to implement a scripting language are hard to find for other kind of applications.</p>
<p>As far as I know, the most of the applications that need to be flexible use a plugin system (more or less complicated &#8211; as <a href="http://a.viary.com">Aviary</a> for instance and <a href="http://hobnox.com">Hobnox</a>) and are fine with it because &#8211; we must say that &#8211; <strong>ActionScript is itself a scripting language</strong> and a SWF file is nothing more than a bunch of instructions (more or less) generated by compiled ActionScript.</p>
<p>But sometimes <strong>security</strong> comes into account, and it is hard (even if possible) to limit the funcionalities used by a plugin if this plugin is created by a 3rd party developer.</p>
<p>And so it comes into account scripting. With scripting we have <strong>full control over what a developer can do</strong> an how he can do that. We can limit it, provide builtin functionalities &#8230; But everything costs something, and building a fast/flexible/powerful/useful scripting languages is not that easy as it might seem.</p>
<p>This is why I decided to spend a bit of time in explaining the basics to implement a scripting language in actionscript to be able to script our applications. I&#8217;ll walk through all the passes &#8211; from lexing to bytecode generation and maybe a quick look into Virtual Machines &#8211; with the aim to provide someone (maybe in the far future) a bunch of documents to use as reference to build their own scripting language.</p>
<p>I&#8217;ll try to give an in depth look, but as far as the argument is quite complicated and full of facets, you may not find here the right solution for you. Nor the fastest or the most flexible. But it is better then nothing so if you are interested in that keep reading the blog 🙂</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Expressions evaluation at (almost) native speed</title>
		<link>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/</link>
					<comments>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Wed, 02 Jul 2008 00:00:00 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[applicationDomain]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[haxe]]></category>
		<category><![CDATA[HxASM]]></category>
		<category><![CDATA[swfcompiler]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=257</guid>

					<description><![CDATA[Finally I found a bit of time this weekend to do some other tests with expressions evaluation. The results are pretty interesting, even if obvious from some point of view. I took the ExpressionEvaluator I wrote as example for the first post about this topic, and then I edited a bit the code adding just &#8230; <a href="https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/" class="more-link">Continue reading <span class="screen-reader-text">Expressions evaluation at (almost) native speed</span></a>]]></description>
										<content:encoded><![CDATA[<p>Finally I found a bit of time this weekend to do some other tests with <strong>expressions evaluation</strong>.</p>
<p>The results are pretty interesting, even if obvious from some point of view. I took the ExpressionEvaluator I wrote as example for the <a title="Runtime expression evaluation in ActionScript" href="https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/">first post</a> about this topic, and then I edited a bit the code adding just in time <strong>AS Bytecode compilation</strong>.<br />
Thanks to that, expressions evaluation is much more fast and always safe because it runs in its own <em>ApplicationDomain</em>.</p>
<p>You can <a href="https://blog.sephiroth.it/wp-content/uploads/2008/07/ExpressionEvaluator_compiler.zip">download the sources here</a>, that includes the edited code and a test file. <strong>The test file runs 1 milion of iterations and may hang your browser or at worst case your system. Reduce the value of the ITERATIONS constant if you are not sure about the power of you machine</strong>.</p>
<p>If you want to read a bit more details about that, click on the link below to continue reading.</p>
<p><span id="more-257"></span></p>
<p>I did some simple modification to the existing classes:</p>
<ul>
<li>Added a <em>compile</em> method to the IExpression interface;</li>
<li>Added the <em>symbolNames</em> getter to the SymbolTable class, to retrieve a list of all the symbols defined inside the symbol table.</li>
<li>Converted hxasm to Actionscript (finxing some small issues to avoid to include all the Haxe support classes);</li>
<li>Implemented the <em>compile</em> method on all the AST nodes using HxASM;</li>
<li>Added an <em>SWFCompile</em> class that takes an IExpression and a SymbolTable and produces an SWF;</li>
<li>Edited the <em>CompiledExpression</em> class adding a <em>compile</em> method that generates an SWF starting from an <em>IExpression</em>.</li>
</ul>
<p>Adding just in time compilation was easy a really quick thanks to <a href="http://haxe.org/com/libs/hxasm">HxASM</a>. <strong>HxASM</strong> is an <a href="http://haxe.org">Haxe</a> library written by Haxe&#8217;s author (<strong>Nicolas Cannasse</strong>) that makes you able to write low level bytecode instructions in ActionScript an generate a <em>ByteArray</em> that contains an (hopefully) valid SWF. The SWF can then be loaded by <em>Loader.loadBytes</em> and executed.</p>
<p>It have been easy to port HxASM to ActionScript too, thanks again to the <strong>Haxe compiler which is able to generate AS3 code starting from Haxe code</strong>.</p>
<p>Compilation was achieved easilly because the expressions we are compiling are really simple. The compiler generates a class called <em>CompiledExpression</em> that has a method <em>execute</em> that can be called to execute the expression. All the values inside the symbol table are exposed as public properties.</p>
<p>Just to make you understand better, givin this expression:</p>
<p><code>sin( x * 10 + y - 11 )</code></p>
<p>SWFCompiler will generate the bytecode for this class:<br />
<code lang="actionscript">class CompiledExpression<br />
{<br />
public var sin: *;<br />
public var x: *;<br />
public var y: *;<br />
public function execute(): Number<br />
{<br />
return sin( x * 10 + y - 11 );<br />
}<br />
}</code><br />
As you can see the type of the class variables is generic, but you can improve the generator to understand if a variable should contain a number or a function.</p>
<p>The generated SWF can be loaded and run easilly:<br />
<code lang="actionscript">var loader: Loader = new Loader();<br />
var parser: Parser = new Parser(<br />
new Scanner( "sin( x / ( 4 / 2 * 2 - 2 + 2 * x / x ) ) * 100" ) );<br />
var compiled: CompiledExpression = parser.parse();<br />
var data: ByteArray = compiled.compile();<br />
loader.contentLoaderInfo.addEventListener( Event.COMPLETE,<br />
function( event: Event ): void<br />
{<br />
var info: LoaderInfo = ( event.target as LoaderInfo );<br />
var klass: Class = (<br />
info.applicationDomain.getDefinition( "CompiledExpression" ) as Class );<br />
var cp: Object = new klass();<br />
cp.x = 10;<br />
cp.sin = Math.sin;<br />
trace( "Result", cp.execute() );<br />
} );<br />
loader.loadBytes( data, new LoaderContext( false,<br />
new ApplicationDomain( ApplicationDomain.currentDomain ) ) );<br />
</code><br />
I build a simple test (shipped with the sources) that runs 1 milion times an expression using 3 methods: Evaluation, Compiled and Native.</p>
<p>Results (on my Mac Pro 8 Core, 4GB ram) are obvious:</p>
<pre>eval 1000000 iterations: 12939
<strong>compiled 1000000 iterations: 848</strong>
native 1000000 iterations: 371</pre>
<p>As you can see native execution is faster, but compiled version runs really well (you might want to optimize the generated code to make it run faster) and it is many times faster then AST evaluation.</p>
<p>If you want to run a complex expression many times and dont want to stress your Flash Plugin, you can use the compiled version.</p>
<p>Gotta run, see u next time!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
