Revision | 78b1fdcbe04515975798731d8526e966e0a89b50 (tree) |
---|---|
Time | 2014-07-08 02:04:59 |
Author | hikarupsp <hikarupsp@user...> |
Commiter | hikarupsp |
MALLOCを実装中。DATA命令の処理のリファクタリングを行った。
@@ -87,11 +87,7 @@ int CHNCPU_VMArgs_BindDataToRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_VM | ||
87 | 87 | env->iReg[bdTag->iReg] = CHNCPU_Op_DATA_GetDataCount(bdTag->dataOPTag); |
88 | 88 | env->iRegBits[bdTag->iReg] = CHNCPU_BITS_MAX; |
89 | 89 | // データタグを指定された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); | |
95 | 91 | } |
96 | 92 | |
97 | 93 | return 0; |
@@ -223,9 +223,13 @@ int CHNCPU_Op_CompareIReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag | ||
223 | 223 | int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); |
224 | 224 | int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); |
225 | 225 | 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); | |
226 | 228 | 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); | |
227 | 230 | int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType); |
228 | 231 | 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); | |
229 | 233 | int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op); |
230 | 234 | int CHNCPU_Op_DATA_GetDataType(CHNCPU_OpTag *op); |
231 | 235 | const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op); |
@@ -124,7 +124,6 @@ struct _CHNCPU_OP_CACHE_TERNARY_REG { | ||
124 | 124 | // 26 TSTZ ((r1 & r2) == 0) ? -1 : 0; |
125 | 125 | // 27 TSTNZ ((r1 & r2) != 0) ? -1 : 0; |
126 | 126 | // |
127 | - | |
128 | 127 | typedef struct _CHNCPU_OP_CACHE_COMPARE_IREG CHNCPU_OpCache_CompareIReg; |
129 | 128 | struct _CHNCPU_OP_CACHE_COMPARE_IREG { |
130 | 129 | ch4_uint rSrc1; |
@@ -159,6 +158,20 @@ struct _CHNCPU_OP_CACHE_DATA { | ||
159 | 158 | // 2F n |
160 | 159 | // |
161 | 160 | |
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 | + | |
162 | 175 | |
163 | 176 | |
164 | 177 | #endif |
@@ -725,15 +725,15 @@ int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, | ||
725 | 725 | opCache->filePath = NULL; |
726 | 726 | |
727 | 727 | // 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 | + } | |
732 | 734 | return -1; |
733 | 735 | } |
734 | 736 | |
735 | - opCache->rawData = malloc(opCache->rawDataByteSize); | |
736 | - | |
737 | 737 | // Bind rawData |
738 | 738 | if(opCache->dataType == CHNCPU_PType_UINT8){ |
739 | 739 | for(i = 0; i < opCache->dataCount; i++){ |
@@ -783,12 +783,10 @@ int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, F | ||
783 | 783 | |
784 | 784 | return 0; |
785 | 785 | } |
786 | -CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType) | |
786 | +CHNCPU_OpTag *CHNCPU_Op_DATA_CreateEmptyData(void) | |
787 | 787 | { |
788 | - FILE *fp; | |
789 | 788 | CHNCPU_OpTag *op; |
790 | 789 | CHNCPU_OpCache_DATA *opCache; |
791 | - int i; | |
792 | 790 | |
793 | 791 | op = malloc(sizeof(CHNCPU_OpTag)); |
794 | 792 | if(!op){ |
@@ -805,8 +803,56 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch | ||
805 | 803 | |
806 | 804 | opCache->labelID = 0; |
807 | 805 | 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; | |
810 | 856 | |
811 | 857 | fp = fopen(dataPath, "rb"); |
812 | 858 | if(!fp){ |
@@ -815,21 +861,19 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch | ||
815 | 861 | return NULL; |
816 | 862 | } |
817 | 863 | fseek(fp, 0, SEEK_END); |
818 | - | |
819 | 864 | opCache->rawDataByteSize = (int)ftell(fp); |
820 | - | |
821 | 865 | fseek(fp, 0, SEEK_SET); |
822 | 866 | |
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)){ | |
825 | 869 | fclose(fp); |
826 | 870 | free(opCache); |
827 | 871 | free(op); |
828 | 872 | return NULL; |
829 | 873 | } |
830 | 874 | |
875 | + // データの中身をファイルからコピー | |
831 | 876 | if(opCache->dataType == CHNCPU_PType_UINT8){ |
832 | - opCache->dataCount = opCache->rawDataByteSize; | |
833 | 877 | for(i = 0; i < opCache->dataCount; i++){ |
834 | 878 | ((unsigned char *)opCache->rawData)[i] = (unsigned char)fgetc(fp); |
835 | 879 | } |
@@ -845,6 +889,24 @@ CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch | ||
845 | 889 | return op; |
846 | 890 | } |
847 | 891 | |
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 | + | |
848 | 910 | int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType) |
849 | 911 | { |
850 | 912 | // retv: written bytes |
@@ -902,6 +964,13 @@ int CHNCPU_Op_DATA_ReadAtIndex(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, | ||
902 | 964 | |
903 | 965 | return retv; |
904 | 966 | } |
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 | +} | |
905 | 974 | int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op) |
906 | 975 | { |
907 | 976 | if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){ |
@@ -927,3 +996,83 @@ const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op) | ||
927 | 996 | return ((CHNCPU_OpCache_DATA *)op->opCache)->filePath; |
928 | 997 | } |
929 | 998 | |
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 |