From 3e22eb797f02cdb54a5eccc8c6ebce33e1e1d7d5 Mon Sep 17 00:00:00 2001 From: Drahflow Date: Sat, 11 Apr 2015 00:59:04 +0200 Subject: Sane handling of integer division --- compiler/elymasAsm.ey | 2 +- compiler/elymasAsmLib.ey | 18 +++++++++--------- compiler/elymasAsmOps.ey | 17 +++++++++++++---- compiler/elymasGlobal.ey | 17 ++++++++++++++++- compiler/standardClient.ey | 14 +++++++------- 5 files changed, 46 insertions(+), 22 deletions(-) (limited to 'compiler') diff --git a/compiler/elymasAsm.ey b/compiler/elymasAsm.ey index 265ff95..ff57627 100644 --- a/compiler/elymasAsm.ey +++ b/compiler/elymasAsm.ey @@ -113,7 +113,7 @@ .base ==:quoteEncodingBufferObjects { ==opcodes - opcodes len 1 sub PAGESIZE div 1 add PAGESIZE mul alloc /codearea defv + opcodes len 1 sub PAGESIZE udiv 1 add PAGESIZE mul alloc /codearea defv sys .asm .|poke =*poke codearea .base opcodes { -101 poke 1 add } each -- codearea diff --git a/compiler/elymasAsmLib.ey b/compiler/elymasAsmLib.ey index 952e6b7..71763c6 100644 --- a/compiler/elymasAsmLib.ey +++ b/compiler/elymasAsmLib.ey @@ -26,7 +26,7 @@ struct values |cat fold ==data constantAllocEnd constantAllocBegin sub ==constantAllocFree data len constantAllocFree gt { - data len 1 sub :PAGESIZE div 1 add :PAGESIZE mul :alloc ==area + data len 1 sub :PAGESIZE udiv 1 add :PAGESIZE mul :alloc ==area area :globalAllocations .register area .base _ constantAllocEnd neq { area .base =constantAllocBegin } rep area .size add =constantAllocEnd @@ -43,7 +43,7 @@ %00 %00 %00 %00 %00 %00 %00 %00 str len :imm64 ] str strToUTF8Bytes cat - [ 8 str len 8 mod sub %00 rep ] cat + [ 8 str len 8 umod sub %00 rep ] cat } /toConstString deff [ ] ==stringHoles @@ -110,8 +110,8 @@ > { defv }' allocateOffsetStruct { ==register - { _ =*array len _ 4 div ==largeMoves - 4 mod ==smallMoves + { _ =*array len _ 4 udiv ==largeMoves + 4 umod ==smallMoves 0 ==i largeMoves { i _ 4 add =i @@ -382,7 +382,7 @@ 0 /rsi :cmpqImm8Mem /quoteEncodingBufferUnused :jzLbl8 - :STACKSIZE 8 sub 8 div /rcx :movqImmReg + :STACKSIZE 8 sub 8 udiv /rcx :movqImmReg @loopThroughEncodingBuffer /rsi /rdi :movqMemReg /markObject :callqLbl32 @@ -775,7 +775,7 @@ heapSize /rax :movqImmReg /rax /rdi :movqMemReg 7 /rdi :shrqImm8Reg - ALLOCCHUNKSIZE 128 div /rsi :movqImmReg + ALLOCCHUNKSIZE 128 udiv /rsi :movqImmReg /rsi /rdi :subqRegReg /rax :movqImmOOBReg BLOCKBASE /rax /rdi :addqRegReg @@ -784,7 +784,7 @@ heapSize /rax :movqImmReg /rax /rdi :movqMemReg 7 /rdi :shrqImm8Reg - ALLOCCHUNKSIZE 128 div /rsi :movqImmReg + ALLOCCHUNKSIZE 128 udiv /rsi :movqImmReg /rsi /rdi :subqRegReg /rax :movqImmOOBReg MARKBASE /rax /rdi :addqRegReg @@ -1304,7 +1304,7 @@ ] /unscopingFunctionFooter defv { strToUTF8Bytes _ =*v len _ ==exactLength - 1 sub 8 div 4 add 8 mul ==memoryLength + 1 sub 8 udiv 4 add 8 mul ==memoryLength memoryLength 2147483648 lt not { "constant string too long" die } rep @@ -1333,7 +1333,7 @@ exactLength 0 neq { # load string contents - 0 exactLength 1 sub 8 div 1 add range { 8 mul ==i + 0 exactLength 1 sub 8 udiv 1 add range { 8 mul ==i 8 /rax :addqImm8Reg /rdx :movqImmOOBReg i _ 8 add range v 8 dearray /rdx /rax :movqRegMem diff --git a/compiler/elymasAsmOps.ey b/compiler/elymasAsmOps.ey index 64b80f2..3adba21 100644 --- a/compiler/elymasAsmOps.ey +++ b/compiler/elymasAsmOps.ey @@ -119,10 +119,10 @@ %07 base regno band add } /sib deff -{ 8 { _ 256 mod -01 256 div } rep -- } /imm64 deff # div / mod unsigned -{ _ 0 lt { 4294967296 add } rep 4294967295 band 4 { _ 256 mod -01 256 div } rep -- } /imm32 deff -{ _ 0 lt { 65536 add } rep 65535 band 2 { _ 256 mod -01 256 div } rep -- } /imm16 deff -{ _ 0 lt { 256 add } rep 255 band } /imm8 deff +{ 8 { _ 256 umod -01 256 udiv } rep -- } /imm64 deff # div / mod unsigned +{ _ 0 lt { 4294967296 add } rep 4294967295 band 4 { _ 256 umod -01 256 udiv } rep -- } /imm32 deff +{ _ 0 lt { 65536 add } rep 65535 band 2 { _ 256 umod -01 256 udiv } rep -- } /imm16 deff +{ _ 0 lt { 256 add } rep 255 band } /imm8 deff { %66 } /width16 deff @@ -797,6 +797,15 @@ memoryAddressingVariants keys { ==variant memoryAddressingVariants variant . =*p } /fildq variant defOp } each +memoryAddressingVariants keys { ==variant memoryAddressingVariants variant . =*parse + { parse ==mem + mem .base regno %07 gt mem .idx regno %07 gt or + { 0 /none mem .idx mem .base rex } rep + %DF + /seven mem .encode + } /fistp64 variant defOp +} each + memoryAddressingVariants keys { ==variant memoryAddressingVariants variant . =*parse { parse ==mem mem .base regno %07 gt mem .idx regno %07 gt or diff --git a/compiler/elymasGlobal.ey b/compiler/elymasGlobal.ey index 0ddc202..2363c6b 100644 --- a/compiler/elymasGlobal.ey +++ b/compiler/elymasGlobal.ey @@ -3446,13 +3446,28 @@ [ /rax :pushqReg /rdx /rax :movqRegReg - /rdx /rdx :xorqRegReg + :cqo /rcx :idivqReg /rax /rdx :movqRegReg /rax :popqReg ] [ :fdivp ] 0 makeFullArith /eydiv defv + [[ + /rax :pushqReg + /rdx /rax :movqRegReg + /rdx /rdx :xorqRegReg + /rcx :divqReg + /rax :popqReg + ]] [[ "umod" floatUnsupported ]] 0 makeFullArith /eyumod defv + [ + /rax :pushqReg + /rdx /rax :movqRegReg + /rdx /rdx :xorqRegReg + /rcx :divqReg + /rax /rdx :movqRegReg + /rax :popqReg + ]] [[ "udiv" floatUnsupported ]] 0 makeFullArith /eyudiv defv [[ /rcx /rcx :testqRegReg diff --git a/compiler/standardClient.ey b/compiler/standardClient.ey index 6df429c..b31011c 100644 --- a/compiler/standardClient.ey +++ b/compiler/standardClient.ey @@ -80,7 +80,7 @@ < 0 ==size [ maxSize { 0 }" rep ] =*get - maxSize 1 sub 8 div 1 add 8 mul str .alloc _ zero ==pcUsed + maxSize 1 sub 8 udiv 1 add 8 mul str .alloc _ zero ==pcUsed { # ==thread _ threadGetPC pcUsed bitTest { -- }" { @@ -898,8 +898,8 @@ sys .asm .intToFloat } each ] =*:FLOAT - { - [ -01 16 { _ 16 mod base16digits * -01 16 div } rep -- ] reverse str .fromArray + { _ 0 lt { neg "-" } { "" } ? * -01 + [ -01 16 { _ 16 umod base16digits * -01 16 udiv } rep -- ] reverse str .fromArray cat } /base16encode64 deffd { ==indent _ ==o @@ -985,13 +985,13 @@ 1 0 { strNumber * 48 sub [ 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 10 11 12 13 14 15 ] * } -20*10* 16 mul add } "%" defq - { 8 { _ 256 mod -01 256 div } rep -- } /uint64 deffd - { _ 0 lt { 4294967296 add } rep 4294967295 band 4 { _ 256 mod -01 256 div } rep -- } /uint32 deffd - { _ 0 lt { 65536 add } rep 65535 band 2 { _ 256 mod -01 256 div } rep -- } /uint16 deffd + { 8 { _ 256 umod -01 256 udiv } rep -- } /uint64 deffd + { _ 0 lt { 4294967296 add } rep 4294967295 band 4 { _ 256 umod -01 256 udiv } rep -- } /uint32 deffd + { _ 0 lt { 65536 add } rep 65535 band 2 { _ 256 umod -01 256 udiv } rep -- } /uint16 deffd { _ 0 lt { 256 add } rep 255 band } /uint8 deffd { ==align ==value - align value align mod sub align mod + align value align umod sub align umod } /alignUpto deff sys .file ==out -- cgit v1.2.3