6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Tue May 14, 2024 3:44 am

All times are UTC




Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Thu May 07, 2015 10:16 am 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
What about using big-endian on the return (machine) stack?
Code:
; >R
tor
    lda tos+1
    pha
    lda tos
    pha
    jmp pops

; R>
rfrom
    pla
    sta n
    pla
    tay
    lda n
    jmp pushya      ; [19] clocks
becomes:
Code:
tor
    lda tos
    pha
    lda tos+1
    pha
    jmp pops     ; nothing gained here

rfrom
    pla
    tay
    pla
    jmp pushya    ; four bytes smaller, only [13] clocks
Things that wind up on the return stack because of BRK, JSR, and interrupts would still be in their natural little-endian order, but it is possible/necessary for the programmer to know what's going on in those cases.


Top
 Profile  
Reply with quote  
PostPosted: Thu May 07, 2015 1:02 pm 
Offline

Joined: Sat Mar 27, 2010 7:50 pm
Posts: 149
Location: Chexbres, VD, Switzerland
Forth requires separate data and return stack, for other interpreted language, having everything on the hardware stack would make sense, as soon as you mostly work with the top-of-stack and rarely work with the inner items.

To be honnest, if you need a stack of 16-bit items and separate data/return stacks, then I'd use the hardware stack as a return stack, and either split-stack or zero page for data stack. If ~64 items is enough, then by all mean use ZP and don't split so that you can use (ZP,X) for addressing memory, and if you need between 64 and 256 items depth, then split stack is perfect (you'll have to copy to ZP temporaries for a couple of instructions).

I am just starting to learn forth, but it would seem to me that using VRAIABLE too much isn't the philosophy of the language and should be normally avoided, just like global variables in more conventional languages. If VARIABLE is in the top 5 of the most used words, very likely it was the result of a conversion from another programing language.


Top
 Profile  
Reply with quote  
PostPosted: Thu May 07, 2015 10:10 pm 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
As somebody who hasn't used the language all that much, I'm always curious how complex Forth code gets for heap-oriented programs and algorithms. Data lifetime is not necessarily tied to its creating function lifetime, and lots of code needs to reference contextual data that isn't necessarily passed in to it.

I know that in languages like the JVM (which has a stack-oriented bytecode), the data stack is used very intermediately. Values are stored to & retrieved from local variables often, instead of juggling them around the data stack. Pretty much just the current expression evaluation is kept on the stack. Of course, it's expected that the compiler will optimize this when translating to its register-based target, but it also does keep the stack based code clean and straightforward.

While there's a lot of compacting tricks you can do with processing immediate data in Forth via keeping it all on the stack, at some level of complexity you need to be able to handle longer lived data structures.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 6:35 am 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
@White Flame, I don't understand your point.

Just about every HLL in the world (except BASIC) uses a stack to temporarily store local variables and these variables are given up when the subroutine involved exits. Forth has a VARIABLE word for long term data storage.

If you want encapsulated data (which other words can't access) then here is a neat trick I use:
Code:
HERE <size> ALLOT
: word ........ [ DUP ] LITERAL <do stuff with private variable> ........ ;
DROP

Of course, you can have more than one word access that private data space if you wish. With Forth, you make the rules.


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 9:17 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
Right, in effect you have to invent your own memory management from scratch. So much that is seen about Forth is on manipulating the data stack, defining variables, or dictionary states that only work with LIFO alloc/dealloc semantics.

In writing complex editors or any form of analyzers that deal with modifying large graph structures (ie, interconnected data structures that might come and go (consider an optimizing whole-program compiler)), the data doesn't live on the stack, nor in individual variables, and their allocations/deallocations happen in any order as opposed to LIFO.

Obviously Chuck Moore has built all sorts of complex tooling on top of Forth. I wonder if he has his own alloc/dealloc heap manager, or if he works purely with preallocated object pools, or what. Because variables and the dictionary stack don't provide the direct means to do such things, and it's a tough problem to solve well.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 11:40 am 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
If you want a heap manager then LOAD one in. If your Forth implementation doesn't have one then I posted some code for one a few months back. (http://forum.6502.org/viewtopic.php?p=36663#p36663). You don't need all the bells and whistles included in an application and taking up megabytes of space whether they are needed or not before the application can be considered useful.

I get that Forth is not everybody's cup of tea. Reading pre-written code can be especially daunting. However making a whole bunch of strawman arguments against Forth isn't doing you any good.


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 12:43 pm 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
I'm not making strawman arguments, I'm legitimately asking how these things are done. I've done a decent amount of stack-based intermediate formats inside compilers, had some huge PostScript jobs in the past, etc, but I'm not really a Forth-head. I haven't heard of malloc-style heap managers mentioned around Forth before (though haven't really searched around either), so I didn't know if there were common libraries for things like this, or if people using Forth tend to back away from that and use more static allocation (pooling and such).

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 2:53 pm 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
White Flame wrote:
I haven't heard of malloc-style heap managers mentioned around Forth before (though haven't really searched around either), so I didn't know if there were common libraries for things like this, or if people using Forth tend to back away from that and use more static allocation (pooling and such).

