2008/10/23
from
葉健欣 老師 的
Google document 筆記
安裝:
這是在 windows 安裝 GTK+ 開發環境的指引。<== GTK+ is a GUI for Linux also Win32 GTK 學習筆記 Reference-2 Reference-3 -Hcchen5600 2008/10/24 下午 7:00
1) Visual C++ studio ( 我是用 2008 版,其他版本應該也可以) <=== Visual Studio 2005 , it works fine too. 詳細步驟下面有說明。 -Hcchen5600 2008/10/24 下午 6:38
2) GTK+ for windows 開發環境包
到 http://downloads.sourceforge.net/gladewin32/ 下載 gtk+-win32-devel <=== Focus on Forth, we skip this, -Hcchen5600 2008/10/24 下午 6:40
3) python (因為轉換 glade 到 GtkBuilder 的script: gtk-builder-convert 要用到)
到這裡下載 http://www.python.org/download/releases/2.5.2/ python-2.5.2.msi <== Focus on Forth, we skip this -Hcchen5600 2008/10/24 下午 6:42
源碼:
1) http://svn.ksana.tw/svn-repos/ksanagtk <== we skip this -Hcchen5600 2008/10/24 下午 7:03
Forth Engine
2) http://svn.ksana.tw/svn-repos/ficl
<=== ficl 的東西,將來我們的 EFI forth 若要用 ficl 可以來參考 -Hcchen5600 2008/10/24 下午 7:06
這兩個源碼包要同一層目錄,也就是說 如果 d:\xxxx\ksanagtk ,那 ficl 就要在d:\xxxx\ficl <== 看到這句話,心中浮起一個想法:葉老師的 KsanaVM 是教學用的,真正上場的還是用專業級的 ficl ? Yes ! see http://tutor.ksana.tw/ksanagtk/ -Hcchen5600 2008/10/24 下午 7:38
教學檔:
http://tutor.ksana.tw/ksanagtk/ <=== 重要!說明如何在 GTK+ 裡引入 ficl ,看完 KsanaVM 之後即可來攻這個。 -Hcchen5600 2008/10/24 下午 7:55
GTK相關參考文件: 值得抽空來看看,我本來以為這是 Linux 專屬得,其實不然? 好像 Windows 也有? -Hcchen5600 2008/10/31 10:47
http://caterpillar.onlyfun.net/Gossip/GTKGossip/GTKGossip.html GTK 學習筆記
http://library.gnome.org/devel/gtk-tutorial/stable/ GTK tutorial
http://www.kplug.org/glade_tutorial/glade2_tutorial/glade2_introduction.html Graphical Interface Development with Glade2
http://www.micahcarrick.com/12-24-2007/gtk-glade-tutorial-part-1.html GTK+ and Glade3 GUI Programming Tutorial
Forth 相關參考文件:
活體解剖:剎那極簡虛擬機 <== KsanaVM is actually a Forth system, up to you if you like to call it a VM (Virtual Machine) -Hcchen5600 2008/10/23 下午 5:24
Source code在SVN http://svn.ksana.tw/svn-repos/ksanavm/
<=== KsanaVM Forth source code -Hcchen5600 2008/10/24 下午 8:05
剎那極簡虛擬機簡介
授權方式: 程式 GPL 3.0 教學檔 GFDL
<=== 找法務幫忙評估法律顧慮 [ ] -Hcchen5600 2008/10/24 下午 8:07 http://www.nhu.edu.tw/~society/e-j/39/39-01.htm
程式撰寫人及講課者:葉健欣 yapcheahshen@gmail.com
了解葉老師的工作
本教學以
Visual C++ Express 2008錄製,其他開發環境使用者請自行設定(開新專案,把所有 .c 檔加入專案即可)
如下,我用 Visual Studio 2005 C++, 本來還想用 Turbo C 2.01 因為 vm.h 的宣告裡用到很多 C++ 的特性而作罷。-Hcchen5600 2008/10/24 上午 8:32 。
第一次請順序看過,不清楚的單元再重看。
Google ksanaVm討論區
下載第1~17課全集打包 (63MB) < ==== downlaod, use the latest one ~Rev6.zip
| Starting up 啟動 2007.12.20 |
| setup |
2007.12.20 |
install and start up 安裝與啟動
|
| fileintro |
2007.12.20 |
File and environment Introduction 檔案及環境解說
|
| vcintro |
|
VC++ IDE intro, 開發環境簡介(不熟VC++請看)
|
| Part1 : The Interpreter 直譯器 下載所需檔案 8KB ZIP (1~3課) |
| lesson1 |
2007.12.20 |
Processing tokens 處理符素
|
| lesson2 |
2007.12.20 |
Working with stack 堆疊
|
| lesson3_1 |
2007.12.20 |
The execution vector 執行單元 (函式介紹)
|
| lesson3_2 |
2007.12.20 |
The execution vector 執行單元 (追縱)
|
| Part2 : The Compiler 編譯器 下載所需檔案 11KB ZIP (4~8課) |
| Lesson4 |
2007.12.21 |
Dictionary 字典
|
| Lesson5_1 |
2007.12.21 |
Invoke a Call and Return 呼叫及返回 (介紹)
|
| Lesson5_2 |
2007.12.22 |
Invoke a Call and Return 呼叫及返回 (追縱)
|
| Lesson6 |
2007.12.22 |
Nested Call 巢狀呼叫
|
| Lesson7 |
2007.12.24 |
Compiling literals 編入數值
|
| Lesson8 |
2007.12.24 |
Colon Defination 定義新字
|
| Part3 : Program Structure 程序結構 下載所需檔案 21KB ZIP(9~12課) |
| Lesson9 |
2008.1.21 |
String compiler 字串編譯
|
| Lesson10 |
2008.1.21 |
if-then-else 改變程式流程(1)
|
| Lesson11 |
2008.1.22 |
do-loop 改變程式流程(2)
|
| Lesson12 |
2008.1.22 |
Variable 變數
|
| Part4 : Developing Application 開發應用程序 下載所需檔案 24KB ZIP(13~17課) |
| Lesson13 |
2008.1.22 |
Localization 中文化
|
| Lesson14 |
2008.1.22 |
Calling System API 調用系統函式
|
| Lesson15 |
2008.1.23 |
loading external source file 載入源碼
|
| Lesson16 |
2008.1.23 |
Recursion 遞迴呼叫:Factorial 階乘
|
| Lesson17 |
2008.1.23 |
Array 陣列:Prime Number 質數
|
| Part5 : Advance Topic 進階主題
|
| Lesson20 |
|
Create Does
|
| Lesson21 |
|
80x86 Disassembler
|
| Lesson22 |
|
80x86 Assembler
|
| Lesson23 |
|
Meta Compiling
|
| Lesson24 |
|
Peek hole optimizer
|
2008/10/23
16:40
Start the practice : 我第一次用 Visual Studio 2005 (以前都用 Turbo C 2.01), 心懷恐懼。反正也摸出來了 . . .
1. run Microsoft Visual Studio 2005 's IDE ==> "D:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe"
2. File > New project
Project type ==> Visual C++ > Win32
Templates ==> Win32 console application
3. Name = Ksana_lesson1 <================================== 隨便取個名字
Specify a folder, click ok
4. On Win32 application Wizard
Click next (not finish) to to Application settings
application type ==> console application
Additional options ==> Empty project <============== because I will port everything from teacher.
5. Click Finish
Now I got a project in Visual Studio 2005.
6. Follow the steps shown above to bring in all the ~.c and ~.h files from teacher's package.
帶進老師的檔案之後,Solution Explorer area 自動變成這樣,
7. Build
17:20
Now, let's see the movie ..... D:\Learnings\ksanavm\setup.htm ==> watch the elearning video setup.swf. I don't know what's TortoiseSVN. It seems like a software release management system. Forget it, it doesn't bother my progress at all. I have passed the setup anyway.
19:27
Next elearning video ==> D:/Learnings/ksanavm/fileintro.htm
Introductions of each file. .c and .h files.
2008/10/24
21:50
Lesson 1 ~ 3 , my study memo
void main()
{
char cmd [1024]; // buffer to hold input command
KsanaVm *vm; // the instance of virtual machine
vm=KVMCreate(); // create the virtual machine
memset(cmd,0,sizeof(cmd));
puts("Tutorial of Ksana Minimal Virtual Machine"); <==== 客人對外面打什麼廣告,我們不在乎。
puts("http://tutor.ksana.tw");
揣摩 main(), lesson1~3, vm, 三者的關係。
main() 是講 forth 語言的外國客人; lesson1~n 等於我們,是承包商; vm 是 solution provider 上游廠商。
我們這個 vm 廠商只有執行能力,卻聽不懂人話。我們身為承包商,負責把客人用 forth 語言交辦的工作逐一指派給廠商家裡正確的部門執行。
while (1) { <=== 這個 loop 是 lesson1~n(我們)與 main(外國客人)之間的整筆生意。客人喊停我們才結束工作。
printf("ok>"); // the command prompt
gets_s(cmd, sizeof(cmd)); // get the command from console <==== 向客人索取命令
KVMSetTib(vm,cmd); // set it as Vm's terminal input buffer <==== VM 的 functions 一律以 KVM~ 開頭。這個 KVMSetTib() 等於是廠商端的業務代表。客人的命令我們連看都不看一眼,直接 pass 給廠商的 sales 請他先帶回去,聽候我們下一步指示。
//lesson1(vm); <== 第一課先 LAB pilot run 測驗廠商的 KVMNextToken() 能不能正確為客人的輸入斷句? 請廠商逐字複誦客人的話。我們聽到複誦 'bye' 時就知道該結束工作了。這樣就算 pass。
lesson2(vm); <== 第二課多試了一些 vm 裡的 KVM~ functions. 這個 vm 廠商只有執行能力,卻聽不懂人話。我們身為承包商,負責把客人用 forth 語言交辦的工作逐一指派給廠商家裡正確的部門執行。
//lesson3(vm); <== 第三課把 VM 裡的 KVM~ functions 編成一串 array 用 KVMFindWord(KsanaVm *vm,char *name) 進去找。這比 f language 的一堆 if() 漂亮多了。我會認為,KsanaWord vm->vectors[] 這樣一個 array 的內容都是 constants, 何必花 run time 來 initialize? 雖然這個 init 動作只做一次,本身的成本又很低,無需吹毛求貲。可以簡單唐塞說:因為 vm 本身是 run time 才 created 出來的。 可是,這更無法解釋:1)何必把這個 KsanaWord constant array 放進 vm 裡面? 2)即使要放在 vm 裡面,為何不在 KVMCreate() 裡做這個 addbasicword(vm) init 動作? 我試著回答第二個疑問:設想 vm.c 裡有一百萬個 KVM~xxx~word(),讓 KVMCreate() 來 init 的話,他只能全部加進 KsanaWord vm->vectors[] 裡去。我們會用到其中幾個?普通 forth kernel 不到兩百 words 。所以第二個疑問明白了。第一個疑問可以用多重獨立的 VM 機來解釋,KsanaVM 架構可能是參考 ficl 而來的,我印象裡 ficl 的說明中有提到這點。
if (vm->terminate) break; <=== 平時只管閉著眼睛把工作丟給廠商做。萬一客人喊停時,靠這個機關通知上中下游,該筆生意結束了。
}
KVMFree(vm); // free the virtual machine
}
2008/10/25
15:46
ksanavm_rev4.zip 裡有缺東西,KVMComma() is missing. 我斷定應該直接用 ksanavm_rev6.zip
<== Correct ! -Hcchen5600 2008/10/25 下午 6:45
1. run Microsoft Visual Studio 2005 's IDE ==> "D:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe"
2. File > New project
Project type ==> Visual C++ > Win32
Templates ==> Win32 console application
3. Name = KsanaVM <================================== 用一個名字跑完全部課程就可以了,無需每課改名。
Specify a folder, click ok
4. On Win32 application Wizard
Click next (not finish) to to Application settings
application type ==> console application
Additional options ==> Empty project <============== because I will port everything from teacher.
5. Click Finish
6. bring in all the ~.c and ~.h files from teacher's package ksanavm_rev6
7. comment / uncomment Lesson1~n in main.c to trace each lesson.
17:14
Lesson4 表面上只是加兩個 words 進 KsanaWord vm->vectors[] 而已, they are ',' and 'here', 這兩個 words 揭開了我先前的疑問:vm->
vectors[] 為何不叫 vm->
dictionary[] ? 把將來要放 colon words 的 vm->dictionary 與用 c++ 打造出來的 vm->vectors 分開,好處盡在不言中。
20:18
Lesson4
DictCompile() look redundant. Why not done its job by KVMComma() directly? [x] Sorry ! They are different tinily.
KVMRet() is called
UNNEST in F-PC. 我很明顯地覺得,這部份用 eforth x86 v2.02 assembly 來理解反而更容易一些。很奇怪喔,assembly 東西少,更能直指核心。
void KVMRet(KsanaVm *vm) // return to caller
{
if (vm->rp<0) {
vm->abortexec=1; // quit innerloop if no more item on return stack *2* <== eforth 裡, UNNEST 從 return stack 裡 pop IP 出來以後就閉著眼睛 $NEXT 過去,我不記得有這個稍嫌冗長的判斷。 這是因為結構不同的關係,KsanaVM 的 KVMCall() / KVMRet() 是為了遷就 c language 嗎? 比 eforth 的 DOLIST - UNNEST & $NEXT 麻煩。 eforth 堪稱天籟。 -Hcchen5600 2008/10/25 下午 8:53
return;
}
vm->ip=KVMRPop(vm); // recover the ip from return stack *1*
}
I think even on a C++ port, DOLIST and $UNNEST $NEXT should be used too. Why not? The benefit is that we don't need to distinguish colon word and C-code word,
void KVMCall(KsanaVm *vm, int startaddr)
{
int addr;
vm->abortexec=0;
vm->ip=startaddr;
do {
addr=*((int*)vm->ip); // fetch the address from ip
vm->ip+=CELLSIZE; //advance to next CELL
if (inDictionary(vm,addr)) {
KVMRPush(vm,vm->ip); // save the next xt for returning from caller , see *1* <=== 我覺得,若用 eforth 的 DOLIST - UNNEST 結構,這個條件式可以不必。改天來改寫看看 [ ] -Hcchen5600 2008/10/25 下午 9:37
KVMCall(vm,addr); // jmp to the addr , recursive call
} else {
(*(KVMXT)(addr))(vm); // call as a C function ( code-word )
}
} while (!vm->abortexec) ; // abort if not more item on return stack *2*
}
22:00
int do_square(KsanaVm *vm) 是否可以改成 KVMXT compile_square(KsanaVm *vm)
<== asking the forum
Lesson 8
compiler.c is amazing !
這樣就讓 forth 活起來了!
2008/10/26
06:41
Lesson 9
string.c 裡, ~ % 4 處應該改成 ~ % CELLSIZE。
11:24
Lesson 17 看完了。準備動手修改 KsanaVM 教學版成為 retail 版本。
11:38
0. Copy all *.c and *.h from ksanavm_rev6 to D:\Learnings\Forth\KsanaVM\Ksanavm_retail
1. run Microsoft Visual Studio 2005 's IDE
2. File > New project
Project type ==> Visual C++ > Win32
Templates ==> Win32 console application
3. Name = KsanaVM
Specify the folder at D:\Learnings\Forth\KsanaVM\Ksanavm_retail
4. On Win32 application Wizard
Click next (not finish) to to Application settings
application type ==> console application
Additional options ==> Empty project <============== because I will port everything from teacher.
5. Click Finish
6. bring in all the ~.c and ~.h files from teacher's package ksanavm_rev6
7. Build ... OK
11:52
Wrap up all words
2008/10/29
目標是在 EFI 環境下打造一整套好用的、充分的 Forth, 因此 meta-compiler, assembler, debgger 這三項我都會去探討。以下是 yap 老師所給的線索
2008/10/28 H.C. Chen <hcchen5600@gmail.com>:
> 您原來好像有計劃要講解 1) meta compiling 以及 2) assembly - 3) disassembly 甚至 4) debugger。
> 我在摸索 meta compiling。至少以會用為目標,祈請指點方向。
>
From: yapcheahshen@gmail.com yap To: hcchen5600@gmail.com H.C. Chen Date: Tue, 28 Oct 2008 23:36:25 +0800 Subject: Re: PowerCam 電腦 demo 、教學、presentation 解決方案 ===> original email step debugger 涉及 UI ,以及text editor ,算是比較大的題目。assembler和 disassembler 要請陳爽來寫,他才是這方面的專家。比較有意思的是pee hole optimizer,forth 的 optimization 很好做, <====== [ ] 待深入理解 -Hcchen5600 2008/10/29 上午 9:29swiftforth 就是極佳的示範。就像 C compiler 是用 C 來寫,正規的 meta compiling 是用forth 來寫forth。 <==== 我猜到了,只是不敢相信。 就等這句話! -Hcchen5600 2008/10/29 上午 9:29只有為數不多的語言可以自己寫自己。可以試著用 KsanaVM 產生 ARM 或者其他CPU 的forth,或者是做一個支持混編
的 x86 subroutine threaded 的forth ( 如丁陳老師的 eforth ) 不過需要對執行 <==== 印證了 CoreForth 的啟示 -Hcchen5600 2008/10/29 上午 9:31
檔的 (e.g. Windows's PE format) 結構有一些了解。在某個平台上打造一個forth ...
1) 首先是決定 暫存器的用法 (instruction pointer, top of stack ) 和 <==== 對照 KsanaVM, 即 VM.h 裡 struct KsanaVM 的設計。 -Hcchen5600 2008/10/29 上午 9:582) 執行架構(token threaded, direct threaded , subroutine threaded )3) 然後就是基本指令集的建構 (參考 eforth) <==== 即 KsanaVM 第三課 -Hcchen5600 2008/10/29 上午 9:593.5) ksanaVM 是有點偷吃步,number 轉換和字串輸入我使用 c library ,傳統的forth 處 <=== 這是個成功的構想,慈航普渡! -Hcchen5600 2008/10/29 上午 10:00
理 number 轉換和 TIB (terminal input buffer) 都蠻複雜的,初學者啃不太動。4) 有了基本指令,剩下就很輕鬆,可以用高階來建構, <=== 第八課完成以後 -Hcchen5600 2008/10/29 上午 10:04
5) 剩下就是一些和 host 系統的嫁接,以取用其資源。這方面wuja 比我熟,他有好好study過 windows EXE /DLL 的結構。我建議你的學習方向和實務需要結合,這樣動力比較足,成就感也大,否則forth 好玩的東西太多,一不小心就會走很多冤枉路。 <=== 哈哈!有默契。真的好好玩,我猜 yap 自己一定玩遍了很多「冤枉路」, but happily I believe, however ... 謹遵師囑!regards,yap 2008/11/01
感謝前輩的貢獻,留下如此優美的智慧結晶 Forth, eforth, everything。感謝 yap 老師精心製作的教學寶典,有緣領受何其幸運。我們的 EFI Forth 已經初步完成,在 PC EFI Shell 上運作順暢。 後來者在這方面遇到什麼困難,皆願予協助。 這個 page 人人可以 edit,若有不足、錯誤之處,請直接惠予評註、更正, ~ 謝謝大家 ~