Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / January 2006

Tip: Looking for answers? Try searching our database.

Creating sub-procedures in MSIL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nicolas - 23 Jan 2006 23:24 GMT
Hello,

I am making a small compiler, targeting the .NET platform. I use C# to
create the AST and System.Reflection.* to create assemblies. It works
fine for the moment, but I have a question about IL :
I'd like to be able to get the same behavior as JMP and CALL in IA32 ASM.
JMP jumps directly to a block of code, while CALL allows a RET to return
, creating a sub-procedure.
I want to implement the same feature in my compiler. I can already
create labels and jump to them, but is there a way to return at some
point, and execute the instruction after the "CALL" ?
Is there a way to store the IL "Labels" in a local list, and then jump
to the label on top of this list when RET-FROM-SUB is found ? It seems
to me that the Opcodes.B* instructions do not allow a target value to be
taken from a variable, they must be hard-coded.

Thanks for your help,

Nicolas.
Tim C - 24 Jan 2006 08:06 GMT
Does the IL "switch" instruction do what you want?

In any case, unlike asm, with IL any jump target must be defined at compile
time - you can't arbitrarily jump to just any IL instruction based on a
variable, otherwise there would be no guarantee of the stack.  But you can
create a pre-defined jump table using the "switch" instruction.

> Hello,
>
[quoted text clipped - 15 lines]
>
> Nicolas.
Nicolas - 24 Jan 2006 16:24 GMT
> Does the IL "switch" instruction do what you want?
>
> In any case, unlike asm, with IL any jump target must be defined at compile
> time - you can't arbitrarily jump to just any IL instruction based on a
> variable, otherwise there would be no guarantee of the stack.  But you can
> create a pre-defined jump table using the "switch" instruction.

Hello Tim,

Thank you for your reply. I guess a pre-defined jump table would do it,
but that's a bit of a hack. For example, to implement this pseudo-asm :

print "start"
push 0

label1:
print "sub proc"
jump to next if top of stack == 0
ret

next:
print "after"
call label1
print "end"
ret

(which would print start,sub proc,after,sub proc,end), I would have, in
the code generation for the "ret" after the conditional jump, to store
"the line number" to jump to, and then to emit the switch itself.
That means storing at generation time n "after-call" labels (one for
each "call") and generating a n-possibilities switch for each ret-from-sub.
Optimization could help reducing this number, by analyzing the code paths.
It is possible, but I'm just wondering how big the overhead will be.

Thank you for your help :)

Nicolas.
Tim C - 25 Jan 2006 04:28 GMT
A couple things in asm won't translate to MSIL:
The IL "call" instruction can only target the beginning of a method
The IL "br__" instructions (jumping) can only jump inside the same method

Translating the asm to IL looks something like:

ldstr "start"
call print
ldc.i4.0

IL_label1:
ldstr "sub proc"
call print
dup
brfalse IL_next
ret

IL_next:
ldstr "after"
call print
; call IL_label1 ; <-- no such instruction; can't call to within method
ldstr "end"
call print
ret

Is there a way to design it such that you only call to methods and only jump
within a method?
-Tim

>> Does the IL "switch" instruction do what you want?
>>
[quoted text clipped - 34 lines]
>
> Nicolas.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.