Thread: I can't spot my mistake...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734

    I can't spot my mistake...

    I'll start with the output
    Code:
    make rebuild
    MAKECMDGOALS=rebuild
    make -f main.mak rebuild
    make[1]: Entering directory '~/alu'
    PRJ_SRC_FILES = 'test.c alu_bit.c alu_main.c alu_math.c alu_mem.c alu_vec.c'
    rm -f *.AppImage *.exe *.so *.dll *.o *.obj
    cc -fPIC -shared -Wall -Wextra -I cloned/fbstdc/include  -o test.o -c test.c
    make[1]: Leaving directory '~/alu'
    fatal: not in a git directory
    In file included from test.c:1:
    alu.h:12:10: fatal error: allstdint.h: No such file or directory
       12 | #include <allstdint.h>
          |          ^~~~~~~~~~~~~
    compilation terminated.
    make[1]: *** [main.mak:98: test.o] Error 1
    make: *** [1st.mak:6: rebuild] Error 2
    Compilation failed.
    I've checked and the files were certainly downloaded and saved to the right place, I'm guessing I'm using the -I flag wrong maybe? If you need more just tell me what you're looking for and I'll post it

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Never mind, just spotted my mistake, I had renamed the files but forgot to change it in code

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    I add
    Code:
    .LIBPATTERNS:=$(DST_LIB_PFX)%.$(DST_LIB_SFX) $(DST_LIB_PFX)%.a
    to the dst_sys.mak header so that should reduce linkage errors

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Made another mistake that I can't identify, this seemed an appropriate thread to tack it onto instead of creating another thread.
    This:
    Code:
    $(info $(call rebase,$(FBSTDC_DIR)))
    $(shell $(call rebase,$(FBSTDC_DIR)))
    Lead to this:
    Code:
    cd 'cloned/fbstdc' && git config pull.rebase true && git pull
    main.mak:42: *** missing separator.  Stop.
    Any ideas what I did wrong there?

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Managed to fix the issue with register acquisition but now encountering a stack smashing fault, I don't know how to look for the cause of that so I would appreciate suggestions, the only info I currently have from debugging is the function it occurs in and the last value printed
    Code:
    char *pos_int[] = {
    	"123",
    	"+145",
    	"0xA",
    	"0b1",
    NULL };
    
    char *neg_int[] = {
    	"-123",
    	"-0xA",
    	"-0b1", // This is the last value printed
    NULL };
    
    char *pos_fpn[] = {
    	"0",
    	"1",
    	"1.0",
    	"0.1",
    	"1.1",
    	"1e+10",
    	"1.0e+10",
    	"0.1e+10",
    	"1.1e+10",
    	"1e-10",
    	"1.0e-10",
    	"0.1e-10",
    	"1.1e-10",
    	"0xA",
    	"0xA.0",
    	"0x0.A",
    	"0xA.A",
    	"0xAp+10",
    	"0xA.0p+10",
    	"0x0.Ap+10",
    	"0xA.Ap+10",
    	"0xAp-10",
    	"0xA.0p-10",
    	"0x0.Ap-10",
    	"0xA.Ap-10",
    	"0b1",
    	"0b1.0",
    	"0b0.1",
    	"0b1.1",
    	"0b1e+10",
    	"0b1.0e+10",
    	"0b0.1e+10",
    	"0b1.1e+10",
    	"0b1e-10",
    	"0b1.0e-10",
    	"0b0.1e-10",
    	"0b1.1e-10",
    NULL };
    
    char *neg_fpn[] = {
    	"-0.0",
    	"-1",
    	"-1.0",
    	"-0.1",
    	"-1.1",
    	"-1e+10",
    	"-1.0e+10",
    	"-0.1e+10",
    	"-1.1e+10",
    	"-1e-10",
    	"-1.0e-10",
    	"-0.1e-10",
    	"-1.1e-10",
    	"-0xA",
    	"-0xA.0",
    	"-0x0.A",
    	"-0xA.A",
    	"-0xAp+10",
    	"-0xA.0p+10",
    	"-0x0.Ap+10",
    	"-0xA.Ap+10",
    	"-0xAp-10",
    	"-0xA.0p-10",
    	"-0x0.Ap-10",
    	"-0xA.Ap-10",
    	"-0b1",
    	"-0b1.0",
    	"-0b0.1",
    	"-0b1.1",
    	"-0b1e+10",
    	"-0b1.0e+10",
    	"-0b0.1e+10",
    	"-0b1.1e+10",
    	"-0b1e-10",
    	"-0b1.0e-10",
    	"-0b0.1e-10",
    	"-0b1.1e-10",
    NULL };
    
    int print_value( alu_t *alu, bool print_anyways )
    {
    	int ret = 0, i;
    	alu_src_t _src = {NULL};
    	alu_dst_t _dst = {NULL};
    	alu_base_t base = {0};
    	alu_lit_t lit = {0};
    	alu_block_t __src = {0}, __dst = {0};
    	char *src;
    	size_t size;
    	long nextpos;
    	
    	base.digsep = '\'';
    	base.base = 10;
    	ret = alu_get_reg_nodes( alu, base.regv, ALU_BASE_COUNT, 0 );
    	
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	ret = alu_get_reg_nodes( alu, lit.regv, ALU_LIT_COUNT, 0 );
    	
    	
    	if ( ret != 0 )
    	{
    		alu_rem_reg_nodes( alu, base.regv, ALU_BASE_COUNT );
    		alu_error( ret );
    		return ret;
    	}
    	
    	_src.src = &__src;
    	_src.next = (alu_func_rdChar32_t)func_rdChar32;
    	_src.nextpos = &nextpos;
    	
    	_dst.dst = &__dst;
    	_dst.next = (alu_func_wrChar32_t)func_wrChar32;
    	_dst.flip = (alu_func_flipstr_t)func_flipstr;
    	
    	for ( i = 0; pos_int[i]; ++i )
    	{
    		src = pos_int[i];
    		size = strlen(src);
    		switch ( src[1] )
    		{
    		case 'x': case 'X': base.base = 16; break;
    		case 'b': case 'B': base.base = 2; break;
    		default: base.base = 10;
    		}
    		
    		nextpos = 0;
    		__src.block = src;
    		__src.bytes.used = strlen(src);
    		__src.bytes.last = __src.bytes.used;
    		__src.bytes.upto = size ? size : __src.bytes.used + 1;
    		
    		ret = alu_block_expand( &__dst, __src.bytes.upto * 2 );
    		if ( ret != 0 )
    			return ret;
    		
    		ret = reg_print_value( alu, _src, _dst, base, lit, print_anyways );
    		
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			goto fail;
    		}
    		
    		ret = uint_print_value( alu, _src, _dst, base, lit, print_anyways );
    		
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			goto fail;
    		}
    		
    		ret = int_print_value( alu, _src, _dst, base, lit, print_anyways );
    		
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			goto fail;
    		}
    	}
    	
    	for ( i = 0; neg_int[i]; ++i )
    	{
    		src = neg_int[i];
    		size = strlen(src);
    		switch ( src[1] )
    		{
    		case 'x': case 'X': base.base = 16; break;
    		case 'b': case 'B': base.base = 2; break;
    		default: base.base = 10;
    		}
    		
    		nextpos = 0;
    		__src.block = src;
    		__src.bytes.used = strlen(src);
    		__src.bytes.last = __src.bytes.used;
    		__src.bytes.upto = size ? size : __src.bytes.used + 1;
    		
    		ret = alu_block_expand( &__dst, __src.bytes.upto * 2 );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			goto fail;
    		}
    		
    		ret = int_print_value( alu, _src, _dst, base, lit, print_anyways );
    		
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			goto fail;
    		}
    	}
    	
    	fail:
    	alu_block_release( &__dst );
    	return ret;
    }
    Sorry Hodor, still further no ideas on your failed linkage issue, if what I suggested and did don't fix it then you'll just have to try what ever you think of, at the most you'll only be editing the *.mak files so that should at least reduce the annoyance if you care enough to try. Let me know how it goes if you do try and even if you don't at least let me know if it's a lost cause trying to get you to compile it again.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Encountering some VERY strange behaviour, here's the relevant output:
    Code:
    ...
    test.c:393: bitwise() 'Shifting values...'
    test.c:394: bitwise() '==========================================='
    test.c:99: modify() num.node = 8, val.node = 9, _num = 0x00000000DEADC0DE, _val = 0x0000000000000004
    num = 0000000000000000000000000000000000000000000000000000000000000100
    val = 0000000000000000000000000000000000000000000000000000000000000100
    test.c:212: modify() 0x00000000DEADC0DE >> 0x0000000000000004, Expected 0x000000000DEADC0D, Got 0x0000000000000000, op = '>'
    test.c:410: bitwise() 'Rotating values...'
    test.c:411: bitwise() '==========================================='
    test.c:212: modify() 0x00000000DEADC0DE <<< 0x0000000000000004, Expected 0x0000000DEADC0DE0, Got 0x00000000DEADC0DE, op = 'l'
    ...
    And here's the relevant code:
    Code:
    int modify(
    	alu_t *alu
    	, ssize_t _num
    	, ssize_t _val
    	, uint_t info
    	, bool print_anyways
    	, int op
    )
    {
    	int ret = 0;
    	uint_t nodes[2] = {0};
    	alu_reg_t num = {0}, val = {0};
    	ssize_t expect = 0;
    	char pfx[sizeof(size_t) * CHAR_BIT] = {0};
    	
    	ret = alu_get_reg_nodes( alu, nodes, 2, 0 );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	alu_reg_init( alu, num, nodes[0], info );
    	alu_reg_init( alu, val, nodes[1], info );
    	
    	num.upto = val.upto = bitsof(size_t);
    	
    	alu_reg_set_nil( alu, num );
    	alu_reg_set_nil( alu, val );
    	
    	alu_reg_set_raw( alu, num, &_num, info, sizeof(ssize_t) );
    	alu_reg_set_raw( alu, val, &_val, info, sizeof(ssize_t) );
    	
    #if 1
    	if ( op == '>' )
    	{
    		alu_printf
    		(
    			"num.node = %u, val.node = %u, _num = 0x%016zX, _val = 0x%016zX",
    			num.node, val.node, _num, _val
    		);
    		alu_print_reg( "num", alu, num, 0, 1 );
    		alu_print_reg( "val", alu, val, 0, 1 );
    	}
    #endif
    ...
    When I try moving
    Code:
    	alu_reg_set_raw( alu, num, &_num, info, sizeof(ssize_t) );
    to below
    Code:
    	alu_reg_set_raw( alu, val, &_val, info, sizeof(ssize_t) );
    Somehow val ends up holding 0xDEADC0DE (value of _num) while num remains fixed at 4 (value of _val), if I leave them as they are then both get 4, I don't understand how that is happening when I very clearly stated which I wanted to hold what, anyone have any ideas? Edit: Btw I've already tried rebuilding everything

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    You need a real tab instead of space
    Makefile:2: *** missing separator. Stop - Stack Overflow
    "without goto we would be wtf'd"

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    By that I assume you mean when under a target, this is supposed to be executed BEFORE any targets, or am I mistaken? Here's a slightly larger snippet:
    Code:
    COMPILE_EXE=$(CC) $(BIN_FLAGS) $1 $(COP)o $2 $3 $(RPATH_FLAG)
    COMPILE_DLL=$(CC) $(LIB_FLAGS) $1 $(COP)o $2 $3 $(RPATH_FLAG)
    COMPILE_OBJ=$(CC) $(SRC_FLAGS) $1 $(COP)o $2 -c $3
    
    $(info PRJ_SRC_FILES = '$(PRJ_SRC_FILES)')
    
    $(call mkdir,$(PRJ_EXT_DIR))
    $(call github_clone,$(FBSTDC_DIR),$(PRJ_EXT_DIR),awsdert/fbstdc)
    $(info $(call rebase,$(FBSTDC_DIR)))
    $(shell $(call rebase,$(FBSTDC_DIR)))
    
    info: .FORCE

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    As it turns out the stack smashing appears to actually be in main(), tried moving the call to later on and tried seeing what other group tests did, it still reported stack smashing, from what little research I did on stack smashing I can't see any particular reason for it to occur unless I misunderstood something, here's what main looks like:
    Code:
    int main()
    {
    	int ret = 0;
    	uint_t preallocate = 32;
    	//uint_t seed = time(NULL);
    	bool print_anyways = false;
    	
    	alu_t _alu = {0}, *alu = &_alu;
    	
    	//print_limits();
    
    	alu_printf( "Pre-allocating %u ALU registers...", preallocate );
    	ret = alu_setup_reg( alu, preallocate, 0, 0 );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return EXIT_FAILURE;
    	}
    
    #if 1
    	ret = compare( alu, print_anyways );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		goto fail;
    	}
    #endif
    
    #if 0
    	ret = bitwise( alu, true, true, print_anyways );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		goto fail;
    	}
    #endif
    
    #if 0
    	ret = mathmatical( alu, false, true, print_anyways );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		goto fail;
    	}
    #endif
    
    	ret = print_value( alu, true );
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		goto fail;
    	}
    	
    #if 0
    	for ( num = 0; num < 0x10; ++num )
    		alu_printf( "%u", (seed = rng(seed)) );
    #endif
    
    	if ( ret != 0 )
    		alu_error( ret );
    		
    		
    	fail:
    	
    	(void)alu_vec_shrink( alu, 0, 0 );
    	(void)memset( alu, 0, sizeof(alu_t) );
    	
    	return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    }
    Any ideas? Reminder of code source: GitHub - awsdert/alu: Library modeled after the Arithmetic Logic Unit built into CPUs

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Tried making some functions I was inevitably gonna make anyways, swapped out the calls for them instead, still getting the same problem, here's the called code:
    Code:
    	alu_int_set_raw( alu, num.node, _num );
    	alu_int_set_raw( alu, val.node, _val );
    ...
    int_t alu_int_set_raw( alu_t *alu, alu_int_t num, intmax_t val )
    {
    	alu_reg_t NUM;
    	
    	alu_reg_init( alu, NUM, num, ALU_INFO__SIGN );
    	
    	return alu_reg_set_raw( alu, NUM, &val, sizeof(intmax_t), ALU_INFO__SIGN );
    }
    ...
    int_t alu_reg_set_raw
    (
    	alu_t *alu
    	, alu_reg_t num
    	, void *raw
    	, size_t size
    	, uint_t info
    )
    {
    	int ret;
    	void *part;
    	uint_t _tmp;
    	alu_reg_t tmp;
    	
    	size = HIGHEST( size, 1 );
    	ret = alu_get_reg_node( alu, &_tmp, size );
    	
    	if ( ret == 0 )
    	{
    		alu_reg_init( alu, tmp, _tmp, info );
    		
    		part = alu_reg_data( alu, tmp );
    		(void)memcpy( part, raw, size );
    		
    		ret = alu_reg_mov( alu, num, tmp );
    		
    		alu_rem_reg_node( alu, &_tmp );
    		return ret;
    	}
    	
    	alu_error( ret );
    	return ret;
    }
    ...
    int alu_reg_mov(
    	alu_t *alu,
    	alu_reg_t num,
    	alu_reg_t val
    )
    {
    	void *num_part, *val_part;
    	alu_bit_t n = {0}, v, e;
    	
    	if ( alu )
    	{
    		num.node %= alu_used( alu );
    		val.node %= alu_used( alu );
    	
    		num_part = alu_reg_data( alu, num );
    		val_part = alu_reg_data( alu, val );
    		
    		n = alu_bit_set_bit( num_part, num.from );
    		v = alu_bit_set_bit( val_part, val.from );
    				
    		e = alu_bit_set_bit(
    			num_part
    			, (num.upto * (num.upto <= val.upto))
    			| (val.upto * (val.upto <= num.upto))
    		);
    		
    		for ( ; n.bit < e.bit; n = alu_bit_inc(n), v = alu_bit_inc(v) )
    		{
    			*(n.ptr) &= ~(n.mask);
    			*(n.ptr) |= n.mask * !!(*(v.ptr) & v.mask);
    		}
    		
    		for ( ; n.bit < num.upto; n = alu_bit_inc(n) )
    		{
    			*(n.ptr) &= ~(n.mask);
    		}
    		
    		return 0;
    	}
    	
    	return alu_err_null_ptr( "alu" );
    }
    ...
    #define alu_reg_init( alu, alu_reg, reg, inf ) \
    	do \
    	{ \
    		(alu_reg).node = reg; \
    		(alu_reg).info = inf; \
    		(alu_reg).mant = 0; \
    		(alu_reg).from = 0; \
    		(alu_reg).upto = alu_size_perN( alu ) * CHAR_BIT; \
    	} \
    	while (0)
    Anyone have any ideas? Edit: Just noticed I pasted a duplicate thinking I had copied the next function, rectified now

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Managed to narrow the source of the problem to here:
    Code:
    int_t alu_reg_set_raw
    (
    	alu_t *alu
    	, alu_reg_t num
    	, void *raw
    	, size_t size
    	, uint_t info
    )
    {
    	int ret;
    	void *part;
    	uint_t _tmp;
    	alu_reg_t tmp;
    	
    	size = HIGHEST( size, 1 );
    	
    	ret = alu_get_reg_node( alu, &_tmp, size );
    	
    	if ( ret == 0 )
    	{
    		alu_reg_init( alu, tmp, _tmp, info );
    		
    		part = alu_reg_data( alu, tmp );
    		(void)memmove( part, raw, size );
    		
    		alu_print_reg( "tmp", alu, tmp, 0, 1 ); // Does not display the expected result here
    		
    		//tmp.upto = size * CHAR_BIT;
    		
    		ret = alu_reg_mov( alu, num, tmp );
    		
    		alu_rem_reg_node( alu, &_tmp );
    		return ret;
    	}
    	
    	alu_error( ret );
    	return ret;
    }
    It seems the value is not being copied into tmp as it should be, any ideas? Edit: I've checked the value of raw and it definitely holds the expected value, it's just somehow being ignored when passed to tmp (even attempted a manual loop)

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    I resolved it by putting an $(info) around the $(shell) part

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    First, a link to the project:
    GitHub - awsdert/alu: Library modeled after the Arithmetic Logic Unit built into CPUs
    Now for the main part, I ran into an inconsistency between the results of multiplication, small numbers work fine but a larger number resulted in an empty value when the opposite should have been there, someone mind taking a look at alu_mul() (in alu_math.c) and seeing if they can find potential causes

    Edit: Occurred to me just after hitting post that I could've just posted the code also:
    Code:
    int alu_mul( alu_t *alu, int num, int val )
    {
    	bool carry = 0;
    	int ret = alu_check2( alu, num, val ), tmp = 0;
    	alu_reg_t *N, *V, *T;
    	alu_bit_t v = {0}, e;
    	size_t bits = 0;
    	
    	if ( ret != 0 )
    		return ret;
    	
    	ret = alu_get_reg( alu, &tmp, sizeof(size_t) );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	T = alu->regv + tmp;
    
    	v = V->init;
    	e = N->upto;
    	
    	for ( ; v.b < e.b; v = alu_bit_inc( v ), ++bits )
    	{
    		if ( *(v.S) & v.B )
    		{
    			(void)alu__shl( alu, num, bits );
    			ret = alu_add( alu, tmp, num );
    			bits = 0;
    			
    			if ( ret == EOVERFLOW )
    				carry = true;
    			else if ( ret != 0 )
    				break;
    		}
    	}
    	
    	memcpy( N->part, T->part, alu->buff.perN );
    	alu_rem_reg( alu, tmp );
    	
    	return carry ? EOVERFLOW : 0;
    }

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Managed to narrow the problematic line but unable to figure out a fix, used a fix that is slower to resolve the output temporarily:
    Code:
    int alu_mul( alu_t *alu, int num, int val )
    {
    	bool carry = 0;
    	int ret = alu_check2( alu, num, val ), tmp = -1;
    	alu_reg_t *V;
    	alu_bit_t p, v = {0}, e;
    	size_t bits = 0;
    	
    	if ( ret != 0 )
    		return ret;
    	
    	ret = alu_get_reg( alu, &tmp, sizeof(size_t) );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	V = alu->regv + val;
    	p = v = V->init;
    	e = V->upto;
    	
    	alu__or( alu, tmp, num );
    	alu_xor( alu, num, num );
    	
    	for ( ; v.b < e.b; v = alu_bit_inc( v ), ++bits )
    	{
    		if ( *(v.S) & v.B )
    		{
    			//(void)alu__shl( alu, tmp, bits );
    			ret = alu_add( alu, num, tmp );
    			bits = 0;
    			
    			if ( ret == EOVERFLOW )
    				carry = true;
    			else if ( ret == ENODATA )
    				break;
    			else if ( ret != 0 )
    			{
    				alu_error( ret );
    				break;
    			}
    		}
    		(void)alu__shl( alu, tmp, 1 );
    	}
    	
    	alu_rem_reg( alu, tmp );
    	
    	return carry ? EOVERFLOW : ret;
    }
    As you can see I commented out the problematic line, did some tests with the shift function and I don't think that was the cause, I believe maybe the value of bits is somehow incorrect, any ideas are welcome

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Found the cause, I forgot to check for the situation where the number of bits to move by was 0, after skipping the shift in that situation the erroneous output was resolved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone help to spot mistake in the code
    By chess_queen in forum C Programming
    Replies: 1
    Last Post: 10-21-2012, 10:37 AM
  2. Can you spot my mistake?
    By Brewer in forum C Programming
    Replies: 13
    Last Post: 11-12-2006, 12:50 PM
  3. going to a certain spot in a file...
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 02:31 AM

Tags for this Thread