473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compilers & stack machines

Aside from comp.compilers, is there any other forum, newsgroup or medium
where I can post questions concerning the development of a byte code
compiler & virtual stack machine?

--

Jul 22 '05 #1
2 2453
What I understand is that I can allocate strings in two ways, either on
the stack or on the heap.

By allocating a string on the stack, the string's fixed size has to be
aligned by the size of the stack. For example, a 16-bit wide stack
would mean that a 30 byte string would need 8 slots on the stack.
Now, lets saying I wanted to define a variable of fixed size 30,
assign a string value to it and then print it. The equivalent C code
would be:

char szText[30];
strcpy(szText, "Hello World");
printf(szText);

How should I handle pushing the string onto the stack and referencing
it when it comes time to print it?

The other alternative is heap allocation. In this case, only 1 slot is
required because it is going to reference the variable that is a memory
address pointer on the heap. Again, how would the stack & referencing
of the string be handled when it comes time to print it?

I'm thinking that in both scenarios, the store(strcpy) and print(printf)
operations will need to be slighly different as one is working with a
pointer to heap memory and the other is working with a value that is
referenced directly on the stack, right?

--
"news.tkdsoftware.com" <ch***@noreply.nospam.tkdsoftware.com> wrote in
message news:wc****************************@tkdsoftware.co m...
Aside from comp.compilers, is there any other forum, newsgroup or medium
where I can post questions concerning the development of a byte code
compiler & virtual stack machine?

--

Jul 22 '05 #2
hi,

you have two problems really - in the first problem it is how to actually
allocate the data. A way commonly used for stack variables is to have the
'compiler' keep a running total of how much stack space is being used by the
function as it is compiling (or by allocating the space in a separate phase
after the function compilation), along with offsets from some base registers
(such as EBP) where each variable is stored. So ALL the space used by the
function is allocated up front, and the compiler decides on offsets from a
base register that will be used to index the variables.

with a little more work, you could have it allocate things dynamically on the
stack in different memory scopes and then index off say the stack pointer
itself, this becomes a matter of how to keep track of what is going on at
compile time. Doing that could 'move' the offsets of other variables that
were previously declared, depending on how you do it. It would mainly be a
matter of appropriate bookeeping at compile time to do this, but it
complicates things somewhat and will especially make writing a debugger
harder.

The other problem is really determining whether to take an address literally
or to do a pointer indirection on that address to find the real address -
again that is a bookeeping issue and depends on the structure of the source
language. In 'c' an array such as char myarray[10] always consideres myarray
to be a literal address... but an array defined as a pointer such as char
*myarray assumes the pointer has to be 'indirected' from the actual address of
the myarray variable (e.g. by the equivalent of loading the destination
address value from the memory location of the variable). The compiler is
designed to handle both situations because this is what the semantics of the C
language call for. If creating a new language, you really need to just make
some rules about what the semantics are in terms of what is literal and what
is a pointer, then design around them.

in general, the library functions in the C language such as strcpy and printf
are the same all the time, and such concerns as to what the actual address of
the data is are handled in the compiled code prior to calling the specified
function (by indirecting as appropriate).

David

"news.tkdsoftware.com" wrote:
What I understand is that I can allocate strings in two ways, either on
the stack or on the heap.

By allocating a string on the stack, the string's fixed size has to be
aligned by the size of the stack. For example, a 16-bit wide stack
would mean that a 30 byte string would need 8 slots on the stack.
Now, lets saying I wanted to define a variable of fixed size 30,
assign a string value to it and then print it. The equivalent C code
would be:

char szText[30];
strcpy(szText, "Hello World");
printf(szText);

How should I handle pushing the string onto the stack and referencing
it when it comes time to print it?

The other alternative is heap allocation. In this case, only 1 slot is
required because it is going to reference the variable that is a memory
address pointer on the heap. Again, how would the stack & referencing
of the string be handled when it comes time to print it?

I'm thinking that in both scenarios, the store(strcpy) and print(printf)
operations will need to be slighly different as one is working with a
pointer to heap memory and the other is working with a value that is
referenced directly on the stack, right?

--

"news.tkdsoftware.com" <ch***@noreply.nospam.tkdsoftware.com> wrote in
message news:wc****************************@tkdsoftware.co m...
Aside from comp.compilers, is there any other forum, newsgroup or medium
where I can post questions concerning the development of a byte code
compiler & virtual stack machine?

--

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
7
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
5
by: kidzero | last post by:
Hello, I have a problem running my application on windows 98. My application is written using C# . NET On some machines with windows 98 program works normaly and on some machines with windows...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
20
by: deepak | last post by:
Hi All, In C I heard the stack grows from top to bottom and heap from bottom to top. Is it compiler depend?
0
by: JosAH | last post by:
Greetings, Introduction This part of the article is one week late; I apologize for that; my excuse is: bizzy, bizzy, bizzy; I attended a nice course and I had to lecture a bit and there...
4
by: 9lives.9lives | last post by:
Hello, everyone! I am trying to optimize some code, but I don't think I'm doing what I think I'm doing. I profiled my code and found that the overloaded operator of my monomial class did...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
20
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
There are a few guarantees I exploit in the C Standard. For instance, I might write (unsigned)-1 to get the maximum value for an unsigned integer. Also, I might rely on things such as: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.