From: Thomas Walker Lynch Date: Wed, 19 Mar 2025 02:20:24 +0000 (+0000) Subject: checkpoint before Core spins out to multiple files: Core TM Map ... X-Git-Url: https://git.reasoningtechnology.com/style/static/gitweb.css?a=commitdiff_plain;h=425a81ca66ef3881b0d6aa2659cf6128ba357d62;p=N checkpoint before Core spins out to multiple files: Core TM Map ... --- diff --git "a/developer/cc\360\237\226\211/Core.lib.c" "b/developer/cc\360\237\226\211/Core.lib.c" index 35d2e8e..26297b8 100644 --- "a/developer/cc\360\237\226\211/Core.lib.c" +++ "b/developer/cc\360\237\226\211/Core.lib.c" @@ -12,6 +12,8 @@ 'ATP' `At This Point' in the code. Acronym used in comments usually before pointing out certain values variables must have. + 'AToW' - At Time of Writing, also used in comments. + 'Tape' is operated on by the Tape Machine. 'Area' is subset of an address space that is used as a virtual Tape by a machine. @@ -53,9 +55,11 @@ #define _Ξ(a ,b) a##·##b #define Ξ(a ,b) _Ξ(a ,b) + // ask the machine what this is + // C language standard left this undefined, probably why unicode uses 'octet' + // AToW industry uses uint8_t // reminds me of FORTRAN star types - // C language did not guarantee 8 bit bytes, which is why Unicode speaks of octets - #define AU AU + #define AU uint8_t #define AU2 uint16_t #define UA4 uint32_t #define UA8 uint64_t @@ -65,14 +69,20 @@ #define UA4_MAX ~(AU4)0 #define UA8_MAX ~(AU8)0 - // ask the compiler what this really is - // when using enums we get this whether we want them or not + // ask the compiler what this is + // when using enums we get this whether we want it or not #define WU unsigned int #define WU ~(WU)0 - // 'extent of an object' is the maximum AU index within an object. For example the extent of AU4 is 3. - #define extentof(x)(sizeof(x) - 1) - #define extent_t size_t + // extent is an address or an index. It is not a length. + // The index scaling CVT type is appended to the end of the extent related identifiers.. + // E.g. exent_of·AU(uint64_t) == 7 while exent_of·uint16_t(uint64_t) == 3; + // exent_of·AU of the address space is ~(uintptr)0; + #define extent_of·AU(x)(sizeof(x) - 1) + #define extent_t·AU size_t + + // Funny, we seldom check for this, but maybe someone is running a microcontroller or something, so we will here. Also, too bad that address 0 can't be used. + #define extent_address_space·AU ~(uintptr)0; typedef enum{ Core·Status·mu = 0 @@ -82,13 +92,17 @@ #endif + #ifdef CVT + #define Ξ(extent_t ,CVT) size_t + #endif + //---------------------------------------- // argument guard interface //---------------------------------------- #ifndef CVT - typedef void (*Flag·Function)(WU *flag ,WU err); + typedef void (*Core·Flag·Fn)(WU *flag ,WU err); void Core·Flag·count(WU *flag ,WU err){ if(err == WU_MAX){ *flag = WU_MAX; return;} @@ -105,12 +119,12 @@ typedef struct { char *name; - Flag·Function flag_function; + Core·Flag·Fn flag_function; WU flag; } Core·Guard; typedef struct { - void (*init)(Core·Guard *chk ,const char *name ,Flag·Function af); + void (*init)(Core·Guard *chk ,const char *name ,Core·Flag·Fn af); void (*reset)(Core·Guard *chk); void (*check)( Core·Guard *chk @@ -149,17 +163,16 @@ typedef struct{ Core·Status (*on_track)(); Core·Status (*derailed)(); - AU *(*offset)(AU *p ,size_t Δ); - bool is_aligned_on(void *p ,extent_t alignment); + Core·Status (*is_aligned)(AU *p ,extent·AU alignment ,bool *flag); + Core·Status (*round_down)(AU *p ,extent·AU alignment ,AU **result); + Core·Status (*round_up)(AU *p ,extent·AU alignment ,AU **result); } Core·F; Local Core·F Core·f; #endif #ifdef CVT typedef struct{ - CVT *(*offset)(CVT *p ,size_t Δ); - AU *(*floor_within_aligned)(CVT *p); - AU *(*ceiling_within_aligned)(CVT *p); + // nothing here right now } Ξ(Core·F ,CVT); Local Ξ(Core·F ,CVT) Ξ(Core·F ,CVT)·f; #endif @@ -211,12 +224,12 @@ struct { Core·Status (*topo)(Core·TM_NX *tm ,Core·Tape·Topo *topo); - Core·Status (*extent)(Core·TM_NX *tm ,extent_t *extent_pt); + Core·Status (*extent)(Core·TM_NX *tm ,extent_t·AU *result); } tape; struct { // Initialize tm - Core·Status (*mount_pe)(Core·TM_NX *tm ,AU position[] ,extent_t extent); + Core·Status (*mount_pe)(Core·TM_NX *tm ,AU position[] ,extent_t·AU extent·AU); Core·Status (*mount_pp)(Core·TM_NX *tm ,AU *position_left ,AU *position_right); // predicates @@ -259,24 +272,15 @@ typedef struct{ - Core·Status (*copy_datum)( Ξ(Core·TM_NX ,CVT) *tm_read ,Ξ(Core·TM_NX ,CVT) *tm_write ); - Core·Status (*read)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *remote_pt ); - Core·Status (*write)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *remote_pt ); - Core·Status (*head_pt)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *head_pt ); + struct { + Core·Status (*extent)(Ξ(Core·TM_NX ,CVT) *tm ,Ξ(extent_t ,CVT) *result); + } tape; struct { // Initialize tm - Core·Status (*mount_pe)( - Core·TM_NX tm - ,CVT position[] - ,extent_t extent - ); - Core·Status (*mount_pp)( - Core·TM_NX *tm - ,CVT *position_left - ,CVT *position_right - ); + Core·Status (*mount_pe)(Core·TM_NX tm ,CVT position[] ,Ξ(extent_t ,CVT) extent); + Core·Status (*mount_pp)(Core·TM_NX *tm ,CVT *position_left ,CVT *position_right); // initializes inner Core·Status (*largest_aligned)(Core·TM_NX_uint8_t *outer ,Ξ(Core·TM_NX ,CVT) *inner); @@ -295,6 +299,12 @@ ); } area; + Core·Status (*copy_datum)( Ξ(Core·TM_NX ,CVT) *tm_read ,Ξ(Core·TM_NX ,CVT) *tm_write ); + Core·Status (*read)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *remote_pt ); + Core·Status (*write)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *remote_pt ); + Core·Status (*head_pt)( Ξ(Core·TM_NX ,CVT) *tm ,CVT *head_pt ); + + } Ξ(Core·TM_NX ,CVT)·FG; Ξ(Core·TM_NX ,CVT)·FG Ξ(Core·TM_NX ,CVT)·fg; @@ -373,7 +383,7 @@ //---------------------------------------- #ifndef CVT - Local void Core·Guard·init(Core·Guard *chk ,Flag·Function af){ + Local void Core·Guard·init(Core·Guard *chk ,Core·Flag·Fn af){ if( !chk ) return; chk->flag_function = af; chk->flag = 0; @@ -412,17 +422,7 @@ Core·Status Core·on_track(){ return Core·Status·on_track; } Core·Status Core·derailed(){ return Core·Status·derailed; } - Local void *Core·offset(AU *p ,size_t Δ){ - #ifdef Core·Debug - if(!p){ - fprintf(stderr ,"Core·offset:: given NULL `p'"); - return NULL; - } - #endif - return (void *)( (CVT *)p ) + Δ; - } - - Local Core·Status Core·is_aligned(AU *p ,extent_t alignment ,bool *flag){ + Local Core·Status Core·is_aligned(AU *p ,extent·AU alignment ,bool *flag){ #ifdef Core·Debug Core·Guard·init_count(chk); Core·Guard·fg.check(&chk ,1 ,p ,"given NULL p"); @@ -433,7 +433,7 @@ return Core·Status·on_track; } - Local Core·Status Core·round_down(AU *p ,extent_t alignment ,AU **result){ + Local Core·Status Core·round_down(AU *p ,extent·AU alignment ,AU **result){ #ifdef Core·Debug Core·Guard·init_count(chk); Core·Guard·fg.check(&chk ,1 ,p ,"given NULL p to round"); @@ -444,7 +444,7 @@ return Core·Status·on_track; } - Local Core·Status Core·round_up(AU *p ,extent_t alignment ,AU **result){ + Local Core·Status Core·round_up(AU *p ,extent·AU alignment ,AU **result){ #ifdef Core·Debug Core·Guard·init_count(chk); Core·Guard·fg.check(&chk ,1 ,p ,"given NULL p to round"); @@ -458,29 +458,17 @@ Local Core·F Core·f = { .on_track = Core·on_track ,.derailed = Core·derailed - ,.offset = Core·offset + ,.is_aligned = Core·is_aligned + ,.round_down = round_down + ,.round_up = round_up }; #endif // #ifndef CVT #ifdef CVT - Local void Ξ(Core·F ,CVT)·offset(CVT *p ,size_t Δ){ - #ifdef Core·Debug - if(!p){ - fprintf(stderr ,"Core·offset_##CVT:: given NULL `p'"); - return NULL; - } - #endif - return p + Δ; - } - // Struct instance initialization Ξ(Core·F ,CVT) Ξ(Core·F ,CVT)·f = { - .offset = Ξ(Core·F ,CVT)·offset - ,.is_aligned_on = Ξ(Core·F ,CVT)·is_aligned_on - ,.floor_within_aligned = Ξ(Core·F ,CVT)·floor_within_aligned - ,.ceiling_within_aligned = Ξ(Core·F ,CVT)·ceiling_within_aligned }; #endif @@ -492,7 +480,7 @@ /* TM_NX are initialized with calls to 'mount_pe' or 'mount_pp'. These both bind the machine to a tape, and mount the tape. Hence, there is no such thing as an initialized which is not bound to a tape. (It is possible to dismount the tm->hd, then remount tm->hd, but the tape remains bound through that process.) - Because the TM_NX has no destructive operations, Once it is initialized the tape will never get shorter. With TM_MX it is not possible to mount an empty tape, because the minimum value of extent is zero. Therefore the tape can never be empty. + Because the TM_NX has no destructive operations, Once it is initialized the tape will never get shorter. With TM_MX it is not possible to mount an empty tape, because the minimum value of extent·AU is zero. Therefore the tape can never be empty. For an initialized TM_NX, TM_NX.array.hd == NULL means the tape is currently not mounted. @@ -505,7 +493,7 @@ #ifndef CVT struct{ AU *position; - extent_t extent; + extent·AU extent·AU; AU *hd; }Core·TM_NX; #endif @@ -513,34 +501,15 @@ #ifdef CVT struct{ CVT *position; - extent_t extent; + extent·AU extent·AU; CVT *hd; }Ξ(Core·TM_NX ,CVT); #endif //---------------------------------------- - // TM_NX + // TM_NX implementation //---------------------------------------- - #ifndef CVT - Core·Status Core·TM_NX·topo(Core·TM_NX *tm ,Core·Tape·Topo *topo){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,topo ,"topo ptr is NULL, so nowhere to put result"); - Core·Guard·if_return(chk); - #endif - if(tm->extent == 0){ - *topo = Core·Tape·Topo·singleton; - }else{ - *topo = Core·Tape·Topo·segment; - } - return Core·Status·on_track; - } - #endif - - #ifdef CVT - //----------------------------------- // common error messages @@ -550,474 +519,498 @@ "given NULL address pointer ,or address struct holds NULL address"; const char *Ξ(Core·TM_NX ,CVT)·Msg·address_on_tape= "given address is not on the tape"; - const char *Ξ(Core·TM_NX ,CVT)·Msg·extent="given NULL extent pointer"; + const char *Ξ(Core·TM_NX ,CVT)·Msg·extent·AU="given NULL extent·AU pointer"; const char *Ξ(Core·TM_NX ,CVT)·Msg·position= "Null position.This is only possible when the tape machine has not been initialized."; - //----------------------------------- - // Area functions within Core·TM_NX_##CVT - - Core·Status Core·TM_NX_##CVT·topo(Core·TM_NX *tm ,Core·Tape·Topo *topo){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,topo ,"topo ptr is NULL, so nowhere to put result"); - Core·Guard·if_return(chk); - #endif - if(tm->extent == 0){ + #ifndef CVT + Core·Status Core·TM_NX·topo(Core·TM_NX *tm ,Core·Tape·Topo *topo){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,topo ,"topo ptr is NULL, so nowhere to put result"); + Core·Guard·if_return(chk); + #endif + if(tm->extent·AU == 0){ *topo = Core·Tape·Topo·singleton; }else{ *topo = Core·Tape·Topo·segment; } return Core·Status·on_track; - } - - Local Core·Status Core·TM_NX_##CVT·extent(Core·TM_NX *tm ,extent_t *extent){ - Core·Tape·Topo topo; - Core·Status status = Core·TM_NX_##CVT·topo(tm ,&topo); - boolean good_topo = - (status == Core·Status·on_track) && (topo & Core·Tape·Topo·finite_nz) - ; - - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,extent ,Core·TM_NX_##CVT·Msg·extent); - Core·Guard·fg.check( - &chk ,0 ,good_topo - ,"Tape does not exist or topology does not have an extent." - ); - Core·Guard·if_return(chk); - #endif - - if(!good_topo) return Core·Status·derailed; - *extent = tm->array.extent; - return Core·Status·on_track; - } + } - Local Core·Status Core·TM_NX_##CVT·mount_pe( - Core·TM_NX *tm ,void *position ,extent_t extent - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,position ,"given NULL position"); - Core·Guard·if_return(chk); - #endif - tm->array.position = position->array.address; - tm->array.extent = extent; - return Core·Status·on_track; - } + // extent·AU is an AU index + Local Core·Status Core·TM_NX·extent·AU(Core·TM_NX *tm ,extent·AU *extent·AU){ + Core·Tape·Topo topo; + Core·Status status = Core·TM_NX_##CVT·topo(tm ,&topo); + boolean good_topo = + (status == Core·Status·on_track) && (topo & Core·Tape·Topo·finite_nz) + ; - Local Core·Status Core·TM_NX_##CVT·mount_pp( - Core·TM_NX *tm ,void *position_left ,void *position_right - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - bool good_address = position_left && position_left->address; - Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); - good_address = position_right && position_right->address; - Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); - if(position_left && position_right){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,extent·AU ,Core·TM_NX·Msg·extent·AU); Core·Guard·fg.check( - &chk ,1 ,position_right->array.address >= position_left->array.address - ,"position_right < position_left" - ); - } - Core·Guard·if_return(chk); - #endif - - extent_t computed_extent = - (extent_t)( - (uintptr_t)position_right->array.address - (uintptr_t)position_left->array.address + &chk ,0 ,good_topo + ,"Tape does not exist or topology does not have an extent·AU." ); + Core·Guard·if_return(chk); + #endif - return Core·TM_NX_##CVT·mount_pe(tm ,position_left ,computed_extent); - } + if(!good_topo) return Core·Status·derailed; + *extent·AU = tm->array.extent·AU; + return Core·Status·on_track; + } - Local Core·Status Core·TM_NX_##CVT·largest_aligned_64( - Core·TM_NX *outer ,Core·TM_NX *inner_64 - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); - if(outer){ - Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,inner_64 ,"given NULL inner TM"); - Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"outer TM has NULL position"); - Core·Guard·if_return(chk); - #endif + #endif - uintptr_t p0 = (uintptr_t)outer->array.position; - uintptr_t p1 = (uintptr_t)outer->array.position + outer->array.extent; + #ifdef CVT - CVT *p0_64 = (CVT *)( (p0 + 0x7) & ~(uintptr_t)0x7 ); - CVT *p1_64 = (CVT *)( (p1 - 0x7) & ~(uintptr_t)0x7 ); + //----------------------------------- + // Area functions within Core·TM_NX_##CVT - if(p1_64 < p0_64){ - inner_64->array.position = NULL; - inner_64->array.extent = 0; - return Core·Status·derailed; + Core·Status Core·TM_NX_##CVT·topo(Core·TM_NX *tm ,Core·Tape·Topo *topo){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,topo ,"topo ptr is NULL, so nowhere to put result"); + Core·Guard·if_return(chk); + #endif + if(tm->extent·AU == 0){ + *topo = Core·Tape·Topo·singleton; + }else{ + *topo = Core·Tape·Topo·segment; + } + return Core·Status·on_track; } - inner_64->array.position = p0_64; - inner_64->array.extent = (extent_t)(p1_64 - p0_64); - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·mount_pe( + Core·TM_NX *tm ,void *position ,extent·AU extent·AU + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,position ,"given NULL position"); + Core·Guard·if_return(chk); + #endif + tm->array.position = position->array.address; + tm->array.extent·AU = extent·AU; + return Core·Status·on_track; + } - Local Core·Status Core·TM_NX_##CVT·encloses_pt( - Core·TM_NX *tm ,void *a ,bool *result - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - bool good_address = a && a->address; - Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); - Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·mount_pp( + Core·TM_NX *tm ,void *position_left ,void *position_right + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + bool good_address = position_left && position_left->address; + Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); + good_address = position_right && position_right->address; + Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); + if(position_left && position_right){ + Core·Guard·fg.check( + &chk ,1 ,position_right->array.address >= position_left->array.address + ,"position_right < position_left" + ); + } + Core·Guard·if_return(chk); + #endif - *result = - (a->array.address >= tm->array.position) - && (a->array.address <= tm->array.position + tm->array.extent); + extent·AU computed_extent·AU = + (extent·AU)( + (uintptr_t)position_right->array.address - (uintptr_t)position_left->array.address + ); - return Core·Status·on_track; - } + return Core·TM_NX_##CVT·mount_pe(tm ,position_left ,computed_extent·AU); + } - Local Core·Status Core·TM_NX_##CVT·encloses_pt_strictly( - Core·TM_NX *tm ,void *a ,bool *result - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - bool good_address = a && a->address; - Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); - Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·largest_aligned_64( + Core·TM_NX *outer ,Core·TM_NX *inner_64 + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); + if(outer){ + Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,inner_64 ,"given NULL inner TM"); + Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"outer TM has NULL position"); + Core·Guard·if_return(chk); + #endif - *result = - (a->array.address > tm->array.position) - && (a->array.address < tm->array.position + tm->array.extent); + uintptr_t p0 = (uintptr_t)outer->array.position; + uintptr_t p1 = (uintptr_t)outer->array.position + outer->array.extent·AU; - return Core·Status·on_track; - } + CVT *p0_64 = (CVT *)( (p0 + 0x7) & ~(uintptr_t)0x7 ); + CVT *p1_64 = (CVT *)( (p1 - 0x7) & ~(uintptr_t)0x7 ); - Local Core·Status Core·TM_NX_##CVT·encloses_tm( - Core·TM_NX *outer ,Core·TM_NX *inner ,bool *flag - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); - if(outer){ - Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,inner ,"given NULL inner TM"); - if(inner){ - Core·Guard·fg.check(&chk ,1 ,inner->array.position ,"NULL inner->array.position"); + if(p1_64 < p0_64){ + inner_64->array.position = NULL; + inner_64->array.extent·AU = 0; + return Core·Status·derailed; } - Core·Guard·fg.check(&chk ,1 ,flag ,"given NULL flag pointer"); - Core·Guard·if_return(chk); - #endif - *flag = - (inner->array.position >= outer->array.position) - && (inner->array.position + inner->array.extent <= outer->array.position + outer->array.extent); + inner_64->array.position = p0_64; + inner_64->array.extent·AU = (extent·AU)(p1_64 - p0_64); + return Core·Status·on_track; + } - return Core·Status·on_track; - } - Local Core·Status Core·TM_NX_##CVT·encloses_tm_strictly( - Core·TM_NX *outer ,Core·TM_NX *inner ,bool *flag - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); - if(outer){ - Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,inner ,"given NULL inner TM"); - if(inner){ - Core·Guard·fg.check(&chk ,1 ,inner->array.position ,"NULL inner->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,flag ,"given NULL flag pointer"); - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·encloses_pt( + Core·TM_NX *tm ,void *a ,bool *result + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + bool good_address = a && a->address; + Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); + Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); + Core·Guard·if_return(chk); + #endif - *flag = - (inner->array.position > outer->array.position) - && (inner->array.position + inner->array.extent < outer->array.position + outer->array.extent); + *result = + (a->array.address >= tm->array.position) + && (a->array.address <= tm->array.position + tm->array.extent·AU); - return Core·Status·on_track; - } + return Core·Status·on_track; + } - Local Core·Status Core·TM_NX_##CVT·overlap( - Core·TM_NX *a ,Core·TM_NX *b ,bool *result - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,a ,"given NULL TM A"); - if(a){ - Core·Guard·fg.check(&chk ,1 ,a->array.position ,"NULL a->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,b ,"given NULL TM B"); - if(b){ - Core·Guard·fg.check(&chk ,1 ,b->array.position ,"NULL b->array.position"); - } - Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·encloses_pt_strictly( + Core·TM_NX *tm ,void *a ,bool *result + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + bool good_address = a && a->address; + Core·Guard·fg.check(&chk ,1 ,good_address ,Core·TM_NX_##CVT·Msg·address); + Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); + Core·Guard·if_return(chk); + #endif - *result = - (a->array.position < b->array.position + b->array.extent) - && (b->array.position < a->array.position + a->array.extent); + *result = + (a->array.address > tm->array.position) + && (a->array.address < tm->array.position + tm->array.extent·AU); - return Core·Status·on_track; - } + return Core·Status·on_track; + } + Local Core·Status Core·TM_NX_##CVT·encloses_tm( + Core·TM_NX *outer ,Core·TM_NX *inner ,bool *flag + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); + if(outer){ + Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,inner ,"given NULL inner TM"); + if(inner){ + Core·Guard·fg.check(&chk ,1 ,inner->array.position ,"NULL inner->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,flag ,"given NULL flag pointer"); + Core·Guard·if_return(chk); + #endif - //----------------------------------- - // base Tape Machine operations + *flag = + (inner->array.position >= outer->array.position) + && (inner->array.position + inner->array.extent·AU <= outer->array.position + outer->array.extent·AU); - Local Core·Status Core·TM_NX_##CVT·mount(Core·TM_NX *tm){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,tm->array.position ,Core·TM_NX_##CVT·Msg·position); - Core·Guard·if_return(chk); - #endif + return Core·Status·on_track; + } - tm->array.hd = tm->array.position; - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·encloses_tm_strictly( + Core·TM_NX *outer ,Core·TM_NX *inner ,bool *flag + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,outer ,"given NULL outer TM"); + if(outer){ + Core·Guard·fg.check(&chk ,1 ,outer->array.position ,"NULL outer->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,inner ,"given NULL inner TM"); + if(inner){ + Core·Guard·fg.check(&chk ,1 ,inner->array.position ,"NULL inner->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,flag ,"given NULL flag pointer"); + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·dismount(Core·TM_NX *tm){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·if_return(chk); - #endif + *flag = + (inner->array.position > outer->array.position) + && (inner->array.position + inner->array.extent·AU < outer->array.position + outer->array.extent·AU); - // Reset head position upon dismount - tm->array.hd = NULL; - return Core·Status·on_track; - } + return Core·Status·on_track; + } - Local Core·Status Core·TM_NX_##CVT·status( - Core·TM_NX *tm ,Core·TM·Head·Status *status - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,status ,"given NULL status pointer"); - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·overlap( + Core·TM_NX *a ,Core·TM_NX *b ,bool *result + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,a ,"given NULL TM A"); + if(a){ + Core·Guard·fg.check(&chk ,1 ,a->array.position ,"NULL a->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,b ,"given NULL TM B"); + if(b){ + Core·Guard·fg.check(&chk ,1 ,b->array.position ,"NULL b->array.position"); + } + Core·Guard·fg.check(&chk ,1 ,result ,"given NULL result pointer"); + Core·Guard·if_return(chk); + #endif + + *result = + (a->array.position < b->array.position + b->array.extent·AU) + && (b->array.position < a->array.position + a->array.extent·AU); - if(tm->array.hd == NULL){ - *status = Core·TM·Head·Status·not_on_tape; - }else if(tm->array.hd == tm->array.position){ - *status = Core·TM·Head·Status·origin; - }else if(tm->array.hd == tm->array.position + tm->array.extent){ - *status = Core·TM·Head·Status·rightmost; - }else{ - *status = Core·TM·Head·Status·interim; + return Core·Status·on_track; } - return Core·Status·on_track; - } - Local Core·Status Core·TM_NX_##CVT·can_read(Core·TM_NX *tm ,bool *flag){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - if(tm){ - // All initialized TM_NX_##CVT have an initialized position - // Maybe this catches that the machine is uninitialized? + //----------------------------------- + // base Tape Machine operations + + Local Core·Status Core·TM_NX_##CVT·mount(Core·TM_NX *tm){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); Core·Guard·fg.check(&chk ,1 ,tm->array.position ,Core·TM_NX_##CVT·Msg·position); - } - Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); - Core·Guard·if_return(chk); - #endif - *flag = tm && tm->array.hd != NULL; - return Core·Status·on_track; - } + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·on_origin( - Core·TM_NX *tm ,bool *flag - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,tm && tm->array.position ,Core·TM_NX_##CVT·Msg·position); - Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); - Core·Guard·if_return(chk); - #endif + tm->array.hd = tm->array.position; + return Core·Status·on_track; + } - *flag = (tm->array.hd == tm->array.position); - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·dismount(Core·TM_NX *tm){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·on_rightmost( - Core·TM_NX *tm ,bool *flag - ){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); - Core·Guard·fg.check(&chk ,1 ,tm && tm->array.position ,Core·TM_NX_##CVT·Msg·position); - Core·Guard·if_return(chk); - #endif + // Reset head position upon dismount + tm->array.hd = NULL; + return Core·Status·on_track; + } - *flag = tm->array.hd == (tm->array.position + tm->array.extent); - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·status( + Core·TM_NX *tm ,Core·TM·Head·Status *status + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,status ,"given NULL status pointer"); + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·copy_datum(Core·TM_NX *tm_read ,Core·TM_NX *tm_write){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm_read ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,tm_write ,Core·TM_NX_##CVT·Msg·tm); - if(tm_read && tm_write){ - Core·Guard·fg.check( - &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm_read ,tm_read->array.hd) - ,"Source address is outside tape bounds" - ); - Core·Guard·fg.check( - &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm_write ,tm_write->array.hd) - ,"Destination address is outside tape bounds" - ); + if(tm->array.hd == NULL){ + *status = Core·TM·Head·Status·not_on_tape; + }else if(tm->array.hd == tm->array.position){ + *status = Core·TM·Head·Status·origin; + }else if(tm->array.hd == tm->array.position + tm->array.extent·AU){ + *status = Core·TM·Head·Status·rightmost; + }else{ + *status = Core·TM·Head·Status·interim; } - Core·Guard·if_return(chk); - #endif - *(tm_write->array.hd) = *(tm_read->array.hd); - return Core·Status·on_track; - } + return Core·Status·on_track; + } - Local Core·Status Core·TM_NX_##CVT·read(Core·TM_NX *tm ,void *read_pt){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - if(tm) Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,Core·TM_NX_##CVT·Msg·address); - if(tm && tm->array.hd){ - Core·Guard·fg.check( - &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm ,tm->array.hd) - ,"Given address is outside tape bounds" - ); - } - Core·Guard·if_return(chk); - #endif + Local Core·Status Core·TM_NX_##CVT·can_read(Core·TM_NX *tm ,bool *flag){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + if(tm){ + // All initialized TM_NX_##CVT have an initialized position + // Maybe this catches that the machine is uninitialized? + Core·Guard·fg.check(&chk ,1 ,tm->array.position ,Core·TM_NX_##CVT·Msg·position); + } + Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); + Core·Guard·if_return(chk); + #endif + *flag = tm && tm->array.hd != NULL; + return Core·Status·on_track; + } - *(CVT *)read_pt = *(tm_array.hd); - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·on_origin( + Core·TM_NX *tm ,bool *flag + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,tm && tm->array.position ,Core·TM_NX_##CVT·Msg·position); + Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·write(Core·TM_NX *tm ,void *write_pt){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - if(tm) Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,Core·TM_NX_##CVT·Msg·address); - if(tm && tm->array.hd){ - Core·Guard·fg.check( - &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm ,tm->array.hd) - ,"Given address is outside tape bounds" - ); - } - Core·Guard·if_return(chk); - #endif + *flag = (tm->array.hd == tm->array.position); + return Core·Status·on_track; + } - *(tm->array.hd) = *(CVT *)write_pt; - return Core·Status·on_track; - } + Local Core·Status Core·TM_NX_##CVT·on_rightmost( + Core·TM_NX *tm ,bool *flag + ){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,flag ,Core·TM_NX_##CVT·Msg·flag); + Core·Guard·fg.check(&chk ,1 ,tm && tm->array.position ,Core·TM_NX_##CVT·Msg·position); + Core·Guard·if_return(chk); + #endif - Local Core·Status Core·TM_NX_##CVT·rewind(Core·TM_NX *tm){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - if(tm){ - Core·Guard·fg.check(&chk ,1 ,tm->array.position ,Core·TM_NX_##CVT·Msg·position); - } - Core·Guard·if_return(chk); - #endif - tm->array.hd = tm->array.position; - return Core·Status·on_track; - } + *flag = tm->array.hd == (tm->array.position + tm->array.extent·AU); + return Core·Status·on_track; + } - Core·Status Core·TM_NX_##CVT·step(Core·TM_NX *tm){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,tm->array.position ,"step requested on unbound machine"); - Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,"step requested but tape not mounted"); - Core·Guard·if_return(chk); - #endif - if( tm->array.hd < tm->array.position + tm->array.extent ){ - tm->array.hd++; + Local Core·Status Core·TM_NX_##CVT·copy_datum(Core·TM_NX *tm_read ,Core·TM_NX *tm_write){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm_read ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,tm_write ,Core·TM_NX_##CVT·Msg·tm); + if(tm_read && tm_write){ + Core·Guard·fg.check( + &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm_read ,tm_read->array.hd) + ,"Source address is outside tape bounds" + ); + Core·Guard·fg.check( + &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm_write ,tm_write->array.hd) + ,"Destination address is outside tape bounds" + ); + } + Core·Guard·if_return(chk); + #endif + + *(tm_write->array.hd) = *(tm_read->array.hd); return Core·Status·on_track; } - return Core·Status·derailed; // Stepping beyond tape bounds - } - Core·Status Core·TM_NX_##CVT·step_left(Core·TM_NX *tm){ - #ifdef Core·Debug - Core·Guard·init_count(chk); - Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); - Core·Guard·fg.check(&chk ,1 ,tm->array.position - ,"step_left requested on unbound machine" - ); - Core·Guard·fg.check(&chk ,1 ,tm->array.hd - ,"step_left requested with no mounted head" - ); - Core·Guard·if_return(chk); - #endif - if( tm->array.hd > tm->array.position ){ - tm->array.hd--; + Local Core·Status Core·TM_NX_##CVT·read(Core·TM_NX *tm ,void *read_pt){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + if(tm) Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,Core·TM_NX_##CVT·Msg·address); + if(tm && tm->array.hd){ + Core·Guard·fg.check( + &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm ,tm->array.hd) + ,"Given address is outside tape bounds" + ); + } + Core·Guard·if_return(chk); + #endif + + *(CVT *)read_pt = *(tm_array.hd); return Core·Status·on_track; } - return Core·Status·derailed; // Stepping beyond left boundary - } - // step_right is a synonym for step + Local Core·Status Core·TM_NX_##CVT·write(Core·TM_NX *tm ,void *write_pt){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + if(tm) Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,Core·TM_NX_##CVT·Msg·address); + if(tm && tm->array.hd){ + Core·Guard·fg.check( + &chk ,1 ,Core·TM_NX_##CVT·area.encloses_pt(tm ,tm->array.hd) + ,"Given address is outside tape bounds" + ); + } + Core·Guard·if_return(chk); + #endif + *(tm->array.hd) = *(CVT *)write_pt; + return Core·Status·on_track; + } - //---------------------------------------- - // Initialization for Core·TM_NX_##CVT·fg - - Local Core·TM_NX·FG Core·TM_NX_##CVT·fg = { - .mount = Core·TM_NX_##CVT·mount - ,.dismount = Core·TM_NX_##CVT·dismount - - ,.status = Core·TM_NX_##CVT·status - ,.can_read = Core·TM_NX_##CVT·can_read - ,.on_origin = Core·TM_NX_##CVT·on_origin - ,.on_rightmost = Core·TM_NX_##CVT·on_rightmost - - ,.read = Core·TM_NX_##CVT·read - ,.write = Core·TM_NX_##CVT·write - - ,.rewind = Core·TM_NX_##CVT·rewind - ,.step = Core·TM_NX_##CVT·step - ,.step_left = Core·TM_NX_##CVT·step_left - ,.step_right = Core·TM_NX_##CVT·step_right // Synonym for step - - ,.area = { - .topo = Core·TM_NX_##CVT·topo - ,.extent = Core·TM_NX_##CVT·extent - - ,.mount_pe = Core·TM_NX_##CVT·mount_pe - ,.mount_pp = Core·TM_NX_##CVT·mount_pp - ,.largest_aligned_64 = Core·TM_NX_##CVT·largest_aligned_64 - - ,.encloses_pt = Core·TM_NX_##CVT·encloses_pt - ,.encloses_pt_strictly = Core·TM_NX_##CVT·encloses_pt_strictly - ,.encloses_tm = Core·TM_NX_##CVT·encloses_tm - ,.encloses_tm_strictly = Core·TM_NX_##CVT·encloses_tm_strictly - ,.overlap = Core·TM_NX_##CVT·overlap + Local Core·Status Core·TM_NX_##CVT·rewind(Core·TM_NX *tm){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + if(tm){ + Core·Guard·fg.check(&chk ,1 ,tm->array.position ,Core·TM_NX_##CVT·Msg·position); + } + Core·Guard·if_return(chk); + #endif + tm->array.hd = tm->array.position; + return Core·Status·on_track; + } + + Core·Status Core·TM_NX_##CVT·step(Core·TM_NX *tm){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,tm->array.position ,"step requested on unbound machine"); + Core·Guard·fg.check(&chk ,1 ,tm->array.hd ,"step requested but tape not mounted"); + Core·Guard·if_return(chk); + #endif + if( tm->array.hd < tm->array.position + tm->array.extent·AU ){ + tm->array.hd++; + return Core·Status·on_track; + } + return Core·Status·derailed; // Stepping beyond tape bounds } - }; + + Core·Status Core·TM_NX_##CVT·step_left(Core·TM_NX *tm){ + #ifdef Core·Debug + Core·Guard·init_count(chk); + Core·Guard·fg.check(&chk ,1 ,tm ,Core·TM_NX_##CVT·Msg·tm); + Core·Guard·fg.check(&chk ,1 ,tm->array.position + ,"step_left requested on unbound machine" + ); + Core·Guard·fg.check(&chk ,1 ,tm->array.hd + ,"step_left requested with no mounted head" + ); + Core·Guard·if_return(chk); + #endif + if( tm->array.hd > tm->array.position ){ + tm->array.hd--; + return Core·Status·on_track; + } + return Core·Status·derailed; // Stepping beyond left boundary + } + + // step_right is a synonym for step + + + //---------------------------------------- + // Initialization for Core·TM_NX_##CVT·fg + + Local Core·TM_NX·FG Core·TM_NX_##CVT·fg = { + .mount = Core·TM_NX_##CVT·mount + ,.dismount = Core·TM_NX_##CVT·dismount + + ,.status = Core·TM_NX_##CVT·status + ,.can_read = Core·TM_NX_##CVT·can_read + ,.on_origin = Core·TM_NX_##CVT·on_origin + ,.on_rightmost = Core·TM_NX_##CVT·on_rightmost + + ,.read = Core·TM_NX_##CVT·read + ,.write = Core·TM_NX_##CVT·write + + ,.rewind = Core·TM_NX_##CVT·rewind + ,.step = Core·TM_NX_##CVT·step + ,.step_left = Core·TM_NX_##CVT·step_left + ,.step_right = Core·TM_NX_##CVT·step_right // Synonym for step + + ,.area = { + .topo = Core·TM_NX_##CVT·topo + ,.extent·AU = Core·TM_NX_##CVT·extent·AU + + ,.mount_pe = Core·TM_NX_##CVT·mount_pe + ,.mount_pp = Core·TM_NX_##CVT·mount_pp + ,.largest_aligned_64 = Core·TM_NX_##CVT·largest_aligned_64 + + ,.encloses_pt = Core·TM_NX_##CVT·encloses_pt + ,.encloses_pt_strictly = Core·TM_NX_##CVT·encloses_pt_strictly + ,.encloses_tm = Core·TM_NX_##CVT·encloses_tm + ,.encloses_tm_strictly = Core·TM_NX_##CVT·encloses_tm_strictly + ,.overlap = Core·TM_NX_##CVT·overlap + } + }; + + #endif //---------------------------------------- // Map implementation @@ -1128,10 +1121,10 @@ Core·Map·Completion Core·map(TM_MX *tm_read ,TM_MX *tm_write ,Core·Map·Fn f Core·Map·Fn Core·Map·Map·ByteByByte·write_available; Local Core·Map·Fn Core·Map·##CVT_by_##CVT(){ - if(Core·TM_NX_##CVT·extent(Core·tf.copy.read) == Core·TM_NX_##CVT·extent(Core·tf.copy.write)) + if(Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) == Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)) return Core·Map·ByteByByte·perfect_fit; - if(Core·TM_NX_##CVT·extent(Core·tf.copy.read) > Core·TM_NX_##CVT·extent(Core·tf.copy.write)) + if(Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) > Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)) return Core·Map·ByteByByte·read_surplus; return Core·Map·ByteByByte·write_available; @@ -1273,10 +1266,10 @@ Core·Map·Completion Core·map(TM_MX *tm_read ,TM_MX *tm_write ,Core·Map·Fn f // Hex Encoding: Initialize Map Local Core·Map·Fn Core·Map·write_hex(){ - if(Core·TM_NX_##CVT·extent(Core·tf.copy.read) == (Core·TM_NX_##CVT·extent(Core·tf.copy.write) >> 1)){ + if(Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) == (Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write) >> 1)){ return Core·Map·WriteHex·perfect_fit; } - if(Core·TM_NX_##CVT·extent(Core·tf.copy.read) > (Core·TM_NX_##CVT·extent(Core·tf.copy.write) >> 1)){ + if(Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) > (Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write) >> 1)){ return Core·Map·WriteHex·read_surplus; } return Core·Map·WriteHex·write_available; @@ -1361,10 +1354,10 @@ Core·Map·Completion Core·map(TM_MX *tm_read ,TM_MX *tm_write ,Core·Map·Fn f Core·Map·Fn Core·Map·ReadHex·write_available; Local Core·Map·Fn Core·Map·read_hex(){ - if((Core·TM_NX_##CVT·extent(Core·tf.copy.read) >> 1) == Core·TM_NX_##CVT·extent(Core·tf.copy.write)){ + if((Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) >> 1) == Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)){ return Core·Map·ReadHex·perfect_fit; } - if((Core·TM_NX_##CVT·extent(Core·tf.copy.read) >> 1) > Core·TM_NX_##CVT·extent(Core·tf.copy.write)){ + if((Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) >> 1) > Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)){ return Core·Map·ReadHex·read_surplus; } return Core·Map·ReadHex·write_available; @@ -1428,10 +1421,10 @@ Core·Map·Completion Core·map(TM_MX *tm_read ,TM_MX *tm_write ,Core·Map·Fn f Core·Map·Fn Core·Map·ReadHex·write_available; Local Core·Map·Fn Core·Map·read_hex(){ - if((Core·TM_NX_##CVT·extent(Core·tf.copy.read) >> 1) == Core·TM_NX_##CVT·extent(Core·tf.copy.write)){ + if((Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) >> 1) == Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)){ return Core·Map·ReadHex·perfect_fit; } - if((Core·TM_NX_##CVT·extent(Core·tf.copy.read) >> 1) > Core·TM_NX_##CVT·extent(Core·tf.copy.write)){ + if((Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.read) >> 1) > Core·TM_NX_##CVT·extent_by·AU(Core·tf.copy.write)){ return Core·Map·ReadHex·read_surplus; } return Core·Map·ReadHex·write_available; @@ -1486,73 +1479,7 @@ Core·Map·Completion Core·map(TM_MX *tm_read ,TM_MX *tm_write ,Core·Map·Fn f return NULL; } - //---------------------------------------- - // Initialization Blocks - //---------------------------------------- - // Tableaux - - Core·TableauFace tf = { - .copy = { - .read = NULL - ,.write = NULL - ,.read_fn_8 = Core·TM_NX_##CVT·read_8_fwd - ,.read_fn_64 = Core·TM_NX_##CVT·read_64_fwd - ,.read_pt = NULL - ,.write_pt = NULL - ,.status = Core·Map·Status·uninitialized - } - }; - - Core·TableauLocal tl = { - .copy_64 = { - .area_64 = {NULL ,0} - } - }; - - Core·M m = { - .Area·init_pe = Core·TM_NX_##CVT·init_pe - ,.Area·init_pp = Core·TM_NX_##CVT·init_pp - ,.Area·set_position = Core·TM_NX_##CVT·set_position - ,.Area·set_position_left = Core·TM_NX_##CVT·set_position - ,.Area·set_position_right = Core·TM_NX_##CVT·set_position_right - ,.Area·set_extent = Core·TM_NX_##CVT·set_extent - ,.Area·position = Core·TM_NX_##CVT·position - ,.Area·position_left = Core·TM_NX_##CVT·position - ,.Area·position_right = Core·TM_NX_##CVT·position_right - ,.Area·extent = Core·TM_NX_##CVT·extent - ,.Area·length_Kung = Core·TM_NX_##CVT·length_Kung - ,.Area·empty = Core·TM_NX_##CVT·empty - - ,.Area·encloses_pt = Core·TM_NX_##CVT·encloses_pt - ,.Area·encloses_pt_strictly = Core·TM_NX_##CVT·encloses_pt_strictly - ,.Area·encloses_area = Core·TM_NX_##CVT·encloses_area - ,.Area·encloses_area_strictly = Core·TM_NX_##CVT·encloses_area_strictly - ,.Area·overlap = Core·TM_NX_##CVT·overlap - ,.Area·largest_aligned_64 = Core·TM_NX_##CVT·largest_aligned_64 - ,.Area·complement = Core·TM_NX_##CVT·complement - - ,.Area·read_8_zero = Core·TM_NX_##CVT·read_8_zero - ,.Area·read_8_fwd = Core·TM_NX_##CVT·read_8_fwd - ,.Area·read_8_rev = Core·TM_NX_##CVT·read_8_rev - ,.Area·read_64_zero = Core·TM_NX_##CVT·read_64_zero - ,.Area·read_64_fwd = Core·TM_NX_##CVT·read_64_fwd - ,.Area·read_64_rev = Core·TM_NX_##CVT·read_64_rev - - ,.is_aligned_on_64 = Core·is_aligned_on_64 - ,.floor_64 = Core·floor_64 - ,.ceiling_64 = Core·ceiling_64 - ,.offset_8 = Core·offset_8 - ,.offset_64 = Core·offset_64 - - ,.byte_to_hex = Core·byte_to_hex - ,.hex_to_byte = Core·hex_to_byte - - ,.copy = Core·map - ,.Map·##CVT_by_##CVT = Core·Map·##CVT_by_##CVT - ,.Map·write_hex = Core·Map·write_hex - ,.Map·read_hex = Core·Map·read_hex - }; #endif // LOCAL diff --git "a/developer/cc\360\237\226\211/Core_scratch.c" "b/developer/cc\360\237\226\211/Core_scratch.c" deleted file mode 100644 index cfaa805..0000000 --- "a/developer/cc\360\237\226\211/Core_scratch.c" +++ /dev/null @@ -1,891 +0,0 @@ - //---------------------------------------- - // TM_NX array - - Local Core·TM_NX_Array·Topo Core·TM_NX·topo(Core·TM_NX *tape){ - if(!tape) return Core·Area·Topo·nonexistent; - if(!tape->position) return Core·Area·Topo·empty; - if(!tape->extent) return Core·Area·Topo·singleton; - return Core·Area·Topo·segment; - } - - Local Core·Status Core·TM_NX_Array·extent(Core·TM_NX *tape ,extent_t *write_pt){ - #ifdef Core·Debug - uint error = 0; - if(!tape){ - fprintf(stderr,"Core·TM_NX·extent:: given NULL tape"); - error++; - }else if(!tape->position){ - fprintf(stderr,"Core·TM_NX·extent:: extent requested for a marked as empty tape"); - error++; - } - if(!write_pt){ - fprintf(stderr,"Core·TM_NX·extent:: given NULL write_pt"); - error++; - } - if(error) return Core·Status·derailed; - #endif - return tape->extent; Local Core·TM_NX·FG Core·TM_NX·fg = { - - } - - Local bool Core·TM_MX_Array·encloses_pt(Core·Area *area ,AU *pt); - - // a must be on tape - Local Core·Status Core·TM_NX·is_leftmost( - Core·TM_NX *tape ,Core·TM_NX·Address *a ,bool *result - ){ - #ifdef Core·Debug - uint error = 0; - if(!tape){ - fprintf(stderr,"Core·TM_NX·is_leftmost:: given NULL tape"); - error++; - }else if(!tape->position){ - fprintf(stderr,"Core·TM_NX·is_leftmost:: leftmost requested of empty tape"); - error++; - } - if(!result){ - fprintf(stderr,"Core·TM_NX·is_leftmost:: NULL result pointer"); - error++; - } - // making this a warning, but by spec it must be true - if(!a){ - fprintf(stderr,"Core·TM_NX·is_leftmost:: given NULL address"); - }else if( !Core·TM_MX_Array·encloses_pt((Core·Area *)tape ,a->address) ){ - fprintf(stderr,"Core·TM_NX·is_leftmost:: address is outside tape bounds"); - } - if(error) return Core·Status·derailed; - #endif - result = tape->position == a; - return Core·Status·on_track; - } - - Local Core·Status Core·TM_NX·is_rightmost( - Core·TM_NX *tape ,Core·TM_NX·Address *a ,bool *result - ){ - #ifdef Core·Debug - uint error = 0; - if(!tape){ - fprintf(stderr,"Core·TM_NX·is_rightmost:: given NULL tape"); - error++; - }else if(!tape->position){ - fprintf(stderr,"Core·TM_NX·is_rightmost:: rightmost requested of empty tape"); - error++; - } - if(!result){ - fprintf(stderr,"Core·TM_NX·is_rightmost:: NULL result pointer"); - error++; - } - // making this a warning, but by spec it must be true - if(!a){ - fprintf(stderr,"Core·TM_NX·is_rightmost:: given NULL address"); - }else if( !Core·TM_MX_Array·encloses_pt((Core·Area *)tape ,a->address) ){ - fprintf(stderr,"Core·TM_NX·is_rightmost:: address is outside tape bounds"); - } - if(error) return Core·Status·derailed; - #endif - result = tape->position == tape->position + tape->extent; - return Core·Status·on_track; - } - - Local Core·Status Core·TM_NX·read( - Core·TM_NX *tape ,Core·TM_NX·Address *a ,Core·TM_NX·Remote *remote - ){ - #ifdef Core·Debug - uint error = 0; - if(!tape){ - fprintf(stderr,"Core·TM_NX·read:: given NULL tape"); - error++; - }else if(!tape->position){ - fprintf(stderr,"Core·TM_NX·read:: read requested for a marked as empty tape"); - error++; - } - if(!a){ - fprintf(stderr,"Core·TM_NX·read:: given NULL address"); - error++; - }else if( !Core·TM_MX_Array·encloses_pt((Core·Area *)tape ,a->address) ){ - fprintf(stderr,"Core·TM_NX·read:: address is outside tape bounds"); - error++; - } - if(!remote){ - fprintf(stderr,"Core·TM_NX·read:: given NULL remote"); - error++; - } - if(error) return Core·Status·derailed; - #endif - *(AU *)remote->address = *(AU *)a->address; - return Core·Status·on_track; - } - - Local Core·Status Core·TM_NX·write( - Core·TM_NX *tape ,Core·TM_NX·Address *a ,Core·TM_NX·Remote *r - ){ - #ifdef Core·Debug - uint error = 0; - if(!tape){ - fprintf(stderr,"Core·TM_NX·write:: given NULL tape"); - error++; - }else if(!tape->position){ - fprintf(stderr, - "Core·TM_NX·write:: write requested for a marked as empty tape" - ); - error++; - } - if(!a){ - fprintf(stderr,"Core·TM_NX·write:: given NULL address"); - error++; - }else if( !Core·TM_MX_Array·encloses_pt((Core·Area *)tape ,a->address) ){ - fprintf(stderr,"Core·TM_NX·write:: address is outside tape bounds"); - error++; - } - if(!r){ - fprintf(stderr,"Core·TM_NX·write:: given NULL remote"); - error++; - } - if(error) return Core·Status·derailed; - #endif - *(AU *)a->address = *(AU *)r->address; - return Core·Status·on_track; - } - - // area functions - - Local Core·Status Core·Area·set_position(Core·Area *area ,AU *new_position){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr, "Core·Area·set_position:: given NULL area"); - return Core·Status·derailed; - } - #endif - area->position = new_position; - return Core·Status·on_track; - } - - Local Core·Status Core·Area·set_extent(Core·Area *area ,extent_t extent){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr, "Core·Area·set_extent:: given NULL area"); - return Core·Status·derailed; - } - #endif - area->extent = extent; - return Core·Status·on_track; - } - - Local Core·Status Core·Area·set_position_right(Core·Area *area ,AU *new_position_right){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr, "Core·Area·set_position_right:: given NULL area"); - return Core·Status·derailed; - } - if(!area->position){ - fprintf(stderr, "Core·Area·set_position_right:: NULL position_left"); - return Core·Status·derailed; - } - #endif - return Core·Area·set_extent(area, new_position_right - area->position); - } - - Local Core·Status Core·Area·init_pe(Core·Area *area ,AU *position ,extent_t extent){ - Core·Status s1 = Core·Area·set_position(area, position); - Core·Status s2 = Core·Area·set_extent(area, extent); - return (s1 == Core·Status·derailed || s2 == Core·Status·derailed) ? Core·Status·derailed : Core·Status·on_track; - } - - Local Core·Status Core·Area·init_pp(Core·Area *area ,AU *position_left ,AU *position_right){ - Core·Status s1 = Core·Area·set_position(area, position_left); - Core·Status s2 = Core·Area·set_position_right(area, position_right); - return (s1 == Core·Status·derailed || s2 == Core·Status·derailed) ? Core·Status·derailed : Core·Status·on_track; - } - - // Read Properties - - Local bool Core·Area·empty(Core·Area *area){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·empty:: given NULL area"); - return true; - } - #endif - return area->position == NULL; - } - - Local Core·Status Core·Area·position(Core·Area *area ,AU **out_position){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·position:: given NULL area"); - *out_position = NULL; - return Core·Status·derailed; - } - #endif - *out_position = area->position; - return Core·Status·on_track; - } - - Local Core·Status Core·Area·position_right(Core·Area *area ,AU **out_position_right){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·position_right:: given NULL area"); - *out_position_right = NULL; - return Core·Status·derailed; - } - if(!area->position){ - fprintf(stderr,"Core·Area·position_right:: request for right position when left is NULL"); - *out_position_right = NULL; - return Core·Status·derailed; - } - #endif - *out_position_right = area->position + area->extent; - return Core·Status·on_track; - } - - Local Core·Status Core·Area·extent(Core·Area *area ,extent_t *out_extent){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·extent:: given NULL area"); - *out_extent = 0; - return Core·Status·derailed; - } - #endif - *out_extent = area->extent; - return Core·Status·on_track; - } - - Local Core·Status Core·Area·complement(Core·Area *area ,AU *r ,AU **out_complement){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·complement:: given NULL area"); - *out_complement = NULL; - return Core·Status·derailed; - } - if(!r){ - fprintf(stderr,"Core·Area·complement:: given NULL reference"); - *out_complement = NULL; - return Core·Status·derailed; - } - #endif - *out_complement = area->position + (area->position + area->extent - r); - return Core·Status·on_track; - } - - // Relationships - - Local bool Core·Area·encloses_pt(Core·Area *area ,AU *pt){ - return - (pt >= Core·Area·position(area)) - && (pt <= Core·Area·position_right(area)); - } - - Local bool Core·Area·encloses_pt_strictly(Core·Area *area ,AU *pt){ - return - (pt > Core·Area·position(area)) - && (pt < Core·Area·position_right(area)); - } - - Local bool Core·Area·encloses_area(Core·Area *outer ,Core·Area *inner){ - return - (Core·Area·position(inner) >= Core·Area·position(outer)) - && (Core·Area·position_right(inner) <= Core·Area·position_right(outer)); - } - - Local bool Core·Area·encloses_area_strictly(Core·Area *outer ,Core·Area *inner){ - return - (Core·Area·position(inner) > Core·Area·position(outer)) - && (Core·Area·position_right(inner) < Core·Area·position_right(outer)); - } - - Local bool Core·Area·overlap(Core·Area *area0 ,Core·Area *area1){ - return - Core·Area·position_right(area0) >= Core·Area·position(area1) - && Core·Area·position(area0) <= Core·Area·position_right(area1); - } - - // Aligned Containment - - Local Core·Status Core·Area·largest_aligned_64(Core·Area *outer ,Core·Area *inner_64){ - #ifdef Core·Debug - if(!outer || !inner_64){ - fprintf(stderr, "Core·Area·largest_aligned_64:: given NULL area(s)"); - return Core·Status·derailed; - } - #endif - - uintptr_t p0 = (uintptr_t)Core·Area·position(outer); - uintptr_t p1 = (uintptr_t)Core·Area·position_right(outer); - - AU *p0_64 = (AU *)( (p0 + 0x7) & ~(uintptr_t)0x7 ); - AU *p1_64 = (AU *)( (p1 - 0x7) & ~(uintptr_t)0x7 ); - - if(p1_64 < p0_64){ - return Core·Area·set_position(inner_64 ,NULL); - }else{ - return Core·Area·init_pp(inner_64 ,p0_64 ,p1_64); - } - } - - // functions given a TM_NX argument - - Local Core·TM_NX·FG Core·TM_NX_Array·fg = { - .mount = Core·TM_NX·mount - ,.dismount = Core·TM_NX·dismount - ,.rewind = Core·TM_NX·rewind - - ,.can_step = Core·TM_NX·can_step - ,.on_origin = Core·TM_NX·on_origin - ,.on_rightmost = Core·TM_NX·on_rightmost - - ,.extent = Core·TM_NX·extent - ,.can_read = Core·TM_NX·can_read - - ,.step = Core·TM_NX·step - ,.step_left = Core·TM_NX·step_left - ,.step_right = Core·TM_NX·step_right - ,.status = Core·TM_NX·status - - ,.area { - .topo = Core·TM_NX·topo - ,.extent = Core·TM_NX·extent - ,.is_origin = Core·TM_NX·is_origin - ,.is_rightmost = Core·TM_NX·is_rightmost - ,.read = Core·TM_NX·read - ,.write = Core·TM_NX·write - - ,.init_pe = Core·TM_NX·init_pe - ,.init_pp = Core·TM_NX·init_pp - ,.set_position = Core·TM_NX·set_position - ,.set_position_left = Core·TM_NX·set_position - ,.set_position_right = Core·TM_NX·set_position_right - ,.set_extent = Core·TM_NX·set_extent - - ,.position = Core·TM_NX·position - ,.position_left = Core·TM_NX·position // synonym - ,.position_right = Core·TM_NX·position_right - - ,.complement = Core·TM_NX·complement - - ,.encloses_pt = Core·TM_NX·encloses_pt - ,.encloses_pt_strictly = Core·TM_NX·encloses_pt_strictly - ,.encloses_tm = Core·TM_NX·encloses_tm - ,.encloses_tm_strictly = Core·TM_NX·encloses_tm_strictly - ,.overlap = Core·TM_NX·overlap - ,.largest_aligned_64 = Core·TM_NX·largest_aligned_64 - }; - - }; - - -================================================================================ - //---------------------------------------- - // Tape model - - typedef struct Core·Tape·Address; - typedef struct Core·Tape·Remote; - typedef struct Core·Tape; - - typedef enum{ - Core·Tape·Topo·mu - ,Core·Tape·Topo·nonexistent // pointer to tape is NULL - ,Core·Tape·Topo·empty // tape has no cells - ,Core·Tape·Topo·singleton // extent is zero - ,Core·Tape·Topo·segment // finite non-singleton tape - ,Core·Tape·Topo·circle // initial location recurs - ,Core·Tape·Topo·tail_cyclic // other than initial location recurs - ,Core·Tape·Topo·infinite // exists, not empty, no cycle, no rightmost - }Core·Tape·Topo; - - const Core·Tape·Topo Core·Tape·Topo·void = - Core·Tape·Topo·nonexistent - | Core·Tape·Topo·empty - ; - - const Core·Tape·Topo Core·Tape·Topo·can_read = - Core·Tape·Topo·singleton - | Core·Tape·Topo·segment - | Core·Tape·Topo·circle - | Core·Tape·Topo·cyclic - | Core·Tape·Topo·infinite - ; - - const Core·Tape·Topo Core·Tape·Topo·bounded = - Core·Tape·Topo·singleton - | Core·Tape·Topo·segment - ; - - const Core·Tape·Topo Core·Tape·Topo·tail_cyclic = - Core·Tape·Topo·circle - | Core·Tape·Topo·tail_cyclic - ; - - typedef struct{ - Core·Tape·Topo (*topo)(Core·Tape *tape); - Core·Tape·Extent·Status (*extent)(Core·Tape *tape ,extent_t *extent_pt); - - Core·Status (*origin)(Core·Tape *tape ,Core·Tape·Address * ,bool *result); - Core·Status (*rightmost)(Core·Tape *tape ,Core·Tape·Address * ,bool *result); - - Core·Status (*is_leftmot)(Core·Tape *tape ,Core·Tape·Address * ,bool *result); - Core·Status (*is_rightmost)(Core·Tape *tape ,Core·Tape·Address * ,bool *result); - Core·Status (*read)(Core·Tape *tape ,Core·Tape·Address *,Core·Tape·Remote *); - Core·Status (*write)(Core·Tape *tape ,Core·Tape·Address * ,Core·Tape·Remote *); - }Core·Tape·FG; - - // a default implementation based on an array - Core·Tape·FG Core·Tape·fg; - - //---------------------------------------- - // Area model - - typedef struct Core·Area; - - - // addresses must be on the tape/area - typedef struct{ - // area versions of the functions given tape - Core·Tape·Topo (*topo)(Core·Area *area); - Core·Tape·Extent·Status (*extent)(Core·Area *area ,extent_t *write_pt); - Core·Status (*is_leftmot)(Core·Tape *tape ,Core·Tape·Address *a ,bool *result); - Core·Status (*is_rightmost)(Core·Tape *tape ,Core·Tape·Address *a ,bool *result); - Core·Status (*read)(Core·Area *area ,Core·Tape·Address *a ,Core·Tape·Remote *remote); - Core·Status (*write)(Core·Area *area ,Core·Tape·Address *a ,Core·Tape·Remote *remote); - - // initialize area - Core·Status (*init_pe)(Core·Area *area ,void *position ,extent_t extent); - Core·Status (*init_pp)(Core·Area *area ,void *position_left ,void *position_right); - Core·Status (*set_position)(Core·Area *area ,AU *new_position); - Core·Status (*set_position_left)(Core·Area *area ,AU *new_position); // synonym - Core·Status (*set_position_right)(Core·Area *area ,AU *new_position_right); - Core·Status (*set_extent)(Core·Area *area ,extent_t extent); - - // read area properties - AU *(*position)(Core·Area *area); - AU *(*position_left)(Core·Area *area); // synonym - AU *(*position_right)(Core·Area *area); - - AU *(*complement)(Core·Area *area ,AU *r); - - // area relationships - bool (*encloses_pt)(Core·Area *area ,AU *pt); - bool (*encloses_pt_strictly)(Core·Area *area ,AU *pt); - bool (*encloses_area)(Core·Area *outer ,Core·Area *inner); - bool (*encloses_area_strictly)(Core·Area *outer ,Core·Area *inner); - bool (*overlap)(Core·Area *a ,Core·Area *b); - void (*largest_aligned_64)(Core·Area *outer ,Core·Area *inner_64); - - } Core·Area·FG; - - Core·Area·FG Core·Area·fg; - - ----------------------------------------- - - - //---------------------------------------- - // Area - - // initialize an area - - Local void Core·Area·set_position(Core·Area *area ,void *new_position){ - area->position = new_position; - } - Local extent_t Core·Area·set_extent(Core·Area *area ,extent_t extent){ - return area->extent = extent; - } - Local void Core·Area·set_position_right(Core·Area *area ,void *new_position_right){ - Core·Area·set_extent(new_position_right - area->position); - } - Local void Core·Area·init_pe(Core·Area *area ,AU *position ,extent_t extent){ - Core·Area·set_position(position); - Core·Area·set_extent(extent); - } - Local void Core·Area·init_pp(Core·Area *area ,void *position_left ,void *position_right){ - Core·Area·set_position_left(position_left); - Core·Area·set_position_right(position_right); - } - - // read area properties - - Local bool Core·Area·empty(Core·Area *area){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·empty:: given NULL area"); - return true; - } - #endif - return area->position == NULL; - } - - // Requesting a NULL position is a logical error, because a NULL position - // means the Area is empty and has no position. Instead, use the `empty` - // predicate. - Local AU *Core·Area·position(Core·Area *area){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·position:: given NULL area"); - return NULL; - } - if(!area->position){ - fprintf(stderr,"Core·Area·position:: request for position when it is NULL"); - } - #endif - return area->position; - } - - - Local AU *Core·Area·position_right(Core·Area *area){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·position_right:: given NULL area"); - return NULL; - } - #endif - return area->position + area->extent; - } - Local extent_t Core·Area·extent(Core·Area *area){ - #ifdef Core·Debug - if(!area){ - fprintf(stderr,"Core·Area·extent:: given NULL area"); - return 0; - } - #endif - return area->extent; - } - Local AU Core·Area·length_Kung(Core·Area *area){ - if(!Core·Area·position_left(area)) return 0; - if(Core·Area·extent(area) >= 2) return 3; - return Core·Area·extent(area) + 1; - } - - Local bool Core·Area·encloses_pt(Core·Area *area ,AU *pt){ - return - (pt >= Core·Area·position_left(area)) - && (pt <= Core·Area·position_right(area)); - } - Local bool Core·Area·encloses_pt_strictly(Core·Area *area ,AU *pt){ - return - (pt > Core·Area·position_left(area)) - && (pt < Core·Area·position_right(area)); - } - Local bool Core·Area·encloses_area(Core·Area *outer ,Core·Area *inner){ - return - (Core·Area·position_left(inner) >= Core·Area·position_left(outer)) - && (Core·Area·position_right(inner) <= Core·Area·position_right(outer)); - } - Local bool Core·Area·encloses_area_strictly(Core·Area *outer ,Core·Area *inner){ - return - (Core·Area·position_left(inner) > Core·Area·position_left(outer)) - && (Core·Area·position_right(inner) < Core·Area·position_right(outer)); - } - - // Possible cases of overlap ,including just touching - // 1. interval 0 to the right of interval 1 ,just touching p00 == p11 - // 2. interval 0 to the left of interval 1 ,just touching p01 == p10 - // 3. interval 0 wholly contained in interval 1 - // 4. interval 0 wholly contains interval 1 - Local bool Core·Area·overlap(Core·Area *area0 ,Core·Area *area1){ - return - Core·Area·position_right(area0) >= Core·Area·position_left(area1) - && Core·Area·position_left(area0) <= Core·Area·position_right(area1); - } - - // find the largest contained interval aligned on 64 bit boundaries - static void Core·Area·largest_aligned_64(Core·Area *outer ,Core·Area *inner_64){ - uintptr_t p0 = (uintptr_t)Core·Area·position_left(outer); - uintptr_t p1 = (uintptr_t)Core·Area·position_right(outer); - - AU *p0_64 = (AU *)( (p0 + 0x7) & ~(uintptr_t)0x7 ); - AU *p1_64 = (AU *)( (p1 - 0x7) & ~(uintptr_t)0x7 ); - - if(p1_64 < p0_64){ - Core·Area·set_position(inner_64 ,NULL); - }else{ - Core·Area·init_pp(inner_64 ,p0_64 ,p1_64); - } - } - - // complement against the extent of the area (reverse direction) - // works for byte pointer - // works for aligned word pointer - Local AU *Core·Area·complement(Core·Area *area ,AU *r){ - return Core·Area·position_left(area) + (Core·Area·position_right(area) - r); - } - - - //---------------------------------------- - // model - - typedef struct{ - Core·Tableau tableau; - }Core·Tableau·State; - - - // some default instances - - //---------------------------------------- - // Tape model - Array Area - // an array area is represented by `position` and `extent`. - - // identical to Core·Link, used for typing pointers - typedef struct{ - Core·Area·ActionTable *action; - Core·Area·Tableau·Face *face; - Core·Area·Tableau·State *state; - Core·NextTable *next_table; - }Core·Area·Array·Link; - - Core·Link *Core·Area·Array·topo(Core·Link *lnk){ - #ifdef Core·Debug - if(!lnk){ - fprintf(stderr,"Core·Area·Array·topo:: given NULL lnk"); - return NULL; - } - if(!lnk->face){ - fprintf(stderr,"Core·Area·Array·topo:: given NULL face"); - return NULL; - } - #endif - l = (Core·Area·Array·Link *)lnk; - if(l->face->extent == 0) l->face->status = Core·Area·Topo·singleton; - l->face->status = Core·Area·Topo·segment; - return &l->next_table->on_track; - } - - Core·Link *Core·Area·Array·copy(Core·Link *link){ - #ifdef Core·Debug - uint error = Core·Link·check( - link - ,Core·Link·Mode·action | Core·Link·Mode·face | Core·Link·Mode·next_table - ); - if(error) return &link->next_table->derailed; - if(!&link->face->remote) return &link->next_table->derailed; - Core·Link link2{ - .action = Core·Area·address_valid - ,.face = link->face - ,.state = NULL - ,.next_table = { - .on_track = NULL - .derailed = link->next_table->derailed - } - } - initiate(link2); - #endif - l = (Core·Area·Array·Link *)link; - return &link->next_table->on_track; - } - - - Local Core·Area·ActionTable Core·Area·Array·action_table = { - .tape = { - .topo = Core·Area·Array·topo - .read - .write - .extent - } - .psoition_right - .complement - .address_valid - .encloses_pt_strictly_q - .encloses_area_q - .encloses_area_strictly_q - .overlap_q - } - - typedef struct{ - AU *position; - extent_t extent; - } Core·Area; - - // I removed the unions, as they are debugging hazards, and also might confuse the optimizer - typedef struct{ - struct{ - } byte_by_byte; - struct{ - Area area_64; - } copy_64; - struct{ - } read_hex; - struct{ - } write_hex; - } Core·TableauLocal; - - // this part goes into Maplib.a - #ifndef LOCAL - #endif - ------------------- - - -Local·Topo Area·topo_byte_array(Area *area){ - if(!area) return Core·Area·Topo·nonexistent; - if(!area->position) return Core·Area·Topo·empty; - if(area->extent == 0) return Core·Area·Topo·singleton; - return Core·Area·Topo·finite; -} - - - typedef struct{ - AU *position; - extent_t extent; - } Core·Area; - - - - typedef struct{ - Core·Area *area; - AU *hd; - } Core·TM·Array; - - - Local Node *Core·step_AU(Node *node){ - Core·Step·Node *step_node = (Core·Step·Node *)node; - step_node->hd = Core·offset(step_node->tm->hd ,1); - return node->next; - } - - Local Node *Core·step_8AU(Node *node){ - Core·Step·Node *step_node = (Core·Step·Node *)node; - step_node->hd = Core·offset_8AU(step_node->tm->hd ,1); - return node->next; - } - - - ------ - typedef struct{ - Core·Tableau *tableau; - Core·Action *action; - }Core·Link; - - - typedef void (*Core·Fn)(Core·Tableau *); - - - - typedef struct{ - void mount(Core·TM *tm ,Core·Area·M *a); - void rewind(Core·TM *tm); - void step(Core·TM *tm); - void step_left(Core·TM *tm); - void step_right(Core·TM *tm); // synonym for step - Core·TM·Head·Status status(Core·TM *tm); - Core·Tape·Topology topology(Core·TM *tm); // tape machine can abstract the tape - } Core·TM_NX·FTable; - - - - - -#include // for offsetof -#include // for printf, etc. - -typedef struct Core·TM Core·TM; - -// Example of a Head Status enum -typedef enum { - Core·TM·Head·Status·mu, - Core·TM·Head·Status·not_on_tape, - Core·TM·Head·Status·leftmost, - Core·TM·Head·Status·interim, - Core·TM·Head·Status·rightmost -} Core·TM·Head·Status; - -// Some placeholders for unknown types -typedef struct Core·Area·M { /* ... */ } Core·Area·M; -typedef enum Core·Tape·Topology { /* ... */ } Core·Tape·Topology; - -// The "tableau" struct: holds data relevant to an instance -typedef struct { - Core·TM *tm; // a pointer to some machine - Core·Area·M *a; // some area pointer - Core·TM·Head·Status status; // the machine's current head status - Core·Tape·Topology topology; // tape topology -} Core·TM_NX·Tableau; - -// Define a function-pointer type: it operates on a Tableau -typedef void (*Core·TM_NX·Fn)(Core·TM_NX·Tableau *t); - -// A table (struct) of function pointers—akin to a “vtable” or dictionary of methods -typedef struct { - Core·TM_NX·Fn mount; - Core·TM_NX·Fn rewind; - Core·TM_NX·Fn step; - Core·TM_NX·Fn step_left; - Core·TM_NX·Fn step_right; - Core·TM_NX·Fn status; - Core·TM_NX·Fn topology; -} Core·TM_NX·FnTable; - -/* - * A macro to create named enumeration constants that hold - * the byte offset of each field within Core·TM_NX·FnTable. - * - * Example usage inside an enum: - * enum { - * offset(Core·TM_NX·FnTable, mount), - * offset(Core·TM_NX·FnTable, rewind), - * ... - * }; - */ -#define offset(Type, field) offset_##field = offsetof(Type, field) - -// Create an enum with an entry for each function-pointer field. -// Each enumerator will hold the byte offset of that field in the struct. -enum { - offset(Core·TM_NX·FnTable, mount), - offset(Core·TM_NX·FnTable, rewind), - offset(Core·TM_NX·FnTable, step), - offset(Core·TM_NX·FnTable, step_left), - offset(Core·TM_NX·FnTable, step_right), - offset(Core·TM_NX·FnTable, status), - offset(Core·TM_NX·FnTable, topology) -}; - -// An example “global” or “default” FnTable (you would define real function pointers here) -static const Core·TM_NX·FnTable defaultFnTable = { - .mount = NULL, // or mount_impl, - .rewind = NULL, // or rewind_impl, - .step = NULL, - .step_left = NULL, - .step_right= NULL, - .status = NULL, - .topology = NULL -}; - -/* - * Dispatcher: given a byte offset (one of the offset_XXX enum values) - * and a tableau, find the correct function pointer in defaultFnTable and call it. - */ -void Core·TM_MX(Core·TM_NX·Tableau *t, size_t fnOffset) -{ - // We know each field in Core·TM_NX·FnTable is of type Core·TM_NX·Fn - // (i.e. a pointer-to-function). We'll do pointer arithmetic on a - // (Core·TM_NX·Fn *) pointer, but we must convert the byte offset to an index. - - // Point a function-pointer array at the start of defaultFnTable: - const Core·TM_NX·Fn *fnArray = (const Core·TM_NX·Fn *)&defaultFnTable; - - // Convert the byte offset to an index in the function-pointer array. - // Each element is the size of (Core·TM_NX·Fn). - // Typically (Core·TM_NX·Fn) is just a pointer, so we do: - size_t index = fnOffset / sizeof(Core·TM_NX·Fn); - - // Grab the function pointer - Core·TM_NX·Fn fn = fnArray[index]; - if (!fn) { - // Handle the case if the pointer is NULL (not implemented) - printf("Function pointer at offset %zu is not implemented.\n", fnOffset); - return; - } - - // Invoke it - fn(t); -} - -// Example usage -int main(void) -{ - Core·TM_NX·Tableau myTableau = { 0 }; - // Suppose we want to call "step" from the table: - Core·TM_MX(&myTableau, offset_step); - - // If it's NULL, the dispatcher prints a message. If not, it would call the function. - return 0; -}