鏈枃鐩綍閫熻锛�
閫氳繃涓婁竴绡�璧拌繘Golang涔嬫眹缂栧師鐞�锛屾垜浠煡閬撲簡鐩爣浠g爜鐨勭敓鎴愮粡鍘嗕簡閭d簺杩囩▼銆備粖澶╂垜浠竴璧锋潵瀛︿範涓�涓嬬敓鎴愮殑鐩爣浠g爜濡備綍鍦ㄨ绠楁満涓婃墽琛屻�備互鍙婇�氳繃鏌ラ槄 Golang
鐨� Plan9 姹囩紪鏉ヤ簡瑙olang鐨勪竴浜涘唴閮ㄧ瀵嗐��
Golang鐨勮繍琛岀幆澧�
褰撴垜浠妸缂栬瘧鍚庣殑Go浠g爜杩愯璧锋潵锛屽畠浼氫互杩涚▼鐨勬柟寮忓嚭鐜板湪绯荤粺涓�傜劧鍚庡紑濮嬪鐞嗚姹傘�佹暟鎹紝鎴戜滑浼氱湅鍒拌繖涓繘绋嬪崰鐢ㄤ簡鍐呭瓨娑堣�椼�乧pu鍗犳瘮绛夌瓑淇℃伅銆傛湰鏂囧氨鏄鏉ヨВ閲婂湪绋嬪簭鐨勮繍琛岃繃绋嬩腑锛屽唴瀛樸�丆PU銆佹搷浣滅郴缁燂紙褰撶劧杩樻湁鍏跺畠鐨勭‖浠讹紝鏂囦腑鍏崇郴涓嶅ぇ锛屽氨涓嶈浜嗭級鏄浣曡繘琛岄厤鍚堬紝瀹屾垚浜嗘垜浠唬鐮佹墍鎸囧畾鐨勪簨鎯呫��
鍐呭瓨
棣栧厛锛屾垜浠厛鏉ヨ璇村唴瀛樸�傚厛鏉ョ湅涓�涓垜浠繍琛岀殑go杩涚▼銆�
浠g爜濡備笅锛�
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", sayHello)
err := http.ListenAndServe(":9999", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
fmt.Printf("fibonacci: %d\n", fibonacci(1000))
_, _ = fmt.Fprint(w, "Hello World!")
}
func fibonacci(num int) int {
if num < 2 {
return 1
}
return fibonacci(num-1) + fibonacci(num-2)
}
鏉ョ湅涓�涓嬫墽琛屾儏鍐�
dayu.com >ps aux
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
xxxxx 3584 99.2 0.1 4380456 4376 s003 R+ 8:33涓嬪崍 0:05.81 ./myhttp
杩欓噷鎴戜滑鍏堟潵涓嶅叧娉ㄥ叾瀹冩寚鏍囷紝鍏堟潵鐪� VSZ
涓� RSS
銆�
- VSZ: 鏄寚铏氭嫙鍦板潃锛屼粬鏄▼搴忓疄闄呮搷浣滅殑鍐呭瓨銆傚寘鍚簡鍒嗛厤杩樻病鏈変娇鐢ㄧ殑鍐呭瓨銆�
- RSS: 鏄疄闄呯殑鐗╃悊鍐呭瓨锛屽寘鍚簡鏍堝唴瀛樹笌鍫嗗唴瀛樸��
姣忎竴涓繘绋嬮兘鏄繍琛屽湪鑷繁鐨勫唴瀛樻矙鐩掗噷锛岀▼搴忚鍒嗛厤鐨勫湴鍧�閮芥槸 鈥滆櫄鎷熷唴瀛樷�濓紝鐗╃悊鍐呭瓨瀵圭▼搴忓紑鍙戣�呮潵璇村疄闄呮槸涓嶅彲瑙佺殑锛岃�屼笖铏氭嫙鍦板潃姣旇繘绋嬪疄闄呯殑鐗╃悊鍦板潃瑕佸ぇ鐨勫銆傛垜浠粡甯哥紪绋嬩腑鍙栨寚閽堝搴旂殑鍦板潃瀹為檯灏辨槸铏氭嫙鍦板潃銆傝繖閲屼竴瀹氳娉ㄦ剰鍖哄垎铏氭嫙鍐呭瓨涓庣墿鐞嗗唴瀛樸�傛潵涓�寮犲浘鎰熷彈涓�涓嬨��
杩欏紶鍥句富瑕佹槸涓轰簡璇存槑涓や釜闂锛�
- 绋嬪簭浣跨敤鐨勬槸铏氭嫙鍐呭瓨锛屼絾鏄搷浣滅郴缁熶細鎶婅櫄鎷熷唴瀛樻槧灏勫埌鐗╃悊鍐呭瓨锛涗綘浼氬彂鐜拌嚜宸辨満鍣ㄤ笂鎵�鏈夎繘绋嬬殑VSZ瑕佸ぇ寰楀锛�
- 鐗╃悊鍐呭瓨鍙互琚涓繘绋嬪叡浜紝鐢氳嚦涓�涓繘绋嬪唴鐨勪笉鍚屽湴鍧�鍙兘鏄犲皠鐨勯兘鏄悓涓�涓墿鐞嗗唴瀛樺湴鍧�銆�
涓婇潰鎼炴槑鐧戒簡绋嬪簭涓殑鍐呭瓨鍏蜂綋鏄寚浠�涔堬紝鎺ヤ笅鏉ヨ鏄庣▼搴忔槸濡備綍浣跨敤鍐呭瓨鐨勶紙铏氭嫙鍐呭瓨锛夛紝鍐呭瓨璇寸櫧浜嗗氨鏄瘮纭洏瀛樺彇閫熷害鏇村揩鐨勪竴涓‖浠讹紝涓轰簡鏂逛究鍐呭瓨鐨勭鐞嗭紝鎿嶄綔绯荤粺鎶婂垎閰嶇粰杩涚▼鐨勫唴瀛樺垝鍒嗘垚浜嗕笉鍚岀殑鍔熻兘鍧椼�傚儚鎴戜滑缁忓父璇寸殑锛氫唬鐮佸尯锛岄潤鎬佹暟鎹尯锛屽爢鍖猴紝鏍堝尯绛夈��
杩欓噷鍊熺敤涓�寮犵綉缁滀笂鐨勫浘鏉ョ湅涓�涓嬨��
杩欓噷灏辨槸鎴戜滑绋嬪簭锛堣繘绋嬶級鍦ㄨ櫄鎷熷唴瀛樹腑鐨勫垎甯冦��
浠g爜鍖猴細瀛樻斁鐨勫氨鏄垜浠紪璇戝悗鐨勬満鍣ㄧ爜锛屼竴鑸潵璇磋繖涓尯鍩熷彧鑳芥槸鍙銆�
闈欐�佹暟鎹尯锛氬瓨鏀剧殑鏄叏灞�鍙橀噺涓庡父閲忋�傝繖浜涘彉閲忕殑鍦板潃缂栬瘧鐨勬椂鍊欏氨纭畾浜嗭紙杩欎篃鏄娇鐢ㄨ櫄鎷熷湴鍧�鐨勫ソ澶勶紝濡傛灉鏄墿鐞嗗湴鍧�锛岃繖浜涘湴鍧�缂栬瘧鐨勬椂鍊欐槸涓嶅彲鑳界‘瀹氱殑锛夈�侱ata涓嶣SS閮藉睘浜庤繖涓�閮ㄥ垎銆傝繖閮ㄥ垎鍙湁绋嬪簭涓锛坘ill鎺夈�乧rasg鎺夌瓑锛夋墠浼氳閿�姣併��
鏍堝尯锛氫富瑕佹槸 Golang
閲岃竟鐨勫嚱鏁般�佹柟娉曚互鍙婂叾鏈湴鍙橀噺瀛樺偍鐨勫湴鏂广�傝繖閮ㄥ垎浼撮殢鍑芥暟銆佹柟娉曞紑濮嬫墽琛岃�屽垎閰嶏紝杩愯瀹屽悗灏辫閲婃斁锛岀壒鍒敞鎰忚繖閲岀殑閲婃斁骞朵笉浼氭竻绌哄唴瀛樸�傚悗闈㈡枃绔犺鍐呭瓨鍒嗛厤鐨勬椂鍊欏啀璇︾粏璇达紱杩樻湁涓�涓偣闇�瑕佽浣忔爤涓�鑸槸浠庨珮鍦板潃鍚戜綆鍦板潃鏂瑰悜鍒嗛厤锛屾崲鍙ヨ瘽璇达細楂樺湴鍧�灞炰簬鏍堜綆锛屼綆鍦板潃灞炰簬鏍堝簳锛屽畠鍒嗛厤鏂瑰悜涓庡爢鏄浉鍙嶇殑銆�
鍫嗗尯锛氬儚 C/C++
璇█锛屽爢瀹屽叏鏄▼搴忓憳鑷繁鎺у埗鐨勩�備絾鏄� Golang
閲岃竟鐢变簬鏈塆C鏈哄埗锛屾垜浠啓浠g爜鐨勬椂鍊欏苟涓嶉渶瑕佸叧蹇冨唴瀛樻槸鍦ㄦ爤杩樻槸鍫嗕笂鍒嗛厤銆�Golang
浼氳嚜宸卞垽鏂鏋滃彉閲忕殑鐢熷懡鍛ㄦ湡鍦ㄥ嚱鏁伴��鍑哄悗杩樹笉鑳介攢姣佹垨鑰呮爤涓婅祫婧愪笉澶熷垎閰嶇瓑绛夋儏鍐碉紝灏变細琚斁鍒板爢涓娿�傚爢鐨勬�ц兘浼氭瘮鏍堣宸竴浜涖�傚師鍥犱篃鐣欏埌鍐呭瓨鍒嗛厤鐩稿叧鐨勬枃绔犲啀缁欏ぇ瀹朵粙缁嶃��
鍐呭瓨鐨勭粨鏋勬悶鏄庣櫧浜嗭紝鎴戜滑鐨勭▼搴忚鍔犺浇鍒板唴瀛樿繕闇�瑕佹搷浣滅郴缁熸潵鎸囨尌鎵嶈兘姝g‘杩愯銆�
琛ュ厖涓�涓瘮杈冮噸瑕佺殑姒傚康锛�
瀵诲潃绌洪棿锛氫竴鑸寚鐨勬槸CPU瀵逛簬鍐呭瓨瀵诲潃鐨勮兘鍔涳紝閫氫織鍦拌锛屽氨鏄兘鏈�澶氱敤鍒板灏戝唴瀛樼殑涓�涓棶棰樸�傛瘮濡傦細32鏉″湴鍧�绾匡紙32浣嶆満鍣級锛岄偅涔堟�荤殑鍦板潃绌洪棿灏辨湁 2^32 涓紝濡傛灉鏄�64浣嶆満鍣紝灏辨槸 2^64 涓鍧�绌洪棿銆傚彲浠ヤ娇鐢�
uname -a
鏉ユ煡鐪嬭嚜宸辩郴缁熸敮鎸佺殑浣嶆暟瀛椼��
鎿嶄綔绯荤粺銆丆PU銆佸唴瀛樹簰鐩搁厤鍚�
涓轰簡璁叉竻妤氱▼搴忚繍琛屼笌璋冪敤锛屾垜浠緱鍏堢悊娓呮鎿嶄綔绯荤粺銆佸唴瀛樸�丆PU銆佸瘎瀛樺櫒杩欏嚑鑰呬箣闂寸殑鍏崇郴銆�
- CPU: 璁$畻鏈虹殑澶ц剳锛屽畠鎵嶈兘鐞嗚В骞舵墽琛屾寚浠わ紱
- 瀵勫瓨鍣細涓ユ牸璁插瘎瀛樺櫒鏄疌PU鐨勭粍鎴愰儴鍒嗭紝瀹冧富瑕佽礋璐PU鍦ㄨ绠楁椂涓存椂瀛樺偍鏁版嵁锛涘綋鐒禖PU杩樻湁澶氱骇鐨勯珮閫熺紦瀛橈紝涓庢垜浠繖閲岀浉鍏冲害涓嶅ぇ锛屽氨鐣ヨ繃锛屽ぇ瀹剁煡閬撳叾鐩殑鏄负浜嗗讥琛ュ唴瀛樹笌CPU閫熷害鐨勫樊璺濆嵆鍙紱
- 鍐呭瓨锛氬儚涓婇潰鍐呭瓨琚垝鍒嗘垚涓嶅悓鍖猴紝姣忎竴閮ㄥ垎瀛樹簡涓嶅悓鐨勬暟鎹紱褰撶劧杩欎簺鍖虹殑鍒掑垎銆佷互鍙婅櫄鎷熷唴瀛樹笌鐗╃悊鍐呭瓨鐨勬槧灏勯兘鏄搷浣滅郴缁熸潵鍋氱殑锛�
- 鎿嶄綔绯荤粺锛氭帶鍒跺悇绉嶇‖浠惰祫婧愶紝涓哄叾瀹冭繍琛岀殑绋嬪簭鎻愪緵鎿嶄綔鎺ュ彛锛堢郴缁熻皟鐢級鍙婄鐞嗐��
杩欓噷鎿嶄綔绯荤粺鏄竴涓蒋浠讹紝CPU銆佸瘎瀛樺櫒銆佸唴瀛橈紙鐗╃悊鍐呭瓨锛夐兘鏄疄鎵撳疄鐨勭‖浠躲�傛搷浣滅郴缁熻櫧鐒朵篃鏄竴鍫嗕唬鐮佸啓鍑烘潵鐨勩�備絾鏄ス鏄‖浠跺鍏跺畠搴旂敤绋嬪簭鐨勬帴鍙c�傛�荤殑鏉ヨ鎿嶄綔绯荤粺閫氳繃绯荤粺璋冪敤鎺у埗鎵�鏈夌殑纭欢璧勬簮锛屼粬鎶婂叾瀹冪殑绋嬪簭璋冨害鍒癈PU涓婅鍏跺畠绋嬪簭鎵ц锛屼絾鏄负浜嗚姣忎釜绋嬪簭閮芥湁鏈轰細浣跨敤CPU锛孋PU鍙堥�氳繃鏃堕棿涓柇鎶婃帶鍒舵潈浜ょ粰鎿嶄綔绯荤粺銆�
璁╂搷浣滅郴缁熷彲浠ユ帶鍒舵垜浠殑绋嬪簭锛屾垜浠紪鍐欑殑绋嬪簭闇�瑕侀伒寰搷浣滅郴缁熺殑瑙勫畾銆傝繖鏍锋搷浣滅郴缁熸墠鑳芥帶鍒剁▼搴忔墽琛屻�佸垏鎹㈣繘绋嬬瓑鎿嶄綔銆�
鏈�鍚庢垜浠殑浠g爜琚紪璇戞垚鏈哄櫒鐮佷箣鍚庯紝鏈川灏辨槸涓�鏉℃潯鐨勬寚浠ゃ�傛垜浠湡鏈涚殑灏辨槸CPU鍘绘墽琛屽畬杩欎簺鎸囦护杩涜�屽畬鎴愪换鍔°�傝�屾搷浣滅郴缁熷張鑳藉甯姪鎴戜滑璁〤PU鏉ユ墽琛屼唬鐮佷互鍙婃彁渚涙墍闇�璧勬簮鐨勮皟鐢ㄦ帴鍙o紙绯荤粺璋冪敤锛夈�傛槸涓嶆槸闈炲父绠�鍗曪紵
Go绋嬪簭鐨勮皟鐢ㄨ绾�
鍦ㄤ笂闈㈡垜浠煡閬撴暣涓櫄鎷熷唴瀛樿鎴戜滑鍒掑垎涓猴細浠g爜鍖恒�侀潤鎬佹暟鎹尯銆佹爤鍖恒�佸爢鍖恒�傛帴涓嬫潵瑕佽鐨凣o绋嬪簭鐨勮皟鐢ㄨ绾︼紙鍏跺疄灏辨槸鍑芥暟銆佹柟娉曡繍琛岀殑瑙勫垯锛夛紝涓昏鏄秹鍙婁笂闈㈡墍璇寸殑鏍堥儴鍒嗭紙鍫嗛儴鍒嗕細鍦ㄥ唴瀛樺垎閰嶇殑鏂囩珷閲岃竟鍘昏锛夈�備互鍙婅绠楁満杞‖鍚勪釜閮ㄥ垎濡備綍閰嶅悎銆傛帴涓嬫潵鎴戜滑灏辨潵鐪嬩竴涓嬬▼搴忕殑鍩烘湰鍗曚綅鍑芥暟璺熸柟娉曟槸鎬庝箞鎵ц涓庣浉浜掕皟鐢ㄧ殑銆�
鍑芥暟鍦ㄦ爤涓婄殑鍒嗗竷
杩欎竴閮ㄥ垎锛屾垜浠厛鏉ヤ簡瑙d竴浜涚悊璁猴紝鐒跺悗鎺ョ潃鐢ㄤ竴涓疄闄呯殑渚嬪瓙鏉ュ垎鏋愪竴涓嬨�傚厛閫氳繃涓�寮犲浘鏉ョ湅涓�涓嬪湪 Golang
涓嚱鏁版槸濡備綍鍦ㄦ爤涓婂垎甯冪殑銆�
鍑犱釜娑夊強鍒扮殑涓撲笟鐢ㄨ锛�
- 鏍堬細杩欓噷璇寸殑鏍堣窡涓婇潰鐨勮В閲婂惈涔変竴鑷淬�傛棤璁烘槸杩涚▼銆佺嚎绋嬨�乬oroutine閮芥湁鑷繁鐨勮皟鐢ㄦ爤锛�
- 鏍堝抚锛氬彲浠ョ悊瑙f槸鍑芥暟璋冪敤鏃跺湪鏍堜笂涓哄嚱鏁版墍鍒嗛厤鐨勫尯鍩燂紱
- 璋冪敤鑰咃細caller锛屾瘮濡傦細a鍑芥暟璋冪敤浜哹鍑芥暟锛岄偅涔坅灏辨槸璋冪敤鑰�
- 琚皟鑰咃細callee锛岃繕鏄笂闈㈢殑渚嬪瓙锛宐灏辨槸琚皟鑰�
杩欏箙鍥炬墍灞曠ず鐨勫氨鏄竴涓� 鏍堝抚
鐨勭粨鏋勩�備篃鍙互璇存爤妗㈡槸鏍堢粰涓�涓嚱鏁板垎閰嶇殑鏍堢┖闂达紝瀹冨寘鎷簡鍑芥暟璋冪敤鑰呭湴鍧�銆佹湰鍦板彉閲忋�佽繑鍥炲�煎湴鍧�銆佽皟鐢ㄨ�呭弬鏁扮瓑淇℃伅銆�
杩欓噷鏈夊嚑涓敞鎰忕偣锛屽浘涓殑 BP
銆�SP
閮借〃绀哄搴旂殑瀵勫瓨鍣ㄣ��
- BP锛氬熀鍧�鎸囬拡瀵勫瓨鍣�(extended base pointer)锛屼篃鍙抚鎸囬拡锛屽瓨鏀剧潃涓�涓寚閽堬紝琛ㄧず鍑芥暟鏍堝紑濮嬬殑鍦版柟銆�
- SP锛氭爤鎸囬拡瀵勫瓨鍣�(extended stack pointer)锛屽瓨鏀剧潃涓�涓寚閽堬紝瀛樺偍鐨勬槸鍑芥暟鏍堢┖闂寸殑鏍堥《锛屼篃灏辨槸鍑芥暟鏍堢┖闂村垎閰嶇粨鏉熺殑鍦版柟锛屾敞鎰忚繖閲屾槸纭欢瀵勫瓨鍣紝涓嶆槸Plan9涓殑浼瘎瀛樺櫒銆�
BP
涓� SP
鏀惧湪涓�璧凤紝涓�涓〃绀哄紑濮嬶紙鏍堥《锛夈�佷竴涓〃绀虹粨鏉燂紙鏍堜綆锛夈��
鏈変簡涓婇潰鐨勫熀纭�鐭ヨ瘑锛屾帴鐫�涓嬮潰鐢ㄥ疄闄呯殑渚嬪瓙鏉ラ獙璇佷竴涓嬨��
Go鐨勮皟鐢ㄥ疄渚�
鎵嶅紑濮嬶紝鎴戜滑灏变粠涓�涓畝鍗曠殑鍑芥暟寮�濮嬫潵鍒嗘瀽涓�涓嬫暣涓嚱鏁扮殑璋冪敤杩囩▼锛堜笅闈㈡秹鍙婂埌 Plan9
姹囩紪锛岃鍒厡锛屽ぇ閮ㄥ垎閮借兘澶熺湅鎳傦紝骞朵笖鎴戜篃浼氬啓娉ㄩ噴锛夈��
package main
func main() {
a := 3
b := 2
returnTwo(a, b)
}
func returnTwo(a, b int) (c, d int) {
tmp := 1 // 杩欎竴琛岀殑涓昏鐩殑鏄繚璇佹爤妗笉涓�0锛屾柟渚垮垎鏋�
c = a + b
d = b - tmp
return
}
涓婇潰鏈変袱涓嚱鏁帮紝main
瀹氫箟浜嗕袱涓湰鍦板彉閲忥紝鐒跺悗璋冪敤 returnTwo
鍑芥暟銆�returnTwo
鍑芥暟鏈変袱涓弬鏁颁笌涓や釜杩斿洖鍊笺�傝璁′袱涓繑鍥炲�间富瑕佹槸涓�璧锋潵鐪嬩竴涓� golang
鐨勫杩斿洖鍊兼槸濡備綍瀹炵幇鐨勩�傛帴涓嬫潵鎴戜滑鎶婁笂闈㈢殑浠g爜瀵瑰簲鐨勬眹缂栦唬鐮佸睍绀哄嚭鏉ャ��
鏈夊嚑琛屼唬鐮侀渶瑕佺壒鍒В閲婁笅锛�
0x0000 00000 (test1.go:3) TEXT "".main(SB), ABIInternal, $56-0
杩欎竴琛屼腑鐨勯噸鐐逛俊鎭細$56-0
銆�56 琛ㄧず鐨勮鍑芥暟鏍堟、澶у皬锛堜袱涓湰鍦板彉閲忥紝涓や釜鍙傛暟鏄痠nt绫诲瀷锛屼袱涓繑鍥炲�兼槸int绫诲瀷锛�1涓繚瀛榖ase pointer锛屽悎璁�7 * 8 = 56锛夛紱0琛ㄧず mian
鍑芥暟鐨勫弬鏁颁笌杩斿洖鍊煎ぇ灏忋�傚緟浼氬彲浠ュ湪 returnTwo
涓幓鐪嬩竴涓嬪畠鐨勮繑鍥炲�煎張鏄灏戙��
鎺ヤ笅鏉ュ湪鐪嬩竴涓嬭绠楁満鏄�庝箞鍦ㄦ爤涓婂垎閰嶅ぇ灏忕殑銆�
0x000f 00015 (test1.go:3) SUBQ $56, SP // 鍒嗛厤锛�56鐨勫ぇ灏忓湪涓婇潰绗竴琛屽畾涔変簡
... ...
0x004b 00075 (test1.go:7) ADDQ $56, SP // 閲婃斁鎺夛紝浣嗘槸骞舵湭娓呯┖
杩欎袱琛岋紝涓�涓槸鍒嗛厤锛屼竴涓槸閲婃斁銆備负浠�涔堢敤浜� SUBQ
鎸囦护灏辫兘杩涜鍒嗛厤鍛紵鑰� ADDQ
鏄噴鏀撅紵璁板緱鎴戜滑鍓嶉潰璇磋繃鍚楋紵 SP
鏄竴涓寚閽堝瘎瀛樺櫒锛屽苟涓旀寚鍚戞爤椤讹紝鏍堝張鏄粠楂樺湴鍧�鍚戜綆鍦板潃鍒嗛厤銆傞偅涔堝瀹冨仛涓�娆″噺娉曪紝鏄笉鏄〃绀轰粠楂樺湴鍧�鍚戜綆鍦板潃鏂瑰悜绉诲姩鎸囬拡浜嗗憿锛熼噴鏀句篃鏄悓鏍风殑閬撶悊锛屼竴娆″姞娉曟搷浣滃張鎶� SP
鎭㈠鍒板垵濮嬬姸鎬併��
鍐嶆潵鐪嬩竴涓嬪 BP
瀵勫瓨鍣ㄧ殑鎿嶄綔銆�
0x0013 00019 (test1.go:3) MOVQ BP, 48(SP) // 淇濆瓨BP
0x0018 00024 (test1.go:3) LEAQ 48(SP), BP // BP瀛樻斁浜嗘柊鐨勫湴鍧�
... ...
0x0046 00070 (test1.go:7) MOVQ 48(SP), BP // 鎭㈠BP鐨勫湴鍧�
杩欎笁琛屼唬鐮佹槸涓嶆槸鎰熻寰堝彉鎵紵鍐欐潵鍐欏幓璁╀汉浜戦噷闆鹃噷鐨勩�傛垜鍏堢敤鏂囧瓧鎻忚堪涓�涓嬶紝鍚庨潰鍐嶇敤鍥炬潵瑙i噴銆�
鎴戜滑鍏堝仛濡備笅鍋囪锛氭鏃� BP 鎸囧悜鐨� 鍊� 鏄細0x00ff锛�48(SP) 鐨� 鍦板潃 鏄細0x0008銆�
- 绗竴鏉℃寚浠�
MOVQ BP, 48(SP)
鏄妸0x00ff
鍐欏叆鍒�48(SP)
鐨勪綅缃紱 - 绗簩鏉℃寚浠�
LEAQ 48(SP), BP
鏄洿鏂板瘎瀛樺櫒鎸囬拡锛岃BP
淇濆瓨48(SP)
杩欎釜浣嶇疆鐨勫湴鍧�锛屼篃灏辨槸0x00ff
杩欎釜鍊笺�� - 绗笁鏉℃寚浠�
MOVQ 48(SP), BP
锛屽洜涓轰竴寮�濮�48(SP)
淇濆瓨浜嗘渶寮�濮�BP
鐨勬墍瀛樼殑鍊�0x00ff
锛屾墍浠ヨ繖閲屾槸鍙堟妸BP
鎭㈠鍥炲幓浜嗐��
杩欏嚑琛屼唬鐮佺殑浣滅敤鑷冲叧閲嶈锛屾鍥犱负濡傛鍦ㄦ墽琛岀殑鏃跺�欙紝鎴戜滑鎵嶈兘鎵惧埌鍑芥暟寮�濮嬬殑鍦版柟浠ュ強鍥炲埌璋冪敤鍑芥暟鐨勪綅缃紝瀹冩墠鍙互缁х画寰�涓嬫墽琛岋紙濡傛灉瑙夊緱楗讹紝鍏堟斁杩囷紝鍚庨潰鏈夊浘锛岀湅瀹屽悗鍐嶅洖鏉ョ悊瑙o級銆傛帴鐫�鏉ョ湅涓�涓� returnTwo
鍑芥暟銆�
杩欓噷 NOSPLIT|ABIInternal, $0-32
璇存槑锛岃鍑芥暟鐨勬爤妗㈠ぇ灏忔槸0锛岀敱浜庢湁涓や釜int鍙傛暟锛屼互鍙�2涓猧nt杩斿洖鍊硷紝鍚堣涓� 4*8 = 32
瀛楄妭澶у皬锛屾槸涓嶆槸璺熶笂闈㈢殑 main
鍑芥暟瀵逛笂浜嗭紵銆�
杩欓噷鏈夋病鏈夊 returnTwo
鍑芥暟鐨勬爤妗㈠ぇ灏忔槸0琛ㄧず杩锋儜鍛紵闅鹃亾杩欎釜鍑芥暟涓嶉渶瑕佹爤绌洪棿鍚楋紵鍏跺疄涓昏鍘熷洜鏄細golang鐨勫弬鏁颁紶閫掍笌杩斿洖鍊奸兘鏄姹備娇鐢ㄦ爤鏉ヨ繘琛岀殑锛堣繖涔熸槸涓轰粈涔坓o鑳藉鏀寔澶氬弬鏁拌繑鍥炵殑鍘熷洜锛夈�傛墍浠ュ弬鏁颁笌杩斿洖鍊兼墍闇�绌洪棿閮界敱 caller
鏉ユ彁渚涖��
鎺ヤ笅鏉ワ紝鎴戜滑鐢ㄥ畬鏁寸殑鍥炬潵婕旂ず涓�涓嬭繖涓皟鐢ㄨ繃绋嬨��
杩欎釜鍥惧氨鐢讳簡灏嗚繎1涓皬鏃讹紝甯屾湜瀵瑰ぇ瀹剁悊瑙f湁甯姪銆�
鏁翠釜鐨勬祦绋嬫槸锛氬垵濮嬪寲 ----> call main function ----> call returnTwo function ----> returnTwo return ----> main return銆�
閫氳繃杩欏紶鍥撅紝鍦ㄧ粨鍚堟垜涓婇潰鐨勬枃瀛楄В閲婏紝鐩镐俊澶у鑳藉鐞嗚В浜嗐�備笉杩囪繖閲岃繕鏈夊嚑涓敞鎰忕偣锛�
- BP 涓� SP 鏄瘎瀛樺櫒锛屽畠淇濆瓨鐨勬槸鏍堜笂鐨勫湴鍧�锛屾墍浠ユ墽琛屼腑鍙互瀵�
SP
鍋氳繍绠楁壘鍒颁笅涓�涓寚浠ょ殑浣嶇疆锛� - 鏍堣鍥炴敹
ADDQ $56, SP
锛屽彧鏄敼鍙樹簡SP
鎸囧悜鐨勪綅缃紝鍐呭瓨涓殑鏁版嵁骞朵笉浼氭竻绌猴紝鍙湁涓嬫琚垎閰嶄娇鐢ㄧ殑鏃跺�欐墠浼氭竻绌猴紱 - callee鐨勫弬鏁般�佽繑鍥炲�煎唴瀛橀兘鏄痗aller鍒嗛厤鐨勶紱
- returnTwo ret鐨勬椂鍊欙紝call returnTwo鐨刵ext鎸囦护 鎵�鍦ㄦ爤浣嶇疆浼氳寮瑰嚭锛屼篃灏辨槸鍥句腑
0x0d00
鍦板潃鎵�淇濆瓨鐨勬寚浠わ紝鎵�浠� returnTwo 鍑芥暟杩斿洖鍚庯紝SP
鍙堟寚鍚戜簡0x0d08
鍦板潃銆�
鐢变簬涓婇潰娑夊強鍒颁竴浜� Plan9
鐨勭煡璇嗭紝灏遍『甯︿竴璧蜂粙缁嶄竴浜涘畠鐨勮娉曪紝濡傛灉鐩存帴璁茶娉曚細寰堟灟鐕ワ紝涓嬮潰浼氱粨鍚堜竴浜涘疄闄呬腑浼氱敤鍒扮殑鎯呭喌鏉ヤ粙缁嶃�傛棦鏈夋敹鑾峰張鑳藉浼氳娉曘��
Go鐨勬眹缂杙lan9
鎴戜滑鏁翠釜绋嬪簭鐨勭紪璇戞渶缁堜細琚炕璇戞垚鏈哄櫒鐮侊紝鑰屾眹缂栧彲浠ョ畻鏄満鍣ㄧ爜鐨勬枃鏈舰寮忥紝浠栦滑涔嬮棿鍙互涓�涓�瀵瑰簲銆傛墍浠ュ鏋滄垜浠兘澶熺湅鎳傛眹缂栦竴鐐圭偣灏辫兘澶熷垎鏋愬嚭寰堝瀹為檯闂銆�
寮�鍙慻o璇█鐨勯兘鏄綋鍓嶄笘鐣屾渶TOP鐨勯偅缇ょ▼搴忓憳锛屼粬浠�夋嫨浜嗘寔缁閫硷紝涓嶇敤鏍囧噯鐨� AT&T 涔熶笉鐢� Intel 姹囩紪鍣紝鍋忚鑷繁鎼炰竴濂楋紝娌″姙娉曪紝璋佽浜哄鐗涘憿锛丟olang鐨勬眹缂栨槸鍩轰簬 Plan9
姹囩紪鐨勶紝涓汉瑙夊緱瑕佸畬鍏ㄥ鎳傚お澶嶆潅浜嗭紝鍥犱负杩欐秹鍙婂埌寰堝搴曞眰鐭ヨ瘑銆備笉杩囧鏋滃彧鏄姹傜湅鎳傝繕鏄兘澶熷仛鍒扮殑銆備笅闈㈡垜浠氨涓句竴浜涗緥瀛愭潵璇曡瘯鐪嬨��
PS: 杩欎笢瑗垮畬鍏ㄥ鎳備篃娌℃湁蹇呰锛屾姇鍏ヤ骇鍑烘瘮澶綆浜嗭紝瀵逛簬涓�涓簲鐢ㄥ伐绋嬪笀鑳藉鐪嬫噦灏辫銆�
鍦ㄦ寮忓紑濮嬪墠锛屾垜浠繕鏄ˉ鍏呬竴浜涘繀瑕佷俊鎭紝涓婃枃宸茬粡娑夊強杩囦竴浜涳紝涓轰簡瀹屾暣杩欓噷鍦ㄦ暣浣撲粙缁嶄竴涓嬨��
鍑犱釜閲嶈鐨勪吉瀵勫瓨鍣細
- SB锛氭槸涓�涓櫄鎷熷瘎瀛樺櫒锛屼繚瀛樹簡闈欐�佸熀鍦板潃(static-base) 鎸囬拡锛屽嵆鎴戜滑绋嬪簭鍦板潃绌洪棿鐨勫紑濮嬪湴鍧�锛�
- NOSPLIT锛氬悜缂栬瘧鍣ㄨ〃鏄庝笉搴旇鎻掑叆
stack-split
鐨勭敤鏉ユ鏌ユ爤闇�瑕佹墿寮犵殑鍓嶅鎸囦护锛� - FP锛氫娇鐢ㄥ舰濡� symbol+offset(FP) 鐨勬柟寮忥紝寮曠敤鍑芥暟鐨勮緭鍏ュ弬鏁帮紱
- SP锛歱lan9 鐨勮繖涓� SP 瀵勫瓨鍣ㄦ寚鍚戝綋鍓嶆爤甯х殑灞�閮ㄥ彉閲忕殑寮�濮嬩綅缃紝浣跨敤褰㈠ symbol+offset(SP) 鐨勬柟寮忥紝寮曠敤鍑芥暟鐨勫眬閮ㄥ彉閲忥紝娉ㄦ剰锛氳繖涓瘎瀛樺櫒涓庝笂鏂囩殑瀵勫瓨鍣ㄦ槸涓嶄竴鏍风殑锛岃繖閲屾槸浼瘎瀛樺櫒锛岃�屾垜浠睍绀哄嚭鏉ョ殑閮芥槸纭欢瀵勫瓨鍣ㄣ��
鍏跺畠杩樻湁涓�浜涙搷浣滄寚浠わ紝鏍规嵁鍚嶅瓧澶氬崐閮借兘澶熺湅鍑烘潵锛屽氨涓嶅啀浠嬬粛锛岀洿鎺ュ紑濮嬪共銆�
鏌ョ湅go搴旂敤浠g爜瀵瑰簲鐨勭炕璇戝嚱鏁�
package main
func main() {
}
func test() []string {
a := make([]string, 10)
return a
}
--------
"".test STEXT size=151 args=0x18 locals=0x40
0x0000 00000 (test1.go:6) TEXT "".test(SB), ABIInternal, $64-24 // 鏍堝抚澶у皬锛屼笌鍙傛暟銆佽繑鍥炲�煎ぇ灏�
0x0000 00000 (test1.go:6) MOVQ (TLS), CX
0x0009 00009 (test1.go:6) CMPQ SP, 16(CX)
0x000d 00013 (test1.go:6) JLS 141
0x000f 00015 (test1.go:6) SUBQ $64, SP
0x0013 00019 (test1.go:6) MOVQ BP, 56(SP)
0x0018 00024 (test1.go:6) LEAQ 56(SP), BP
... ...
0x001d 00029 (test1.go:6) MOVQ $0, "".~r0+72(SP)
0x0026 00038 (test1.go:6) XORPS X0, X0
0x0029 00041 (test1.go:6) MOVUPS X0, "".~r0+80(SP)
0x002e 00046 (test1.go:7) PCDATA $2, $1
0x002e 00046 (test1.go:7) LEAQ type.string(SB), AX
0x0035 00053 (test1.go:7) PCDATA $2, $0
0x0035 00053 (test1.go:7) MOVQ AX, (SP)
0x0039 00057 (test1.go:7) MOVQ $10, 8(SP)
0x0042 00066 (test1.go:7) MOVQ $10, 16(SP)
0x004b 00075 (test1.go:7) CALL runtime.makeslice(SB) // 瀵瑰簲鐨勫簳灞俽untime function
... ...
0x008c 00140 (test1.go:8) RET
0x008d 00141 (test1.go:8) NOP
0x008d 00141 (test1.go:6) PCDATA $0, $-1
0x008d 00141 (test1.go:6) PCDATA $2, $-1
0x008d 00141 (test1.go:6) CALL runtime.morestack_noctxt(SB)
0x0092 00146 (test1.go:6) JMP 0
鏍规嵁瀵瑰簲鐨勪唬鐮佽鏁颁笌鍚嶅瓧锛屽緢鏄庢樉鐨勫彲浠ョ湅鍒板簲鐢ㄥ眰鍐欑殑 make
瀵瑰簲搴曞眰鏄� makeslice
銆�
閫冮�稿垎鏋�
杩欓噷鍏堣涓�涓嬮�冮�稿垎鏋愮殑姒傚康銆傝繖閲岀壍鎵埌鏍堛�佸爢鍒嗛厤鐨勯棶棰樸�傚鏋滃彉閲忚鍒嗛厤鍒版爤涓婏紝浼氫即闅忓嚱鏁拌皟鐢ㄧ粨鏉熻嚜鍔ㄥ洖鏀讹紝骞朵笖鍒嗛厤鏁堢巼寰堥珮锛涘叾娆″垎閰嶅埌鍫嗕笂锛屽垯闇�瑕丟C杩涜鏍囪鍥炴敹銆傛墍璋撻�冮�稿氨鏄寚鍙橀噺浠庢爤涓婇�冨埌浜嗗爢涓婏紙寰堝浜哄杩欎釜姒傚康閮戒笉娓呮灏卞湪璋堥�冮�稿垎鏋愶紝闈㈣瘯閬囧埌浜嗗ソ鍑犳馃槗锛夈��
package main
func main() {
}
func test() *int {
t := 3
return &t
}
------
"".test STEXT size=98 args=0x8 locals=0x20
0x0000 00000 (test1.go:6) TEXT "".test(SB), ABIInternal, $32-8
0x0000 00000 (test1.go:6) MOVQ (TLS), CX
0x0009 00009 (test1.go:6) CMPQ SP, 16(CX)
0x000d 00013 (test1.go:6) JLS 91
0x000f 00015 (test1.go:6) SUBQ $32, SP
0x0013 00019 (test1.go:6) MOVQ BP, 24(SP)
0x0018 00024 (test1.go:6) LEAQ 24(SP), BP
... ...
0x001d 00029 (test1.go:6) MOVQ $0, "".~r0+40(SP)
0x0026 00038 (test1.go:7) PCDATA $2, $1
0x0026 00038 (test1.go:7) LEAQ type.int(SB), AX
0x002d 00045 (test1.go:7) PCDATA $2, $0
0x002d 00045 (test1.go:7) MOVQ AX, (SP)
0x0031 00049 (test1.go:7) CALL runtime.newobject(SB) // 鍫嗕笂鍒嗛厤绌洪棿锛岃〃绀洪�冮�镐簡
... ...
杩欓噷濡傛灉鏄 slice
浣跨敤姹囩紪杩涜閫冮�稿垎鏋愶紝骞朵笉浼氬緢鐩磋銆傚洜涓哄彧浼氱湅鍒拌皟鐢ㄤ簡 runtime.makeslice
鍑芥暟锛岃鍑芥暟鍐呴儴鍏跺疄鍙堣皟鐢ㄤ簡 runtime.mallocgc
鍑芥暟锛岃繖涓嚱鏁颁細鍒嗛厤鐨勫唴瀛樺叾瀹炲氨鏄爢涓婄殑鍐呭瓨锛堝鏋滄爤涓婅冻澶熶繚瀛橈紝鏄笉浼氱湅鍒板 runtime.makslice
鍑芥暟鐨勮皟鐢級銆�
瀹為檯go涔熸彁渚涗簡鏇存柟渚跨殑鍛戒护鏉ヨ繘琛岄�冮�稿垎鏋愶細go build -gcflags="-m"
锛屽鏋滅湡鐨勬槸鍋氶�冮�稿垎鏋愶紝寤鸿浣跨敤璇ュ懡浠わ紝鍒姌鑵剧敤姹囩紪銆�
浼犲�艰繕鏄紶鎸囬拡
瀵逛簬golang涓殑鍩烘湰绫诲瀷锛氬瓧绗︿覆銆佹暣鍨嬨�佸竷灏旂被鍨嬪氨涓嶅璇翠簡锛岃偗瀹氭槸鍊间紶閫掞紝閭d箞瀵逛簬缁撴瀯浣撱�佹寚閽堝埌搴曟槸鍊间紶閫掕繕鏄寚閽堜紶閫掑憿锛�
package main
type Student struct {
name string
age int
}
func main() {
jack := &Student{"jack", 30}
test(jack)
}
func test(s *Student) *Student {
return s
}
-------
"".test STEXT nosplit size=20 args=0x10 locals=0x0
0x0000 00000 (test1.go:14) TEXT "".test(SB), NOSPLIT|ABIInternal, $0-16
... ...
0x0000 00000 (test1.go:14) MOVQ $0, "".~r1+16(SP) // 鍒濆杩斿洖鍊间负0
0x0009 00009 (test1.go:15) PCDATA $2, $1
0x0009 00009 (test1.go:15) PCDATA $0, $1
0x0009 00009 (test1.go:15) MOVQ "".s+8(SP), AX // 灏嗗紩鐢ㄥ湴鍧�澶嶅埗鍒� AX 瀵勫瓨鍣�
0x000e 00014 (test1.go:15) PCDATA $2, $0
0x000e 00014 (test1.go:15) PCDATA $0, $2
0x000e 00014 (test1.go:15) MOVQ AX, "".~r1+16(SP) // 灏� AX 鐨勫紩鐢ㄥ湴鍧�鍙堝鍒跺埌杩斿洖鍦板潃
0x0013 00019 (test1.go:15) RET
閫氳繃杩欓噷鍙互鐪嬪埌鍦╣o閲岃竟锛屽彧鏈夊�间紶閫掞紝鍥犱负瀹冨簳灞傝繕鏄�氳繃鎷疯礉瀵瑰簲鐨勫�笺��
鎬荤粨
浠婂ぉ鐨勬枃绔犲埌姝ょ粨鏉燂紝鏈涓昏璁蹭簡涓嬮潰鍑犱釜鐐癸細
- 璁$畻鏈鸿蒋纭祫婧愪箣闂寸殑鐩镐簰閰嶅悎锛�
-
Golang
缂栧啓鐨勪唬鐮侊紝鍑芥暟涓庢柟娉曟槸鎬庝箞鎵ц鐨勶紝涓昏璁蹭簡鏍堜笂鍒嗛厤涓庣浉鍏宠皟鐢紱 - 浣跨敤
Plan9
鍒嗘瀽浜嗕竴浜涘父瑙佺殑闂銆�
甯屾湜鏈枃瀵瑰ぇ瀹跺湪鐞嗚В銆佸涔燝o鐨勮矾涓婃湁涓�浜涘府鍔┿��
鍙傝�冭祫鏂�
- [1] A visual guide to Go Memory Allocator from scratch
- [2] A Quick Guide to Go's Assembler
- [3] A Quick Guide to Go's Assembler 涓枃鐗�
- [4] go 鍜� plan9 姹囩紪
- [5] plan9 assembly 瀹屽叏瑙f瀽
- [6] 瀵勫瓨鍣� wiki
- [7] Go 鍑芥暟璋冪敤 鈹� 鏍堝拰瀵勫瓨鍣ㄨ瑙