It would have been better to post questions about memory management in Forth and deciding if you like the answers instead of making unresearched remarks as in your previous post.

I wrote a heap manager for Forth because I was curious about linked-lists and binary trees etc. (Mind you, I have also experimented with these structures in BASIC using parallel arrays). I could probably have found something on-line (maybe gforth has one) but I usually find it less hassley (and more satisfying) to write my own. That is the power of Forth.


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 3:21 pm 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
Quote:
It would have been better to post questions about memory management in Forth and deciding if you like the answers instead of making unresearched remarks as in your previous post.

Not to belabor the point, but that's what I did. Including saying what I've already seen that didn't cover the answer.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 7:57 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8182
Location: Midwestern USA
theGSman wrote:
I get that Forth is not everybody's cup of tea. Reading pre-written code can be especially daunting. However making a whole bunch of strawman arguments against Forth isn't doing you any good.

I don't think White Flame was making a strawman argument—his questions are interesting. I've often wondered about how one would avoid "data volatility" (for lack of a better term) in Forth. I'm not a Forth user, but as a matter of course, I tend to be interested in how such environments work.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sat May 16, 2015 9:37 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1929
Location: Sacramento, CA, USA
BigDumbDinosaur wrote:
I don't think White Flame was making a strawman argument—his questions are interesting ...

Yeah, White Flame has a quirky sense of humor and a posting style that is a bit "dryer" than many others here, so his tone sometimes gets misinterpreted. I feel quite comfortable in assuming that he wasn't trying to offend. I'll refrain from giving examples of forum members who are genuinely offensive, because that's not my style, but White Flame definitely doesn't belong on that list (at least not on my personal list).

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Sun May 17, 2015 2:03 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
I will concur that my natural communication style is abnormally dry. I get that a lot IRL as well.

It generally comes from being overly introspective and dealing heavily with AI design and conceptual communication. But at the end of the day, I actually prefer being precise to the alternatives.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sun May 17, 2015 7:58 am 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
White Flame wrote:
As somebody who hasn't used the language all that much, I'm always curious how complex Forth code gets for heap-oriented programs and algorithms.

Had I read that opening statement correctly the posts that followed would have been of a different nature so, my bad. :oops:

The short answer is, "no worse than with C" (aside from the usual caveats about Forth syntax).

You might have to write your own MALLOC and FREE words but K&R's Ansi C shows you how. As a matter of fact, there are a number of standard functions in that book that are worthy of being coded in Forth.


Top
 Profile  
Reply with quote  
PostPosted: Sun May 17, 2015 4:07 pm 
Offline

Joined: Sun Jul 28, 2013 12:59 am
Posts: 235
theGSman wrote:
You might have to write your own MALLOC and FREE words but K&R's Ansi C shows you how.

And not even that, if you're using an ANS forth that provides the Memory-Allocation word set (ALLOCATE, FREE, and RESIZE).


Top
 Profile  
Reply with quote  
PostPosted: Mon May 18, 2015 11:57 pm 
Offline

Joined: Tue Jan 07, 2014 8:40 am
Posts: 91
Hmmm. I co-wrote a paper on the subject of memory allocation many years ago for the Journal of Forth Application and Research, but upon checking I see that I never put it on-line. Some of that work made it into Section C of my 1989 Master's dissertation. (Now, where did I put that program listing?) It was nothing particularly sophisticated, but it does serve as an existence proof. :)

_________________
Because there are never enough Forth implementations: http://www.camelforth.com


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 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: