@@ -80,14 +80,25 @@ write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags)
8080
8181 cur += uint32_to_bin (irep -> ilen , cur ); /* number of opcode */
8282 cur += write_padding (cur );
83- if (flags & FLAG_BYTEORDER_NATIVE ) {
84- memcpy (cur , irep -> iseq , irep -> ilen * sizeof (mrb_code ));
85- cur += irep -> ilen * sizeof (mrb_code );
86- }
87- else {
83+ switch (flags & DUMP_ENDIAN_NAT ) {
84+ case DUMP_ENDIAN_BIG :
85+ if (bigendian_p ()) goto native ;
8886 for (iseq_no = 0 ; iseq_no < irep -> ilen ; iseq_no ++ ) {
8987 cur += uint32_to_bin (irep -> iseq [iseq_no ], cur ); /* opcode */
9088 }
89+ break ;
90+ case DUMP_ENDIAN_LIL :
91+ if (!bigendian_p ()) goto native ;
92+ for (iseq_no = 0 ; iseq_no < irep -> ilen ; iseq_no ++ ) {
93+ cur += uint32l_to_bin (irep -> iseq [iseq_no ], cur ); /* opcode */
94+ }
95+ break ;
96+
97+ native :
98+ case DUMP_ENDIAN_NAT :
99+ memcpy (cur , irep -> iseq , irep -> ilen * sizeof (mrb_code ));
100+ cur += irep -> ilen * sizeof (mrb_code );
101+ break ;
91102 }
92103
93104 return cur - buf ;
@@ -323,7 +334,7 @@ write_footer(mrb_state *mrb, uint8_t *bin)
323334{
324335 struct rite_binary_footer footer ;
325336
326- memcpy (footer .section_identify , RITE_BINARY_EOF , sizeof (footer .section_identify ));
337+ memcpy (footer .section_ident , RITE_BINARY_EOF , sizeof (footer .section_ident ));
327338 uint32_to_bin (sizeof (struct rite_binary_footer ), footer .section_size );
328339 memcpy (bin , & footer , sizeof (struct rite_binary_footer ));
329340
@@ -336,7 +347,7 @@ write_section_irep_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
336347{
337348 struct rite_section_irep_header * header = (struct rite_section_irep_header * )bin ;
338349
339- memcpy (header -> section_identify , RITE_SECTION_IREP_IDENTIFIER , sizeof (header -> section_identify ));
350+ memcpy (header -> section_ident , RITE_SECTION_IREP_IDENT , sizeof (header -> section_ident ));
340351
341352 mrb_assert_int_fit (size_t , section_size , uint32_t , UINT32_MAX );
342353 uint32_to_bin ((uint32_t )section_size , header -> section_size );
@@ -373,7 +384,7 @@ write_section_lineno_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
373384{
374385 struct rite_section_lineno_header * header = (struct rite_section_lineno_header * )bin ;
375386
376- memcpy (header -> section_identify , RITE_SECTION_LINENO_IDENTIFIER , sizeof (header -> section_identify ));
387+ memcpy (header -> section_ident , RITE_SECTION_LINENO_IDENT , sizeof (header -> section_ident ));
377388 uint32_to_bin ((uint32_t )section_size , header -> section_size );
378389
379390 return MRB_DUMP_OK ;
@@ -664,7 +675,7 @@ write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur, mrb_sym const
664675 dlen = write_debug_record (mrb , irep , cur , filenames , filenames_len );
665676 section_size += dlen ;
666677
667- memcpy (header -> section_identify , RITE_SECTION_DEBUG_IDENTIFIER , sizeof (header -> section_identify ));
678+ memcpy (header -> section_ident , RITE_SECTION_DEBUG_IDENT , sizeof (header -> section_ident ));
668679 mrb_assert (section_size <= INT32_MAX );
669680 uint32_to_bin (section_size , header -> section_size );
670681
@@ -803,7 +814,7 @@ write_section_lv(mrb_state *mrb, mrb_irep *irep, uint8_t *start, mrb_sym const *
803814 goto lv_section_exit ;
804815 }
805816
806- memcpy (header -> section_identify , RITE_SECTION_LV_IDENTIFIER , sizeof (header -> section_identify ));
817+ memcpy (header -> section_ident , RITE_SECTION_LV_IDENT , sizeof (header -> section_ident ));
807818
808819 diff = cur - start ;
809820 mrb_assert_int_fit (ptrdiff_t , diff , size_t , SIZE_MAX );
@@ -820,19 +831,23 @@ write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8
820831 uint16_t crc ;
821832 uint32_t offset ;
822833
823- if (flags & FLAG_BYTEORDER_NATIVE ) {
824- uint32_t ident = 0 ;
825- size_t i ;
826-
827- for (i = 0 ; i < sizeof (ident ); i ++ ) {
828- ident <<=8 ;
829- ident |=RITE_BINARY_IDENTIFIER [i ];
830- }
831- memcpy (header -> binary_identify , (char * )& ident , sizeof (uint32_t ));
832- }
833- else {
834- memcpy (header -> binary_identify , RITE_BINARY_IDENTIFIER , sizeof (header -> binary_identify ));
834+ switch (flags & DUMP_ENDIAN_NAT ) {
835+ default :
836+ endian_big :
837+ case DUMP_ENDIAN_BIG :
838+ memcpy (header -> binary_ident , RITE_BINARY_IDENT , sizeof (header -> binary_ident ));
839+ break ;
840+ endian_little :
841+ case DUMP_ENDIAN_LIL :
842+ memcpy (header -> binary_ident , RITE_BINARY_IDENT_LIL , sizeof (header -> binary_ident ));
843+ break ;
844+
845+ case DUMP_ENDIAN_NAT :
846+ if (bigendian_p ()) goto endian_big ;
847+ goto endian_little ;
848+ break ;
835849 }
850+
836851 memcpy (header -> binary_version , RITE_BINARY_FORMAT_VER , sizeof (header -> binary_version ));
837852 memcpy (header -> compiler_name , RITE_COMPILER_NAME , sizeof (header -> compiler_name ));
838853 memcpy (header -> compiler_version , RITE_COMPILER_VERSION , sizeof (header -> compiler_version ));
@@ -872,8 +887,23 @@ is_lv_defined(mrb_irep *irep)
872887 return FALSE;
873888}
874889
890+ static uint8_t
891+ dump_flags (uint8_t flags , uint8_t native )
892+ {
893+ if (native == FLAG_BYTEORDER_NATIVE ) {
894+ if ((flags & DUMP_ENDIAN_NAT ) == 0 ) {
895+ return (flags & DUMP_DEBUG_INFO ) | DUMP_ENDIAN_NAT ;
896+ }
897+ return flags ;
898+ }
899+ if ((flags & DUMP_ENDIAN_NAT ) == 0 ) {
900+ return (flags & DUMP_DEBUG_INFO ) | DUMP_ENDIAN_BIG ;
901+ }
902+ return flags ;
903+ }
904+
875905static int
876- dump_irep (mrb_state * mrb , mrb_irep * irep , int debug_info , uint8_t * * bin , size_t * bin_size , uint8_t flags )
906+ dump_irep (mrb_state * mrb , mrb_irep * irep , uint8_t flags , uint8_t * * bin , size_t * bin_size )
877907{
878908 int result = MRB_DUMP_GENERAL_FAILURE ;
879909 size_t malloc_size ;
@@ -893,7 +923,7 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
893923 section_irep_size += get_irep_record_size (mrb , irep );
894924
895925 /* DEBUG section size */
896- if (debug_info ) {
926+ if (flags & DUMP_DEBUG_INFO ) {
897927 if (debug_info_defined ) {
898928 section_lineno_size += sizeof (struct rite_section_debug_header );
899929 /* filename table */
@@ -933,7 +963,7 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
933963 sizeof (struct rite_binary_footer );
934964
935965 /* write DEBUG section */
936- if (debug_info ) {
966+ if (flags & DUMP_DEBUG_INFO ) {
937967 if (debug_info_defined ) {
938968 result = write_section_debug (mrb , irep , cur , filenames , filenames_len );
939969 }
@@ -972,13 +1002,13 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
9721002}
9731003
9741004int
975- mrb_dump_irep (mrb_state * mrb , mrb_irep * irep , int debug_info , uint8_t * * bin , size_t * bin_size )
1005+ mrb_dump_irep (mrb_state * mrb , mrb_irep * irep , uint8_t flags , uint8_t * * bin , size_t * bin_size )
9761006{
977- return dump_irep (mrb , irep , debug_info , bin , bin_size , FLAG_BYTEORDER_NONATIVE );
1007+ return dump_irep (mrb , irep , dump_flags ( flags , FLAG_BYTEORDER_NONATIVE ), bin , bin_size );
9781008}
9791009
9801010int
981- mrb_dump_irep_binary (mrb_state * mrb , mrb_irep * irep , int debug_info , FILE * fp )
1011+ mrb_dump_irep_binary (mrb_state * mrb , mrb_irep * irep , uint8_t flags , FILE * fp )
9821012{
9831013 uint8_t * bin = NULL ;
9841014 size_t bin_size = 0 ;
@@ -988,7 +1018,7 @@ mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE* fp)
9881018 return MRB_DUMP_INVALID_ARGUMENT ;
9891019 }
9901020
991- result = dump_irep (mrb , irep , debug_info , & bin , & bin_size , FLAG_BYTEORDER_NONATIVE );
1021+ result = dump_irep (mrb , irep , dump_flags ( flags , FLAG_BYTEORDER_NONATIVE ), & bin , & bin_size );
9921022 if (result == MRB_DUMP_OK ) {
9931023 if (fwrite (bin , sizeof (bin [0 ]), bin_size , fp ) != bin_size ) {
9941024 result = MRB_DUMP_WRITE_FAULT ;
@@ -1015,8 +1045,22 @@ is_valid_c_symbol_name(const char *name)
10151045 return TRUE;
10161046}
10171047
1048+ static int
1049+ dump_bigendian_p (uint8_t flags )
1050+ {
1051+ switch (flags & DUMP_ENDIAN_NAT ) {
1052+ case DUMP_ENDIAN_BIG :
1053+ return TRUE;
1054+ case DUMP_ENDIAN_LIL :
1055+ return FALSE;
1056+ default :
1057+ case DUMP_ENDIAN_NAT :
1058+ return bigendian_p ();
1059+ }
1060+ }
1061+
10181062int
1019- mrb_dump_irep_cfunc (mrb_state * mrb , mrb_irep * irep , int debug_info , FILE * fp , const char * initname )
1063+ mrb_dump_irep_cfunc (mrb_state * mrb , mrb_irep * irep , uint8_t flags , FILE * fp , const char * initname )
10201064{
10211065 uint8_t * bin = NULL ;
10221066 size_t bin_size = 0 , bin_idx = 0 ;
@@ -1025,9 +1069,23 @@ mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE *fp, co
10251069 if (fp == NULL || initname == NULL || !is_valid_c_symbol_name (initname )) {
10261070 return MRB_DUMP_INVALID_ARGUMENT ;
10271071 }
1028-
1029- result = dump_irep (mrb , irep , debug_info , & bin , & bin_size , FLAG_BYTEORDER_NATIVE );
1072+ flags = dump_flags ( flags , FLAG_BYTEORDER_NATIVE );
1073+ result = dump_irep (mrb , irep , flags , & bin , & bin_size );
10301074 if (result == MRB_DUMP_OK ) {
1075+ if (!dump_bigendian_p (flags )) {
1076+ if (fprintf (fp , "/* dumped in little endian order.\n"
1077+ " use `mrbc -E` option for big endian CPU. */\n" ) < 0 ) {
1078+ mrb_free (mrb , bin );
1079+ return MRB_DUMP_WRITE_FAULT ;
1080+ }
1081+ }
1082+ else {
1083+ if (fprintf (fp , "/* dumped in big endian order.\n"
1084+ " use `mrbc -e` option for better performance on little endian CPU. */\n" ) < 0 ) {
1085+ mrb_free (mrb , bin );
1086+ return MRB_DUMP_WRITE_FAULT ;
1087+ }
1088+ }
10311089 if (fprintf (fp , "#include <stdint.h>\n" ) < 0 ) { /* for uint8_t under at least Darwin */
10321090 mrb_free (mrb , bin );
10331091 return MRB_DUMP_WRITE_FAULT ;
0 commit comments