In-line assembly in an Abstract Syntax Tree

Programming, for all ages and all languages.
Post Reply
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

In-line assembly in an Abstract Syntax Tree

Post by azblue »

It is my understanding that creating an Abstract Syntax Tree is the last step of the front end of a compiler, and happens right before the conversion to intermediate code. How does in-line assembly fit into an AST? And how does it fit into intermediate code?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: In-line assembly in an Abstract Syntax Tree

Post by Brendan »

Hi,
azblue wrote:It is my understanding that creating an Abstract Syntax Tree is the last step of the front end of a compiler, and happens right before the conversion to intermediate code. How does in-line assembly fit into an AST? And how does it fit into intermediate code?
There isn't one answer - each different compiler may do this in completely different ways.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Thomas
Member
Member
Posts: 281
Joined: Thu Jun 04, 2009 11:12 pm

Re: In-line assembly in an Abstract Syntax Tree

Post by Thomas »

It is my understanding that creating an Abstract Syntax Tree is the last step of the front end of a compiler, and happens right before the conversion to intermediate code. How does in-line assembly fit into an AST? And how does it fit into intermediate code?

Abstract Syntax Tree is typically what you get after parsing, how detailed you want it is an implementation option. So it usually does not happen right before conversion to intermediate code, typically just after parsing is done. You can do some check on this tree, and even do some optimizations like constant folding here.

in-line assembly is part not part of the c grammar, therefore there are multiple ways to do it. For instance i can think of
(1) Add an additional asm statement to the grammar -> This ways it becomes part of the AST. and apply the seperate rules for this AST node.
(2) Have a compiler specific way of doing it. When you see an asm keyworkd, put everything into a buffer and hint the other layers what to do.

PS: I am not a compiler engineer, Most compilery thing i have done is report bugs to compiler teams and say you folks messed up :D.

--Thomas
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: In-line assembly in an Abstract Syntax Tree

Post by Solar »

Thomas wrote:in-line assembly is part not part of the c grammar...
Well, the asm() keyword is mentioned in section J.5 "Common Extensions".

Personally though, I would go for __asm() or __asm__(), symbols which are "reserved for the implementation", whereas asm() is not strictly conforming.

As ASM targets the machine directly, i.e. there is nothing "intermediate" about it, whereas C targets an abstraction. You will need hints for the compiler to avoid breakage at the boundary (registers clobbered by the ASM), and a way to feed data from the abstraction to the machine and back.

Actually, I think the GCC __asm__ extension does a reasonable job.

If you can get your compiler to auto-deduce e.g. the clobbered registers, even better. But, to be quite honest, I don't really see the use-case here. The need for inline ASM is not that big...
Every good solution is obvious once you've found it.
Post Reply