OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:40 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: In-line assembly in an Abstract Syntax Tree
PostPosted: Fri Jan 05, 2018 7:23 pm 
Offline
Member
Member

Joined: Sat Feb 27, 2010 8:55 pm
Posts: 147
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?


Top
 Profile  
 
 Post subject: Re: In-line assembly in an Abstract Syntax Tree
PostPosted: Sat Jan 06, 2018 2:56 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
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.


Top
 Profile  
 
 Post subject: Re: In-line assembly in an Abstract Syntax Tree
PostPosted: Wed Jan 10, 2018 4:55 pm 
Offline
Member
Member
User avatar

Joined: Thu Jun 04, 2009 11:12 pm
Posts: 281
Quote:
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


Top
 Profile  
 
 Post subject: Re: In-line assembly in an Abstract Syntax Tree
PostPosted: Thu Jan 11, 2018 6:22 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group