首页 4-MMIX-IO

4-MMIX-IO

举报
开通vip

4-MMIX-IO 138 MMIX-IO 1. Introduction. This program module contains brute-force implementations of the ten input/output primitives de�ned at the beginning of MMIX-SIM. The subrou- tines are grouped here as a separate package, because they are intended to be loaded ...

4-MMIX-IO
138 MMIX-IO 1. Introduction. This program module contains brute-force implementations of the ten input/output primitives de�ned at the beginning of MMIX-SIM. The subrou- tines are grouped here as a separate package, because they are intended to be loaded with the pipeline simulator as well as with the simple simulator. hPreprocessor macros 2 i hType de�nitions 3 i hExternal subroutines 4 i hGlobal variables 6 i h Subroutines 7 i 2. Of course we include standard C library routines, and we set things up to accommodate older versions of C. hPreprocessor macros 2 i � #include #include #ifdef __STDC__ #de�ne ARGS(list ) list #else #de�ne ARGS(list ) ( ) #endif #ifndef FILENAME_MAX #de�ne FILENAME_MAX 256 #endif #ifndef SEEK_SET #de�ne SEEK_SET 0 #endif #ifndef SEEK_END #de�ne SEEK_END 2 #endif This code is used in section 1. 3. The unsigned 32-bit type tetra must agree with its de�nition in the simulators. hType de�nitions 3 i � typedef unsigned int tetra; typedef struct f tetra h; l; g octa; =� two tetrabytes makes one octabyte �= See also section 5. This code is used in section 1. 4. Three basic subroutines are used to get strings from the simulated memory and to put strings into that memory. These subroutines are de�ned appropriately in each simulator. We also use a few subroutines and constants de�ned in MMIX-ARITH. hExternal subroutines 4 i � extern char stdin chr ARGS((void)); extern int mmgetchars ARGS((char �buf ; int size ;octa addr ; int stop )); D.E. Knuth: MMIXware, LNCS 1750, pp. 138-147, 1999.  Springer-Verlag Berlin Heidelberg 1999 139 MMIX-IO: INTRODUCTION extern void mmputchars ARGS((unsigned char �buf ; int size ;octa addr )); extern octa oplus ARGS((octa;octa)); extern octa ominus ARGS((octa;octa)); extern octa incr ARGS((octa; int)); extern octa zero octa ; =� zero octa :h = zero octa :l = 0 �= extern octa neg one ; =� neg one :h = neg one :l = �1 �= This code is used in section 1. 5. Each possible handle has a �le pointer and a current mode. hType de�nitions 3 i +� typedef struct f FILE �fp ; =� �le pointer �= int mode ; =� [read OK] + 2[write OK] + 4[binary] + 8[readwrite] �= g sim �le info; 6. hGlobal variables 6 i � sim �le info s�le [256]; See also sections 9 and 24. This code is used in section 1. 7. The �rst three handles are initially open. hSubroutines 7 i � void mmix io init ARGS((void)); void mmix io init ( ) f s�le [0]:fp = stdin ; s�le [0]:mode = 1; s�le [1]:fp = stdout ; s�le [1]:mode = 2; s�le [2]:fp = stderr ; s�le [2]:mode = 2; g See also sections 8, 10, 11, 12, 14, 16, 18, 19, 20, 21, 22, and 23. This code is used in section 1. __STDC__, Standard C. FILE, . FILENAME_MAX=macro, . incr : octa ( ), MMIX-ARITH x6. mmgetchars : int ( ), MMIX-SIM x114. mmgetchars : int ( ), MMIX-PIPE x381. mmputchars : int ( ), MMIX-SIM x117. mmputchars : int ( ), MMIX-PIPE x384. neg one : octa, MMIX-ARITH x4. ominus : octa ( ), MMIX-ARITH x5. oplus : octa ( ), MMIX-ARITH x5. SEEK_END=macro, . SEEK_SET=macro, . stderr : FILE �, . stdin : FILE �, . stdin chr : char ( ), MMIX-SIM x120. stdin chr : char ( ), MMIX-PIPE x387. stdout : FILE �, . zero octa : octa, MMIX-ARITH x4. MMIX-IO: INTRODUCTION 140 8. The only tricky thing about these routines is that we want to protect the standard input, output, and error streams from being preempted. hSubroutines 7 i +� octa mmix fopen ARGS((unsigned char;octa;octa)); octa mmix fopen (handle ; name ;mode ) unsigned char handle ; octa name ; mode ; f char name buf [FILENAME_MAX]; FILE �tmp ; if (mode :h _mode :l > 4) goto abort ; if (mmgetchars (name buf ; FILENAME_MAX;name ; 0) � FILENAME_MAX) goto abort ; if (s�le [handle ]:mode 6= 0 ^ handle > 2) fclose (s�le [handle ]:fp); s�le [handle ]:fp = fopen (name buf ;mode string [mode :l]); if (:s�le [handle ]:fp) goto abort ; s�le [handle ]:mode = mode code [mode :l]; return zero octa ; =� success �= abort : s�le [handle ]:mode = 0; return neg one ; =� failure �= g 9. hGlobal variables 6 i +� char �mode string [ ] = f"r"; "w"; "rb"; "wb"; "w+b"g; int mode code [ ] = f # 1; # 2; # 5; # 6; # fg; 10. If the simulator is being used interactively, we can avoid competition for stdin by substituting another �le. hSubroutines 7 i +� void mmix fake stdin ARGS((FILE �)); void mmix fake stdin (f) FILE �f ; f s�le [0]:fp = f ; =� f should be open in mode "r" �= g 11. h Subroutines 7 i +� octa mmix fclose ARGS((unsigned char)); octa mmix fclose (handle ) unsigned char handle ; f if (s�le [handle ]:mode � 0) return neg one ; if (handle > 2 ^ fclose (s�le [handle ]:fp) 6= 0) return neg one ; s�le [handle ]:mode = 0; return zero octa ; =� success �= g 141 MMIX-IO: INTRODUCTION 12. h Subroutines 7 i +� octa mmix fread ARGS((unsigned char;octa;octa)); octa mmix fread (handle ; bu�er ; size ) unsigned char handle ; octa bu�er ; size ; f register unsigned char �buf ; register int n; octa o; o = neg one ; if (:(s�le [handle ]:mode & # 1)) goto done ; if (s�le [handle ]:mode & # 8) s�le [handle ]:mode &= � # 2; if (size :h) goto done ; buf = (unsigned char �) calloc(size :l; sizeof (char)); if (:buf ) goto done ; hRead n � size :l characters into buf 13 i; mmputchars (buf ; n; bu�er ); free (buf ); o:h = 0; o:l = n; done : return ominus (o; size ); g 13. hRead n � size :l characters into buf 13 i � if (s�le [handle ]:fp � stdin ) f register unsigned char �p; for (p = buf ; n = size :l; p < buf + n; p ++ ) �p = stdin chr ( ); g else f clearerr (s�le [handle ]:fp); n = fread (buf ; 1; size :l; s�le [handle ]:fp); if (ferror (s�le [handle ]:fp)) goto done ; g This code is used in section 12. ARGS=macro ( ), x2. calloc : void �( ), . clearerr : void ( ), . fclose : int ( ), . ferror : int ( ), . FILE, . FILENAME_MAX=macro, . fopen : FILE �( ), . fp : FILE �, x5. fread : size t ( ), . free : void ( ), . h: tetra, x3. l: tetra, x3. mmgetchars : extern int ( ), x4. mmputchars : extern int ( ), x4. mode : int, x5. neg one : octa, MMIX-ARITH x4. octa= struct, x3. ominus : octa ( ), MMIX-ARITH x5. s�le : sim �le info [ ], x6. stdin : FILE �, . stdin chr : extern char ( ), x4. zero octa : octa, MMIX-ARITH x4. MMIX-IO: INTRODUCTION 142 14. h Subroutines 7 i +� octa mmix fgets ARGS((unsigned char;octa;octa)); octa mmix fgets (handle ; bu�er ; size ) unsigned char handle ; octa bu�er ; size ; f char buf [256]; register int n; s; register char �p; octa o; int eof = 0; if (:(s�le [handle ]:mode & # 1)) return neg one ; if (:size :l ^ :size :h) return neg one ; if (s�le [handle ]:mode & # 8) s�le [handle ]:mode &= � # 2; size = incr (size ;�1); o = zero octa ; while (1) f hRead n < 256 characters into buf 15 i; mmputchars (buf ; n+ 1; bu�er ); o = incr (o; n); size = incr (size ;�n); if ((n ^ buf [n� 1] � '\n') _ (:size :l ^ :size :h) _ eof ) return o; bu�er = incr (bu�er ; n); g g 15. hRead n < 256 characters into buf 15 i � s = 255; if (size :l < s ^ :size :h) s = size :l; if (s�le [handle ]:fp � stdin ) for (p = buf ; n = 0; n < s; ) f �p = stdin chr ( ); n ++ ; if (�p ++ � '\n') break; g else f if (:fgets (buf ; s+ 1; s�le [handle ]:fp )) return neg one ; eof = feof (s�le [handle ]:fp); for (p = buf ; n = 0; n < s; ) f if (:�p ^ eof ) break; n ++ ; if (�p ++ � '\n') break; g g �p = '\0'; This code is used in section 14. 143 MMIX-IO: INTRODUCTION 16. The routines that deal with wyde characters might need to be changed on a system that is little-endian; the author wishes good luck to whoever has to do this. MMIX is always big-endian, but external �les prepared on random operating systems might be backwards. hSubroutines 7 i +� octa mmix fgetws ARGS((unsigned char;octa;octa)); octa mmix fgetws (handle ; bu�er ; size ) unsigned char handle ; octa bu�er ; size ; f char buf [256]; register int n; s; register char �p; octa o; int eof ; if (:(s�le [handle ]:mode & # 1)) return neg one ; if (:size :l ^ :size :h) return neg one ; if (s�le [handle ]:mode & # 8) s�le [handle ]:mode &= � # 2; bu�er :l &= �2; size = incr (size ;�1); o = zero octa ; while (1) f hRead n < 128 wyde characters into buf 17 i; mmputchars (buf ; 2 � n+ 2; bu�er ); o = incr (o; n); size = incr (size ;�n); if ((n ^ buf [2 � n� 1] � '\n' ^ buf [2 � n� 2] � 0) _ (:size :l ^ :size :h) _ eof ) return o; bu�er = incr (bu�er ; 2 � n); g g ARGS=macro ( ), x2. feof : int ( ), . fgets : char �( ), . fp : FILE �, x5. h: tetra, x3. incr : octa ( ), MMIX-ARITH x6. l: tetra, x3. mmputchars : extern int ( ), x4. mode : int, x5. neg one : octa, MMIX-ARITH x4. octa= struct, x3. s�le : sim �le info [ ], x6. stdin : FILE �, . stdin chr : extern char ( ), x4. zero octa : octa, MMIX-ARITH x4. MMIX-IO: INTRODUCTION 144 17. hRead n < 128 wyde characters into buf 17 i � s = 127; if (size :l < s ^ :size :h) s = size :l; if (s�le [handle ]:fp � stdin ) for (p = buf ; n = 0; n < s; ) f �p ++ = stdin chr ( ); �p ++ = stdin chr ( ); n ++ ; if (�(p� 1) � '\n' ^ �(p� 2) � 0) break; g else for (p = buf ; n = 0; n < s; ) f if (fread (p; 1; 2; s�le [handle ]:fp ) 6= 2) f eof = feof (s�le [handle ]:fp); if (:eof ) return neg one ; break; g n ++ ; p += 2; if (�(p� 1) � '\n' ^ �(p� 2) � 0) break; g �p = �(p+ 1) = '\0'; This code is used in section 16. 18. h Subroutines 7 i +� octa mmix fwrite ARGS((unsigned char;octa;octa)); octa mmix fwrite (handle ; bu�er ; size ) unsigned char handle ; octa bu�er ; size ; f char buf [256]; register int n; if (:(s�le [handle ]:mode & # 2)) return ominus (zero octa ; size ); if (s�le [handle ]:mode & # 8) s�le [handle ]:mode &= � # 1; while (1) f if (size :h _ size :l � 256) n = mmgetchars (buf ; 256; bu�er ;�1); else n = mmgetchars (buf ; size :l; bu�er ;�1); size = incr (size ;�n); if (fwrite (buf ; 1; n; s�le [handle ]:fp) 6= n) return ominus (zero octa ; size ); �ush (s�le [handle ]:fp); if (:size :l ^ :size :h) return zero octa ; bu�er = incr (bu�er ; n); g g 19. h Subroutines 7 i +� octa mmix fputs ARGS((unsigned char;octa)); octa mmix fputs (handle ; string ) unsigned char handle ; octa string ; f 145 MMIX-IO: INTRODUCTION char buf [256]; register int n; octa o; o = zero octa ; if (:(s�le [handle ]:mode & # 2)) return neg one ; if (s�le [handle ]:mode & # 8) s�le [handle ]:mode &= � # 1; while (1) f n = mmgetchars (buf ; 256; string ; 0); if (fwrite (buf ; 1; n; s�le [handle ]:fp) 6= n) return neg one ; o = incr (o; n); if (n < 256) f �ush (s�le [handle ]:fp); return o; g string = incr (string ; n); g g 20. h Subroutines 7 i +� octa mmix fputws ARGS((unsigned char;octa)); octa mmix fputws (handle ; string ) unsigned char handle ; octa string ; f char buf [256]; register int n; octa o; o = zero octa ; if (:(s�le [handle ]:mode & # 2)) return neg one ; while (1) f n = mmgetchars (buf ; 256; string ; 1); if (fwrite (buf ; 1; n; s�le [handle ]:fp) 6= n) return neg one ; o = incr (o; n� 1); if (n < 256) f �ush (s�le [handle ]:fp); return o; g string = incr (string ; n); g g ARGS=macro ( ), x2. buf : char [ ], x16. eof : int, x16. feof : int ( ), . �ush : int ( ), . fp : FILE �, x5. fread : size t ( ), . fwrite : size t ( ), . h: tetra, x3. handle : unsigned char, x16. incr : octa ( ), MMIX-ARITH x6. l: tetra, x3. mmgetchars : extern int ( ), x4. mode : int, x5. n: register int, x16. neg one : octa, MMIX-ARITH x4. octa= struct, x3. ominus : octa ( ), MMIX-ARITH x5. p: register char �, x16. s: register int, x16. s�le : sim �le info [ ], x6. size : octa, x16. stdin : FILE �, . stdin chr : extern char ( ), x4. zero octa : octa, MMIX-ARITH x4. MMIX-IO: INTRODUCTION 146 21. #de�ne sign bit ((unsigned) # 80000000) hSubroutines 7 i +� octa mmix fseek ARGS((unsigned char;octa)); octa mmix fseek (handle ; o�set ) unsigned char handle ; octa o�set ; f if (:(s�le [handle ]:mode & # 4)) return neg one ; if (s�le [handle ]:mode & # 8) s�le [handle ]:mode = # f; if (o�set :h& sign bit ) f if (o�set :h 6= # ffffffff _ :(o�set :l & sign bit )) return neg one ; if (fseek (s�le [handle ]:fp ; (int) o�set :l + 1; SEEK_END) 6= 0) return neg one ; g else f if (o�set :h _ (o�set :l & sign bit )) return neg one ; if (fseek (s�le [handle ]:fp ; (int) o�set :l; SEEK_SET) 6= 0) return neg one ; g return zero octa ; g 22. h Subroutines 7 i +� octa mmix ftell ARGS((unsigned char)); octa mmix ftell (handle ) unsigned char handle ; f register long x; octa o; if (:(s�le [handle ]:mode & # 4)) return neg one ; x = ftell (s�le [handle ]:fp); if (x < 0) return neg one ; o:h = 0; o:l = x; return o; g 23. One last subroutine belongs here, just in case the user has modi�ed the standard error handle. hSubroutines 7 i +� void print trip warning ARGS((int;octa)); void print trip warning (n; loc) int n; octa loc ; f if (s�le [2]:mode & # 2) fprintf (s�le [2]:fp ; "Warning: %s at location %08x%08x\n"; trip warning [n]; loc :h; loc :l); g 24. hGlobal variables 6 i +� char �trip warning [ ] = f"TRIP"; "integer divide check"; "integer overflow"; "float-to-fix overflow"; "invalid floating point operation"; "floating point overflow"; "floating point underflow"; "floating point division by zero"; "floating point inexact"g; 147 MMIX-IO: NAMES OF THE SECTIONS 25. Names of the sections. hExternal subroutines 4 i Used in section 1. hGlobal variables 6, 9, 24 i Used in section 1. hPreprocessor macros 2 i Used in section 1. hRead n < 128 wyde characters into buf 17 i Used in section 16. hRead n < 256 characters into buf 15 i Used in section 14. hRead n � size :l characters into buf 13 i Used in section 12. hSubroutines 7, 8, 10, 11, 12, 14, 16, 18, 19, 20, 21, 22, 23 i Used in section 1. hType de�nitions 3, 5 i Used in section 1. ARGS=macro ( ), x2. fp : FILE �, x5. fprintf : int ( ), . fseek : int ( ), . ftell : long ( ), . h: tetra, x3. l: tetra, x3. mode : int, x5. neg one : octa, MMIX-ARITH x4. octa= struct, x3. SEEK_END=macro, . SEEK_SET=macro, . s�le : sim �le info [ ], x6. zero octa : octa, MMIX-ARITH x4. Introduction Names of the sections
本文档为【4-MMIX-IO】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_021234
暂无简介~
格式:pdf
大小:237KB
软件:PDF阅读器
页数:10
分类:互联网
上传时间:2010-12-01
浏览量:24