Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential optimization: -mno-callee-assume-ss-data-segment overzealous with %ds restoring #140

Open
asiekierka opened this issue Jun 7, 2023 · 0 comments

Comments

@asiekierka
Copy link

asiekierka commented Jun 7, 2023

Calls to three external functions in order:

void main(int argc, char** argv) {
        wwc_get_hardarch();
        wwc_get_hardarch();
        wwc_get_hardarch();
        test();
}

with these compiler flags: /opt/wonderful/toolchain/gcc-ia16-elf/bin/ia16-elf-gcc -std=gnu11 -Wall -march=v30mz -mtune=v30mz -mregparmcall -ffreestanding -mcmodel=small -mno-callee-assume-ss-data-segment -msegelf -mno-segment-relocation-stuff -fexec-charset=shift-jis -Iinclude -Ibuild/cbin -I/opt/wonderful/target/wwitch/include -ffunction-sections -fdata-sections -O2 -MMD -MP -c -o build/src/main.c.o src/main.c, become the following machine code:

000000a4 <main>:
  a4:	06                   	push   %es
  a5:	1e                   	push   %ds
  a6:	07                   	pop    %es
  a7:	e8 10 00             	call   ba <wwc_get_hardarch>
  aa:	06                   	push   %es
  ab:	1f                   	pop    %ds
  ac:	e8 0b 00             	call   ba <wwc_get_hardarch>
  af:	06                   	push   %es
  b0:	1f                   	pop    %ds
  b1:	e8 06 00             	call   ba <wwc_get_hardarch>
  b4:	06                   	push   %es
  b5:	1f                   	pop    %ds
  b6:	07                   	pop    %es
  b7:	e9 db ff             	jmp    95 <test>

This strikes me as odd - according to the documentation:

@item
@code{ds} is a special case.  Both @code{ds} and @code{ss} are assumed to
point to the program's data segment on entry, and @code{ds} should be
restored on exit.  A function is free to modify @code{ds}, but it should
restore it to its initial value---perhaps via a @samp{pushw %ss; popw
%ds}---before calling another function, and before returning to its caller.

and so, if SS cannot be assumed to point to the program's data segment but DS can (on this platform, SS == the program's stack segment == 0x0000 and DS == the program's data segment == 0x1000), I would not expect the caller to be the party responsible for preserving the state of DS, but rather the callee - which does appear to be the case at least in the code I've examined.

I assume this is because DS can be used by the caller?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant