Re: How to implement dynamic typing?

Mike Pall <mikelu-1004cc@mike.de>
Wed, 21 Apr 2010 12:23:48 +0200

          From comp.compilers

Related articles
[17 earlier articles]
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-14)
Re: How to implement dynamic typing? dot@dotat.at (Tony Finch) (2010-04-14)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-16)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-18)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-18)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-20)
Re: How to implement dynamic typing? mikelu-1004cc@mike.de (Mike Pall) (2010-04-21)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-21)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-22)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-23)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB) (2010-04-23)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-24)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-26)
[1 later articles]
| List of all articles for this month |

From: Mike Pall <mikelu-1004cc@mike.de>
Newsgroups: comp.compilers
Date: Wed, 21 Apr 2010 12:23:48 +0200
Organization: Compilers Central
References: 10-04-009 10-04-028 10-04-031 10-04-036 10-04-038 10-04-051
Keywords: types
Posted-Date: 21 Apr 2010 10:48:42 EDT

George Neuner wrote:
> On 14 Apr 2010 17:55:11 +0100 (BST), Tony Finch wrote:
> >LuaJIT takes an interesting approach. Its only numeric type is the
> >double precision floating point number. It uses different kinds of NAN
> >as tags for the other types, with the pointer embedded in the
> >mantissa.
> >
> >http://article.gmane.org/gmane.comp.lang.lua.general/44823
> >
> >Regarding typed word-sized pointers, see for instance
> >http://www.haskell.org/~simonmar/papers/ptr-tagging.pdf
>
> That is interesting. It does, however, prevent loading tagged values
> directly as doubles if the CPU might throw an exception on a NAN.


The type check is performed in the integer unit _before_ loading
anything into the FPU. Only values tagged as numbers are loaded by the
FPU (they may be NaN, too, but this is uncommon).


[Also, none of the popular architectures would throw an exception on a
NaN load. They only slow down when doing arithmetics with NaNs.]


> Also the IEEE storage format for doubles leaves the tag in the
> middle of the first word (the sign bit is first - NANs are encoded
> in the exponent). That makes tag checking/extraction cumbersome on a
> 32-bit machine.


It's not necessary to extract the type tag from the middle. There are
enough tags available in the range 0xfff80001 .. 0xffffffff. Using
small signed numbers (e.g. -1 .. -12) allows shorter instruction
encodings, too.


The following post shows the assembler code for a dynamic type check
(in the context of the interpreter part of LuaJIT):


    http://www.reddit.com/r/programming/comments/badl2/luajit_2_beta_3_is_out_support_both_x32_x64/c0lrus0


The dynamic type check is very cheap on a super-scalar CPU as it's
out of the dependency chain. I should mention the trace compiler
can get rid of most of these type checks of course.


--Mike



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.