MOVING FORTH.docx
- 文档编号:30210721
- 上传时间:2023-08-07
- 格式:DOCX
- 页数:83
- 大小:74.96KB
MOVING FORTH.docx
《MOVING FORTH.docx》由会员分享,可在线阅读,更多相关《MOVING FORTH.docx(83页珍藏版)》请在冰豆网上搜索。
MOVINGFORTH
MOVINGFORTH
Part1:
DesignDecisionsintheForthKernel
byBradRodriguez
ThisarticlefirstappearedinTheComputerJournal#59(January/February1993).
INTRODUCTION
EveryoneintheForthcommunitytalksabouthoweasyitistoportForthtoanewCPU.Butlikemany"easy"and"obvious"tasks,notmuchiswrittenonhowtodoit!
So,whenBillKiblersuggestedthistopicforanarticle,IdecidedtobreakwiththegreatoraltraditionofForthwrights,anddocumenttheprocessinblackandwhite.
OverthecourseofthesearticlesIwilldevelopForthsforthe6809,8051,andZ80.I'mdoingthe6809toillustrateaneasyandconventionalForthmodel;plus,I'vealreadypublisheda6809assembler[ROD91,ROD92],andI'llbeneedinga6809ForthforfutureTCJprojects.I'mdoingthe8051ForthforaUniversityproject,butitalsoillustratessomeratherdifferentdesigndecisions.TheZ80ForthisforalltheCP/MreadersofTCJ,andforsomefriendswithTRS-80sgatheringdust.
THEESSENTIALHARDWARE
YoumustchooseaCPU.IwillnotdelveintothemeritsofoneCPUoveranotherforForth,sinceaCPUchoiceisusuallyforceduponyoubyotherconsiderations.Besides,theobjectofthisarticleistoshowhowtomoveForthtoanyCPU.
Youcanexpecttheusual16-bitForthkernel(seebelow)tooccupyabout8Kbytesofprogramspace.ForafullkernelthatcancompileForthdefinitions,youshouldallowaminimumof1KbyteofRAM.TouseForth'sblock-managementsystemfordiskstorage,youshouldadd3Kbytesormoreforbuffers.Fora32-bitForthmodel,doublethesenumbers.
ThesearetheminimumstogetaForthkernelupandrunning.Torunanapplicationonyourhardware,youshouldincreasePROMandRAMsizestosuit.
16OR32BIT?
ThewordsizeusedbyForthisnotnecessarilythesameasthatoftheCPU.ThesmallestpracticalForthisa16-bitmodel;i.e.,onewhichuses16-bitintegersand16-bitaddresses.TheForthcommunitycallsthisthe"cell"size,since"word"referstoaForthdefinition.
8-bitCPUsalmostinvariablysupport16-bitForths.Thisusuallyrequiresexplicitcodingofdouble-bytearithmetic,althoughsome8-bitCPUsdohaveafew16-bitoperations.
16-bitCPUscommonlyrun16-bitForths,althoughthesamedouble-precisiontechniquescanbeusedtowritea32-bitForthona16-bitCPU.Atleastone32-bitForthhasbeenwrittenforthe8086/8088.
32-bitCPUsnormallyrun32-bitForths.AsmallerForthmodelrarelysavescodelengthorprocessortime.However,Iknowofatleastone16-bitForthwrittenforthe68000.Thisdoesshrinkapplicationcodesizebyafactoroftwo,sincehigh-levelForthdefinitionsbecomeastringof16-bitaddressesratherthanastringof32-bitaddresses.(Thiswillbecomeevidentshortly.)Most68000s,though,haveplentyofRAM.
Alloftheexamplesdescribedinthisarticleare16-bitForthsrunningon8-bitCPUs.
THETHREADINGTECHNIQUE
"Threadedcode"isthehallmarkofForth.AForth"thread"isjustalistofaddressesofroutinestobeexecuted.Youcanthinkofthisasalistofsubroutinecalls,withtheCALLinstructionsremoved.Overtheyearsmanythreadingvariationshavebeendevised,andwhichoneisbestdependsupontheCPUandtheapplication.Tomakeadecision,youneedtounderstandhowtheywork,andtheirtradeoffs.
IndirectThreadedCode(ITC)
ThisistheclassicalForththreadingtechnique,usedinfig-ForthandF83,anddescribedinmostbooksonForth.Alltheotherthreadingschemesare"improvements"onthis,soyouneedtounderstandITCtoappreciatetheothers.
Let'slookatthedefinitionofaForthwordSQUARE:
:
SQUAREDUP*;
InatypicalITCForththiswouldappearinmemoryasshowninFigure1.(Theheaderwillbediscussedinafuturearticle;itholdshousekeepinginformationusedforcompilation,andisn'tinvolvedinthreading.)
AssumeSQUAREisencounteredwhileexecutingsomeotherForthword.Forth'sInterpreterPointer(IP)willbepointingtoacellinmemory--containedwithinthat"other"word--whichcontainstheaddressofthewordSQUARE.(Tobeprecise,thatcellcontainstheaddressofSQUARE'sCodeField.)Theinterpreterfetchesthataddress,andthenusesittofetchthecontentsofSQUARE'sCodeField.Thesecontentsareyetanotheraddress--theaddressofamachinelanguagesubroutinewhichperformsthewordSQUARE.Inpseudo-code,thisis:
(IP)->WfetchmemorypointedbyIPinto"W"register
...WnowholdsaddressoftheCodeField
IP+2->IPadvanceIP,justlikeaprogramcounter
(assuming2-byteaddressesinthethread)
(W)->XfetchmemorypointedbyWinto"X"register
...Xnowholdsaddressofthemachinecode
JP(X)jumptotheaddressintheXregister
Thisillustratesanimportantbutrarely-elucidatedprinciple:
theaddressoftheForthwordjustenterediskeptinW.CODEwordsdon'tneedthisinformation,butallotherkindsofForthwordsdo.
IfSQUAREwerewritteninmachinecode,thiswouldbetheendofthestory:
thatbitofmachinecodewouldbeexecuted,andthenjumpbacktotheForthinterpreter--which,sinceIPwasincremented,ispointingtothenextwordtobeexecuted.ThisiswhytheForthinterpreterisusuallycalledNEXT.
But,SQUAREisahigh-level"colon"definition--itholdsa"thread",alistofaddresses.Inordertoperformthisdefinition,theForthinterpretermustbere-startedatanewlocation:
theParameterFieldofSQUARE.Ofcourse,theinterpreter'soldlocationmustbesaved,toresumethe"other"ForthwordonceSQUAREisfinished.Thisisjustlikeasubroutinecall!
ThemachinelanguageactionofSQUAREissimplytopushtheoldIP,setIPtoanewlocation,runtheinterpreter,andwhenSQUAREisdonepoptheIP.(Asyoucansee,theIPisthe"programcounter"ofhigh-levelForth.)ThisiscalledDOCOLONorENTERinvariousForths:
PUSHIPontothe"returnaddressstack"
W+2->IPWstillpointstotheCodeField,soW+2is
theaddressoftheBody!
(Assuminga2-byte
address--otherForthsmaybedifferent.)
JUMPtointerpreter("NEXT")
Thisidenticalcodefragmentisusedbyallhigh-level(i.e.,threaded)Forthdefinitions!
That'swhyapointertothiscodefragment,notthefragmentitself,isincludedintheForthdefinition.Overhundredsofdefinitions,thesavingsaddup!
Andthisiswhyit'scalledIndirectthreading.
The"returnfromsubroutine"isthewordEXIT,whichgetscompiledwhenForthsees';'.(SomeForthscallit;SinsteadofEXIT.)EXITjustexecutesamachinelanguageroutinewhichdoesthefollowing:
POPIPfromthe"returnaddressstack"
JUMPtointerpreter
WalkthroughacoupleofnestedForthdefinitions,justtoassureyourselfthatthisworks.
NotethecharacteristicsofITC:
everyForthwordhasaone-cellCodeField.Colondefinitionscompileonecellforeachwordusedinthedefinition.AndtheForthinterpretermustactuallyperformadoubleindirectiontogettheaddressofthenextmachinecodetorun(firstthroughIP,thenthroughW).
ITCisneitherthesmallestnorthefastestthreadingtechnique.Itmaybethesimplest;althoughDTC(describednext)isreallynomorecomplex.SowhyaresomanyForthsindirect-threaded?
MainlybecausepreviousForths,usedasmodels,wereindirect-threaded.Thesedays,DTCisbecomingmorepopular.
SowhenshouldITCbeused?
Ofthevarioustechniques,ITCproducesthecleanestandmostelegantdefinitions--nothingbutaddresses.Ifyou'reattunedtosuchconsiderations,ITCmayappealtoyou.Ifyourcodefiddlesaroundwiththeinsidesofdefinitions,thesimplicityanduniformityoftheITCrepresentationmayenhanceportability.ITCistheclassicalForthmodel,soitmaybepreferredforeducation.Finally,onCPUslackingasubroutinecallinstruction--suchasthe1802--ITCisoftenmoreefficientthanDTC.
DirectThreadedCode(DTC)
DirectThreadedCodediffersfromITCinonlyonerespect:
insteadoftheCodeFieldcontainingtheaddressofsomemachinecode,theCodeFieldcontainsactualmachinecodeitself.
I'mnotsayingthatthecompletecodeforENTERiscontainedineachandeverycolondefinition!
In"high-level"Forthwords,theCodeFieldwillcontainasubroutinecall,asshowninFigure2.Colondefinitions,forinstance,willcontainacalltotheENTERroutine.
TheNEXTpseudo-codefordirectthreadingissimply:
(IP)->WfetchmemorypointedbyIPinto"W"register
IP+2->IPadvanceIP(assuming2-byteaddresses)
JP(W)jumptotheaddressintheWregister
Thisgainsspeed:
theinterpreternowperformsonlyasingleindirection.OntheZ80thisreducestheNEXTroutine--themost-usedcodefragmentintheForthkernel--fromeleveninstructionstoseven!
Thiscostsspace:
everyhigh-leveldefinitioninaZ80Forth(forexample)isnowonebytelonger,sincea2-byteaddresshasbeenreplacedbya3-bytecall.Butthisisnotuniversallytrue.A32-bit68000Forthmayreplacea4-byteaddresswitha4-byteBSRinstruction,fornonetloss.AndontheZilogSuper8,whichhasmachineinstructionsforDTCForth,the2-byteaddressisreplacedbya1-byteENTERinstruction,makingaDTCForthsmallerontheSuper8!
Ofcourse,DTCCODEdefinitionsaretwobytesshorter,sincetheynolongerneedapointeratall!
Iusedtothinkthathigh-leveldefinitionsinDTCForthsrequiredtheuseofasubroutinecallintheCodeField.FrankSergeant'sPygmyForth[SER90]demonstratesthatasimplejumpcanbeusedjustaseasily,andwillusuallybefaster.
GuyKellyhascompiledasuperbreviewofForthimplementationsfortheIBMPC[KEL92],whichIstronglyrecommendtoallForthkernelwriters.Ofthe19Forthshestudied,10usedDTC,7usedITC,and2usedsubroutinethreading(discussednext).Ire
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MOVING FORTH
![提示](https://static.bdocx.com/images/bang_tan.gif)