• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision78b1fdcbe04515975798731d8526e966e0a89b50 (tree)
Time2014-07-08 02:04:59
Authorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

MALLOCを実装中。DATA命令の処理のリファクタリングを行った。

Change Summary

Incremental Difference

--- a/chncpu/args.c
+++ b/chncpu/args.c
@@ -87,11 +87,7 @@ int CHNCPU_VMArgs_BindDataToRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_VM
8787 env->iReg[bdTag->iReg] = CHNCPU_Op_DATA_GetDataCount(bdTag->dataOPTag);
8888 env->iRegBits[bdTag->iReg] = CHNCPU_BITS_MAX;
8989 // データタグを指定されたPRegに格納
90- env->pReg[bdTag->pReg].mindex = CHNCPU_MemoryIndex_INVALID;
91- env->pReg[bdTag->pReg].opTag = bdTag->dataOPTag;
92- env->pReg[bdTag->pReg].pindex = 0;
93- env->pReg[bdTag->pReg].type = CHNCPU_Op_DATA_GetDataType(bdTag->dataOPTag);
94-
90+ CHNCPU_Op_DATA_AssignDataTagToPReg(env, bdTag->pReg, bdTag->dataOPTag);
9591 }
9692
9793 return 0;
--- a/chncpu/chncpu.h
+++ b/chncpu/chncpu.h
@@ -223,9 +223,13 @@ int CHNCPU_Op_CompareIReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag
223223 int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
224224 int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
225225 int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
226+CHNCPU_OpTag *CHNCPU_Op_DATA_CreateEmptyData(void);
227+int CHNCPU_Op_DATA_AllocateRawDataMemory(CHNCPU_OpTag *op, ch4_sint dataType, ch4_uint dataCount);
226228 CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType);
229+int CHNCPU_Op_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_OpTag *op);
227230 int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType);
228231 int CHNCPU_Op_DATA_ReadAtIndex(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, int index);
232+int CHNCPU_Op_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount);
229233 int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op);
230234 int CHNCPU_Op_DATA_GetDataType(CHNCPU_OpTag *op);
231235 const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op);
--- a/chncpu/opcache.h
+++ b/chncpu/opcache.h
@@ -124,7 +124,6 @@ struct _CHNCPU_OP_CACHE_TERNARY_REG {
124124 // 26 TSTZ ((r1 & r2) == 0) ? -1 : 0;
125125 // 27 TSTNZ ((r1 & r2) != 0) ? -1 : 0;
126126 //
127-
128127 typedef struct _CHNCPU_OP_CACHE_COMPARE_IREG CHNCPU_OpCache_CompareIReg;
129128 struct _CHNCPU_OP_CACHE_COMPARE_IREG {
130129 ch4_uint rSrc1;
@@ -159,6 +158,20 @@ struct _CHNCPU_OP_CACHE_DATA {
159158 // 2F n
160159 //
161160
161+//
162+// 32 MALLOC
163+//
164+// 32 rType bitType rCount bitCount p
165+//
166+typedef struct _CHNCPU_OP_CACHE_MALLOC CHNCPU_OpCache_MALLOC;
167+struct _CHNCPU_OP_CACHE_MALLOC {
168+ ch4_uint rType;
169+ ch4_uint bitType;
170+ ch4_uint rCount;
171+ ch4_uint bitCount;
172+ ch4_uint p;
173+};
174+
162175
163176
164177 #endif
--- a/chncpu/opcode.c
+++ b/chncpu/opcode.c
@@ -725,15 +725,15 @@ int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op,
725725 opCache->filePath = NULL;
726726
727727 // Check ptype & Calculate rawDataByteSize
728- if(opCache->dataType == CHNCPU_PType_UINT8){
729- opCache->rawDataByteSize = opCache->dataCount;
730- } else{
731- env->errFlags |= CHNCPU_ERR_C_UNAVAILABLE_PTYPE;
728+ if(CHNCPU_Op_DATA_AllocateRawDataMemory(op, opCache->dataType, opCache->dataCount)){
729+ if(CHNCPU_Op_DATA_CalculateByteSizeOfRawData(opCache->dataType, opCache->dataCount) <= 0){
730+ env->errFlags |= CHNCPU_ERR_C_UNAVAILABLE_PTYPE;
731+ } else{
732+ env->errFlags |= CHNCPU_ERR_C_INTERNAL;
733+ }
732734 return -1;
733735 }
734736
735- opCache->rawData = malloc(opCache->rawDataByteSize);
736-
737737 // Bind rawData
738738 if(opCache->dataType == CHNCPU_PType_UINT8){
739739 for(i = 0; i < opCache->dataCount; i++){
@@ -783,12 +783,10 @@ int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, F
783783
784784 return 0;
785785 }
786-CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType)
786+CHNCPU_OpTag *CHNCPU_Op_DATA_CreateEmptyData(void)
787787 {
788- FILE *fp;
789788 CHNCPU_OpTag *op;
790789 CHNCPU_OpCache_DATA *opCache;
791- int i;
792790
793791 op = malloc(sizeof(CHNCPU_OpTag));
794792 if(!op){
@@ -805,8 +803,56 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch
805803
806804 opCache->labelID = 0;
807805 opCache->labelOpt = 0;
808- opCache->dataType = pType;
809- opCache->filePath = dataPath;
806+ opCache->dataType = CHNCPU_PType_Undefined;
807+ opCache->dataCount = 0;
808+ opCache->rawData = NULL;
809+ opCache->rawDataByteSize = 0;
810+ opCache->filePath = NULL;
811+
812+ return op;
813+}
814+int CHNCPU_Op_DATA_AllocateRawDataMemory(CHNCPU_OpTag *op, ch4_sint dataType, ch4_uint dataCount)
815+{
816+ // 生データのメモリを割り当てる。
817+ CHNCPU_OpCache_DATA *opCache;
818+
819+ if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
820+ return -1;
821+ }
822+ opCache = op->opCache;
823+
824+ opCache->rawDataByteSize = CHNCPU_Op_DATA_CalculateByteSizeOfRawData(dataType, dataCount);
825+ if(opCache->rawDataByteSize <= 0){
826+ return -1;
827+ }
828+ if(opCache->rawData){
829+ puts("CHNCPU_Op_DATA_AllocateRawDataMemory: Double allocation. abort.");
830+ exit(EXIT_FAILURE);
831+ }
832+ opCache->rawData = malloc(opCache->rawDataByteSize);
833+ if(!opCache->rawData){
834+ opCache->rawDataByteSize = 0;
835+ return -1;
836+ }
837+
838+ opCache->dataType = dataType;
839+ opCache->dataCount = dataCount;
840+
841+ return 0;
842+}
843+CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType)
844+{
845+ // 現状ではUINT8のみ
846+ FILE *fp;
847+ CHNCPU_OpTag *op;
848+ CHNCPU_OpCache_DATA *opCache;
849+ int i;
850+
851+ op = CHNCPU_Op_DATA_CreateEmptyData();
852+ if(!op){
853+ return NULL;
854+ }
855+ opCache = op->opCache;
810856
811857 fp = fopen(dataPath, "rb");
812858 if(!fp){
@@ -815,21 +861,19 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch
815861 return NULL;
816862 }
817863 fseek(fp, 0, SEEK_END);
818-
819864 opCache->rawDataByteSize = (int)ftell(fp);
820-
821865 fseek(fp, 0, SEEK_SET);
822866
823- opCache->rawData = malloc(opCache->rawDataByteSize);
824- if(!opCache->rawData){
867+ // 現状はUINT8前提なのでopCache->rawDataByteSize==dataCount
868+ if(CHNCPU_Op_DATA_AllocateRawDataMemory(op, pType, opCache->rawDataByteSize)){
825869 fclose(fp);
826870 free(opCache);
827871 free(op);
828872 return NULL;
829873 }
830874
875+ // データの中身をファイルからコピー
831876 if(opCache->dataType == CHNCPU_PType_UINT8){
832- opCache->dataCount = opCache->rawDataByteSize;
833877 for(i = 0; i < opCache->dataCount; i++){
834878 ((unsigned char *)opCache->rawData)[i] = (unsigned char)fgetc(fp);
835879 }
@@ -845,6 +889,24 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch
845889 return op;
846890 }
847891
892+int CHNCPU_Op_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_OpTag *op)
893+{
894+ CHNCPU_OpCache_DATA *opCache;
895+
896+ if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache || p >= CHNCPU_NUMBER_OF_PREG){
897+ return -1;
898+ }
899+ opCache = op->opCache;
900+
901+ // データタグを指定されたPRegに格納
902+ env->pReg[p].mindex = CHNCPU_MemoryIndex_INVALID;
903+ env->pReg[p].opTag = op;
904+ env->pReg[p].pindex = 0;
905+ env->pReg[p].type = opCache->dataType;
906+
907+ return 0;
908+}
909+
848910 int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType)
849911 {
850912 // retv: written bytes
@@ -902,6 +964,13 @@ int CHNCPU_Op_DATA_ReadAtIndex(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op,
902964
903965 return retv;
904966 }
967+int CHNCPU_Op_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount)
968+{
969+ if(dataType == CHNCPU_PType_UINT8){
970+ return dataCount;
971+ }
972+ return 0;
973+}
905974 int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op)
906975 {
907976 if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
@@ -927,3 +996,83 @@ const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op)
927996 return ((CHNCPU_OpCache_DATA *)op->opCache)->filePath;
928997 }
929998
999+//
1000+// 32 MALLOC
1001+//
1002+/*
1003+int CHNCPU_Op_MALLOC_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
1004+{
1005+ CHNCPU_OpCache_MALLOC *opCache;
1006+
1007+ opCache = malloc(sizeof(CHNCPU_OpCache_MALLOC));
1008+ op->opCache = opCache;
1009+
1010+ opCache->rType = CH4Reader_ReadNextAsUINT(env->appbinReader);
1011+ opCache->bitType = CH4Reader_ReadNextAsUINT(env->appbinReader);
1012+ opCache->rCount = CH4Reader_ReadNextAsUINT(env->appbinReader);
1013+ opCache->bitCount = CH4Reader_ReadNextAsUINT(env->appbinReader);
1014+ opCache->p = CH4Reader_ReadNextAsUINT(env->appbinReader);
1015+
1016+ if(opCache->rType >= CHNCPU_NUMBER_OF_IREG ||
1017+ opCache->rCount >= CHNCPU_NUMBER_OF_IREG ||
1018+ opCache->p >= CHNCPU_NUMBER_OF_PREG){
1019+ env->errFlags |= CHNCPU_ERR_C_REGNUM;
1020+ return -1;
1021+ }
1022+ if(!CHNCPU_CHK_IsAvailableBits(env, opCache->bitType) ||
1023+ !CHNCPU_CHK_IsAvailableBits(env, opCache->bitCount)){
1024+ return -1;
1025+ }
1026+ if(prefix != 0){
1027+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
1028+ return -1;
1029+ }
1030+ return 0;
1031+}
1032+
1033+int CHNCPU_Op_MALLOC_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
1034+{
1035+ CHNCPU_OpTag *dataOpTag;
1036+ CHNCPU_OpCache_MALLOC *opCache;
1037+ int value;
1038+ ch4_sint type;
1039+ ch4_uint count;
1040+
1041+ opCache = op->opCache;
1042+
1043+ type = env->iReg[opCache->rType];
1044+ count = env->iReg[opCache->rCount];
1045+
1046+ dataOpTag = CHNCPU_Op_DATA_CreateEmptyData();
1047+ if(CHNCPU_Op_DATA_AllocateRawDataMemory(dataOpTag, type, count)){
1048+
1049+ }
1050+
1051+
1052+ value = CHNCPU_Op_DATA_ReadAtIndex(env, p->opTag, p->pindex);
1053+ if(env->errFlags){
1054+ return -1;
1055+ }
1056+
1057+ CHNCPU_AdjustValueForBit(env, &value, opCache->bitDst, CHNCPU_PREFIX_ALLOW_TRUNCATE);
1058+ if(env->errFlags){
1059+ return -1;
1060+ }
1061+ env->iReg[opCache->r] = value;
1062+ env->iRegBits[opCache->r] = opCache->bitDst;
1063+
1064+ p->pindex += opCache->pDiff;
1065+
1066+ return 0;
1067+}
1068+int CHNCPU_Op_MALLOC_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file)
1069+{
1070+ CHNCPU_OpCache_LMEM *opCache;
1071+
1072+ opCache = op->opCache;
1073+ fprintf(file, "LMEM(p:0x%02X, pType:0x%X, pDiff:%d, r:%02X, bitDst:%d);\n", opCache->p, opCache->pType, opCache->pDiff, opCache->r, opCache->bitDst);
1074+
1075+ return 0;
1076+}
1077+
1078+*/
\ No newline at end of file